parser_test.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rss // import "miniflux.app/reader/rss"
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. )
  10. func TestParseRss2Sample(t *testing.T) {
  11. data := `
  12. <?xml version="1.0"?>
  13. <rss version="2.0">
  14. <channel>
  15. <title>Liftoff News</title>
  16. <link>http://liftoff.msfc.nasa.gov/</link>
  17. <description>Liftoff to Space Exploration.</description>
  18. <language>en-us</language>
  19. <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
  20. <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
  21. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  22. <generator>Weblog Editor 2.0</generator>
  23. <managingEditor>editor@example.com</managingEditor>
  24. <webMaster>webmaster@example.com</webMaster>
  25. <item>
  26. <title>Star City</title>
  27. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  28. <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>
  29. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  30. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  31. </item>
  32. <item>
  33. <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.</description>
  34. <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
  35. <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
  36. </item>
  37. <item>
  38. <title>The Engine That Does More</title>
  39. <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
  40. <description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
  41. <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
  42. <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
  43. </item>
  44. <item>
  45. <title>Astronauts' Dirty Laundry</title>
  46. <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
  47. <description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
  48. <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
  49. <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
  50. </item>
  51. </channel>
  52. </rss>`
  53. feed, err := Parse("http://liftoff.msfc.nasa.gov/rss.xml", bytes.NewBufferString(data))
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if feed.Title != "Liftoff News" {
  58. t.Errorf("Incorrect title, got: %s", feed.Title)
  59. }
  60. if feed.FeedURL != "http://liftoff.msfc.nasa.gov/rss.xml" {
  61. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  62. }
  63. if feed.SiteURL != "http://liftoff.msfc.nasa.gov/" {
  64. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  65. }
  66. if len(feed.Entries) != 4 {
  67. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  68. }
  69. expectedDate := time.Date(2003, time.June, 3, 9, 39, 21, 0, time.UTC)
  70. if !feed.Entries[0].Date.Equal(expectedDate) {
  71. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  72. }
  73. if feed.Entries[0].Hash != "5b2b4ac2fe1786ddf0fd2da2f1b07f64e691264f41f2db3ea360f31bb6d9152b" {
  74. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  75. }
  76. if feed.Entries[0].URL != "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp" {
  77. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  78. }
  79. if feed.Entries[0].Title != "Star City" {
  80. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  81. }
  82. if feed.Entries[0].Content != `How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.` {
  83. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  84. }
  85. }
  86. func TestParseFeedWithoutTitle(t *testing.T) {
  87. data := `<?xml version="1.0" encoding="utf-8"?>
  88. <rss version="2.0">
  89. <channel>
  90. <link>https://example.org/</link>
  91. </channel>
  92. </rss>`
  93. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if feed.Title != "https://example.org/" {
  98. t.Errorf("Incorrect feed title, got: %s", feed.Title)
  99. }
  100. }
  101. func TestParseEntryWithoutTitleAndDescription(t *testing.T) {
  102. data := `<?xml version="1.0" encoding="utf-8"?>
  103. <rss version="2.0">
  104. <channel>
  105. <link>https://example.org/</link>
  106. <item>
  107. <link>https://example.org/item</link>
  108. </item>
  109. </channel>
  110. </rss>`
  111. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if feed.Entries[0].Title != "https://example.org/item" {
  116. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  117. }
  118. }
  119. func TestParseEntryWithoutTitleButWithDescription(t *testing.T) {
  120. data := `<?xml version="1.0" encoding="utf-8"?>
  121. <rss version="2.0">
  122. <channel>
  123. <link>https://example.org/</link>
  124. <item>
  125. <link>https://example.org/item</link>
  126. <description>
  127. This is the description
  128. </description>
  129. </item>
  130. </channel>
  131. </rss>`
  132. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if feed.Entries[0].Title != "This is the description" {
  137. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  138. }
  139. }
  140. func TestParseEntryWithMediaTitle(t *testing.T) {
  141. data := `<?xml version="1.0" encoding="utf-8"?>
  142. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  143. <channel>
  144. <link>https://example.org/</link>
  145. <item>
  146. <title>Entry Title</title>
  147. <link>https://example.org/item</link>
  148. <media:title>Media Title</media:title>
  149. </item>
  150. </channel>
  151. </rss>`
  152. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. if feed.Entries[0].Title != "Entry Title" {
  157. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  158. }
  159. }
  160. func TestParseEntryWithDCTitleOnly(t *testing.T) {
  161. data := `<?xml version="1.0" encoding="utf-8"?>
  162. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  163. <channel>
  164. <link>https://example.org/</link>
  165. <item>
  166. <dc:title>Entry Title</dc:title>
  167. <link>https://example.org/item</link>
  168. </item>
  169. </channel>
  170. </rss>`
  171. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if feed.Entries[0].Title != "Entry Title" {
  176. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  177. }
  178. }
  179. func TestParseEntryWithoutLink(t *testing.T) {
  180. data := `<?xml version="1.0" encoding="utf-8"?>
  181. <rss version="2.0">
  182. <channel>
  183. <link>https://example.org/</link>
  184. <item>
  185. <guid isPermaLink="false">1234</guid>
  186. </item>
  187. </channel>
  188. </rss>`
  189. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. if feed.Entries[0].URL != "https://example.org/" {
  194. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  195. }
  196. if feed.Entries[0].Hash != "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4" {
  197. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  198. }
  199. }
  200. func TestParseEntryWithAtomLink(t *testing.T) {
  201. data := `<?xml version="1.0" encoding="utf-8"?>
  202. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  203. <channel>
  204. <link>https://example.org/</link>
  205. <item>
  206. <title>Test</title>
  207. <atom:link href="https://example.org/item" />
  208. </item>
  209. </channel>
  210. </rss>`
  211. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. if feed.Entries[0].URL != "https://example.org/item" {
  216. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  217. }
  218. }
  219. func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
  220. data := `<?xml version="1.0" encoding="utf-8"?>
  221. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  222. <channel>
  223. <link>https://example.org/</link>
  224. <item>
  225. <title>Test</title>
  226. <atom:link rel="payment" href="https://example.org/a" />
  227. <atom:link rel="http://foobar.tld" href="https://example.org/b" />
  228. </item>
  229. </channel>
  230. </rss>`
  231. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if feed.Entries[0].URL != "https://example.org/b" {
  236. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  237. }
  238. }
  239. func TestParseFeedURLWithAtomLink(t *testing.T) {
  240. data := `<?xml version="1.0" encoding="utf-8"?>
  241. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  242. <channel>
  243. <title>Example</title>
  244. <link>https://example.org/</link>
  245. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  246. </channel>
  247. </rss>`
  248. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. if feed.FeedURL != "https://example.org/rss" {
  253. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  254. }
  255. if feed.SiteURL != "https://example.org/" {
  256. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  257. }
  258. }
  259. func TestParseFeedWithWebmaster(t *testing.T) {
  260. data := `<?xml version="1.0" encoding="utf-8"?>
  261. <rss version="2.0">
  262. <channel>
  263. <title>Example</title>
  264. <link>https://example.org/</link>
  265. <webMaster>webmaster@example.com</webMaster>
  266. <item>
  267. <title>Test</title>
  268. <link>https://example.org/item</link>
  269. </item>
  270. </channel>
  271. </rss>`
  272. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. expected := "webmaster@example.com"
  277. result := feed.Entries[0].Author
  278. if result != expected {
  279. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  280. }
  281. }
  282. func TestParseFeedWithManagingEditor(t *testing.T) {
  283. data := `<?xml version="1.0" encoding="utf-8"?>
  284. <rss version="2.0">
  285. <channel>
  286. <title>Example</title>
  287. <link>https://example.org/</link>
  288. <webMaster>webmaster@example.com</webMaster>
  289. <managingEditor>editor@example.com</managingEditor>
  290. <item>
  291. <title>Test</title>
  292. <link>https://example.org/item</link>
  293. </item>
  294. </channel>
  295. </rss>`
  296. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. expected := "editor@example.com"
  301. result := feed.Entries[0].Author
  302. if result != expected {
  303. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  304. }
  305. }
  306. func TestParseEntryWithAuthorAndInnerHTML(t *testing.T) {
  307. data := `<?xml version="1.0" encoding="utf-8"?>
  308. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  309. <channel>
  310. <title>Example</title>
  311. <link>https://example.org/</link>
  312. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  313. <item>
  314. <title>Test</title>
  315. <link>https://example.org/item</link>
  316. <author>by <a itemprop="url" class="author" rel="author" href="/author/foobar">Foo Bar</a></author>
  317. </item>
  318. </channel>
  319. </rss>`
  320. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. expected := "by Foo Bar"
  325. result := feed.Entries[0].Author
  326. if result != expected {
  327. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  328. }
  329. }
  330. func TestParseEntryWithAuthorAndCDATA(t *testing.T) {
  331. data := `<?xml version="1.0" encoding="utf-8"?>
  332. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  333. <channel>
  334. <title>Example</title>
  335. <link>https://example.org/</link>
  336. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  337. <item>
  338. <title>Test</title>
  339. <link>https://example.org/item</link>
  340. <author>
  341. by <![CDATA[Foo Bar]]>
  342. </author>
  343. </item>
  344. </channel>
  345. </rss>`
  346. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. expected := "by Foo Bar"
  351. result := feed.Entries[0].Author
  352. if result != expected {
  353. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  354. }
  355. }
  356. func TestParseEntryWithNonStandardAtomAuthor(t *testing.T) {
  357. data := `<?xml version="1.0" encoding="utf-8"?>
  358. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  359. <channel>
  360. <title>Example</title>
  361. <link>https://example.org/</link>
  362. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  363. <item>
  364. <title>Test</title>
  365. <link>https://example.org/item</link>
  366. <author xmlns:author="http://www.w3.org/2005/Atom">
  367. <name>Foo Bar</name>
  368. <title>Vice President</title>
  369. <department/>
  370. <company>FooBar Inc.</company>
  371. </author>
  372. </item>
  373. </channel>
  374. </rss>`
  375. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. expected := "Foo Bar"
  380. result := feed.Entries[0].Author
  381. if result != expected {
  382. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  383. }
  384. }
  385. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  386. data := `<?xml version="1.0" encoding="utf-8"?>
  387. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  388. <channel>
  389. <title>Example</title>
  390. <link>https://example.org/</link>
  391. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  392. <item>
  393. <title>Test</title>
  394. <link>https://example.org/item</link>
  395. <atom:author>
  396. <email>author@example.org</email>
  397. </atom:author>
  398. </item>
  399. </channel>
  400. </rss>`
  401. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. expected := "author@example.org"
  406. result := feed.Entries[0].Author
  407. if result != expected {
  408. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  409. }
  410. }
  411. func TestParseEntryWithAtomAuthor(t *testing.T) {
  412. data := `<?xml version="1.0" encoding="utf-8"?>
  413. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  414. <channel>
  415. <title>Example</title>
  416. <link>https://example.org/</link>
  417. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  418. <item>
  419. <title>Test</title>
  420. <link>https://example.org/item</link>
  421. <atom:author>
  422. <name>Foo Bar</name>
  423. </atom:author>
  424. </item>
  425. </channel>
  426. </rss>`
  427. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  428. if err != nil {
  429. t.Fatal(err)
  430. }
  431. expected := "Foo Bar"
  432. result := feed.Entries[0].Author
  433. if result != expected {
  434. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  435. }
  436. }
  437. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  438. data := `<?xml version="1.0" encoding="utf-8"?>
  439. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  440. <channel>
  441. <title>Example</title>
  442. <link>https://example.org/</link>
  443. <item>
  444. <title>Test</title>
  445. <link>https://example.org/item</link>
  446. <dc:creator>Me (me@example.com)</dc:creator>
  447. </item>
  448. </channel>
  449. </rss>`
  450. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  451. if err != nil {
  452. t.Fatal(err)
  453. }
  454. expected := "Me (me@example.com)"
  455. result := feed.Entries[0].Author
  456. if result != expected {
  457. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  458. }
  459. }
  460. func TestParseEntryWithItunesAuthor(t *testing.T) {
  461. data := `<?xml version="1.0" encoding="utf-8"?>
  462. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  463. <channel>
  464. <title>Example</title>
  465. <link>https://example.org/</link>
  466. <item>
  467. <title>Test</title>
  468. <link>https://example.org/item</link>
  469. <itunes:author>Someone</itunes:author>
  470. </item>
  471. </channel>
  472. </rss>`
  473. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. expected := "Someone"
  478. result := feed.Entries[0].Author
  479. if result != expected {
  480. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  481. }
  482. }
  483. func TestParseFeedWithItunesAuthor(t *testing.T) {
  484. data := `<?xml version="1.0" encoding="utf-8"?>
  485. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  486. <channel>
  487. <title>Example</title>
  488. <link>https://example.org/</link>
  489. <itunes:author>Someone</itunes:author>
  490. <item>
  491. <title>Test</title>
  492. <link>https://example.org/item</link>
  493. </item>
  494. </channel>
  495. </rss>`
  496. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  497. if err != nil {
  498. t.Fatal(err)
  499. }
  500. expected := "Someone"
  501. result := feed.Entries[0].Author
  502. if result != expected {
  503. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  504. }
  505. }
  506. func TestParseFeedWithItunesOwner(t *testing.T) {
  507. data := `<?xml version="1.0" encoding="utf-8"?>
  508. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  509. <channel>
  510. <title>Example</title>
  511. <link>https://example.org/</link>
  512. <itunes:owner>
  513. <itunes:name>John Doe</itunes:name>
  514. <itunes:email>john.doe@example.com</itunes:email>
  515. </itunes:owner>
  516. <item>
  517. <title>Test</title>
  518. <link>https://example.org/item</link>
  519. </item>
  520. </channel>
  521. </rss>`
  522. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. expected := "John Doe"
  527. result := feed.Entries[0].Author
  528. if result != expected {
  529. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  530. }
  531. }
  532. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  533. data := `<?xml version="1.0" encoding="utf-8"?>
  534. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  535. <channel>
  536. <title>Example</title>
  537. <link>https://example.org/</link>
  538. <itunes:owner>
  539. <itunes:email>john.doe@example.com</itunes:email>
  540. </itunes:owner>
  541. <item>
  542. <title>Test</title>
  543. <link>https://example.org/item</link>
  544. </item>
  545. </channel>
  546. </rss>`
  547. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  548. if err != nil {
  549. t.Fatal(err)
  550. }
  551. expected := "john.doe@example.com"
  552. result := feed.Entries[0].Author
  553. if result != expected {
  554. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  555. }
  556. }
  557. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  558. data := `<?xml version="1.0" encoding="utf-8"?>
  559. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  560. <channel>
  561. <title>Example</title>
  562. <link>https://example.org/</link>
  563. <item>
  564. <title>Test</title>
  565. <link>https://example.org/item</link>
  566. <googleplay:author>Someone</googleplay:author>
  567. </item>
  568. </channel>
  569. </rss>`
  570. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  571. if err != nil {
  572. t.Fatal(err)
  573. }
  574. expected := "Someone"
  575. result := feed.Entries[0].Author
  576. if result != expected {
  577. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  578. }
  579. }
  580. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  581. data := `<?xml version="1.0" encoding="utf-8"?>
  582. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  583. <channel>
  584. <title>Example</title>
  585. <link>https://example.org/</link>
  586. <googleplay:author>Someone</googleplay:author>
  587. <item>
  588. <title>Test</title>
  589. <link>https://example.org/item</link>
  590. </item>
  591. </channel>
  592. </rss>`
  593. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  594. if err != nil {
  595. t.Fatal(err)
  596. }
  597. expected := "Someone"
  598. result := feed.Entries[0].Author
  599. if result != expected {
  600. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  601. }
  602. }
  603. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  604. data := `<?xml version="1.0" encoding="utf-8"?>
  605. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  606. <channel>
  607. <title>Example</title>
  608. <link>http://example.org/</link>
  609. <item>
  610. <title>Item 1</title>
  611. <link>http://example.org/item1</link>
  612. <description>Description.</description>
  613. <guid isPermaLink="false">UUID</guid>
  614. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  615. </item>
  616. </channel>
  617. </rss>`
  618. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  619. if err != nil {
  620. t.Fatal(err)
  621. }
  622. location, _ := time.LoadLocation("EST")
  623. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  624. if !feed.Entries[0].Date.Equal(expectedDate) {
  625. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  626. }
  627. }
  628. func TestParseEntryWithContentEncoded(t *testing.T) {
  629. data := `<?xml version="1.0" encoding="utf-8"?>
  630. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  631. <channel>
  632. <title>Example</title>
  633. <link>http://example.org/</link>
  634. <item>
  635. <title>Item 1</title>
  636. <link>http://example.org/item1</link>
  637. <description>Description.</description>
  638. <guid isPermaLink="false">UUID</guid>
  639. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  640. </item>
  641. </channel>
  642. </rss>`
  643. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  644. if err != nil {
  645. t.Fatal(err)
  646. }
  647. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  648. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  649. }
  650. }
  651. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  652. data := `<?xml version="1.0" encoding="utf-8"?>
  653. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  654. <channel>
  655. <title>Example</title>
  656. <link>http://example.org/</link>
  657. <item>
  658. <title>Item 1</title>
  659. <link>http://example.org/item1</link>
  660. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  661. </item>
  662. </channel>
  663. </rss>`
  664. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  665. if err != nil {
  666. t.Fatal(err)
  667. }
  668. if feed.Entries[0].URL != "http://example.org/original" {
  669. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  670. }
  671. }
  672. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  673. data := `<?xml version="1.0" encoding="utf-8"?>
  674. <rss version="2.0">
  675. <channel>
  676. <title>Example</title>
  677. <link>http://example.org</link>
  678. <item>
  679. <title>
  680. Some Title
  681. </title>
  682. <link>http://www.example.org/entries/1</link>
  683. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  684. </item>
  685. </channel>
  686. </rss>`
  687. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  688. if err != nil {
  689. t.Fatal(err)
  690. }
  691. if feed.Entries[0].Title != "Some Title" {
  692. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  693. }
  694. }
  695. func TestParseEntryWithEnclosures(t *testing.T) {
  696. data := `<?xml version="1.0" encoding="utf-8"?>
  697. <rss version="2.0">
  698. <channel>
  699. <title>My Podcast Feed</title>
  700. <link>http://example.org</link>
  701. <author>some.email@example.org</author>
  702. <item>
  703. <title>Podcasting with RSS</title>
  704. <link>http://www.example.org/entries/1</link>
  705. <description>An overview of RSS podcasting</description>
  706. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  707. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  708. <enclosure url="http://www.example.org/myaudiofile.mp3"
  709. length="12345"
  710. type="audio/mpeg" />
  711. </item>
  712. </channel>
  713. </rss>`
  714. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. if len(feed.Entries) != 1 {
  719. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  720. }
  721. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  722. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  723. }
  724. if len(feed.Entries[0].Enclosures) != 1 {
  725. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  726. }
  727. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  728. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  729. }
  730. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  731. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  732. }
  733. if feed.Entries[0].Enclosures[0].Size != 12345 {
  734. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  735. }
  736. }
  737. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  738. data := `<?xml version="1.0" encoding="utf-8"?>
  739. <rss version="2.0">
  740. <channel>
  741. <title>My Podcast Feed</title>
  742. <link>http://example.org</link>
  743. <author>some.email@example.org</author>
  744. <item>
  745. <title>Podcasting with RSS</title>
  746. <link>http://www.example.org/entries/1</link>
  747. <description>An overview of RSS podcasting</description>
  748. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  749. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  750. <enclosure url="" length="0"/>
  751. </item>
  752. </channel>
  753. </rss>`
  754. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  755. if err != nil {
  756. t.Fatal(err)
  757. }
  758. if len(feed.Entries) != 1 {
  759. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  760. }
  761. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  762. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  763. }
  764. if len(feed.Entries[0].Enclosures) != 0 {
  765. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  766. }
  767. }
  768. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  769. data := `<?xml version="1.0" encoding="utf-8"?>
  770. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  771. <channel>
  772. <title>My Example Feed</title>
  773. <link>http://example.org</link>
  774. <author>some.email@example.org</author>
  775. <item>
  776. <title>Example Item</title>
  777. <link>http://www.example.org/entries/1</link>
  778. <enclosure
  779. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  780. length="76192460"
  781. type="audio/mpeg" />
  782. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  783. </item>
  784. </channel>
  785. </rss>`
  786. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  787. if err != nil {
  788. t.Fatal(err)
  789. }
  790. if len(feed.Entries) != 1 {
  791. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  792. }
  793. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  794. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  795. }
  796. if len(feed.Entries[0].Enclosures) != 1 {
  797. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  798. }
  799. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  800. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  801. }
  802. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  803. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  804. }
  805. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  806. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  807. }
  808. }
  809. func TestParseEntryWithRelativeURL(t *testing.T) {
  810. data := `<?xml version="1.0" encoding="utf-8"?>
  811. <rss version="2.0">
  812. <channel>
  813. <link>https://example.org/</link>
  814. <item>
  815. <link>item.html</link>
  816. </item>
  817. </channel>
  818. </rss>`
  819. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. if feed.Entries[0].Title != "https://example.org/item.html" {
  824. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  825. }
  826. }
  827. func TestParseEntryWithCommentsURL(t *testing.T) {
  828. data := `<?xml version="1.0" encoding="utf-8"?>
  829. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  830. <channel>
  831. <link>https://example.org/</link>
  832. <item>
  833. <title>Item 1</title>
  834. <link>https://example.org/item1</link>
  835. <comments>
  836. https://example.org/comments
  837. </comments>
  838. <slash:comments>42</slash:comments>
  839. </item>
  840. </channel>
  841. </rss>`
  842. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  843. if err != nil {
  844. t.Fatal(err)
  845. }
  846. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  847. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  848. }
  849. }
  850. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  851. data := `<?xml version="1.0" encoding="utf-8"?>
  852. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  853. <channel>
  854. <link>https://example.org/</link>
  855. <item>
  856. <title>Item 1</title>
  857. <link>https://example.org/item1</link>
  858. <comments>
  859. Some text
  860. </comments>
  861. </item>
  862. </channel>
  863. </rss>`
  864. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. if feed.Entries[0].CommentsURL != "" {
  869. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  870. }
  871. }
  872. func TestParseInvalidXml(t *testing.T) {
  873. data := `garbage`
  874. _, err := Parse("https://example.org/", bytes.NewBufferString(data))
  875. if err == nil {
  876. t.Error("Parse should returns an error")
  877. }
  878. }
  879. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  880. data := `<?xml version="1.0" encoding="utf-8"?>
  881. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  882. <channel>
  883. <link>https://example.org/</link>
  884. <title>Example &nbsp; Feed</title>
  885. </channel>
  886. </rss>`
  887. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  888. if err != nil {
  889. t.Fatal(err)
  890. }
  891. if feed.Title != "Example \u00a0 Feed" {
  892. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  893. }
  894. }
  895. func TestParseFeedTitleWithUnicodeEntityAndCdata(t *testing.T) {
  896. data := `<?xml version="1.0" encoding="utf-8"?>
  897. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  898. <channel>
  899. <link>https://example.org/</link>
  900. <title><![CDATA[Jenny&#8217;s Newsletter]]></title>
  901. </channel>
  902. </rss>`
  903. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  904. if err != nil {
  905. t.Fatal(err)
  906. }
  907. if feed.Title != `Jenny’s Newsletter` {
  908. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  909. }
  910. }
  911. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  912. data := `<?xml version="1.0" encoding="utf-8"?>
  913. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  914. <channel>
  915. <link>https://example.org/</link>
  916. <title>Example</title>
  917. <item>
  918. <title>&lt;/example&gt;</title>
  919. <link>http://www.example.org/entries/1</link>
  920. </item>
  921. </channel>
  922. </rss>`
  923. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  924. if err != nil {
  925. t.Fatal(err)
  926. }
  927. if feed.Entries[0].Title != "</example>" {
  928. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  929. }
  930. }
  931. func TestParseItemTitleWithNumericCharacterReference(t *testing.T) {
  932. data := `<?xml version="1.0" encoding="utf-8"?>
  933. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  934. <channel>
  935. <link>https://example.org/</link>
  936. <title>Example</title>
  937. <item>
  938. <title>&#931; &#xDF;</title>
  939. <link>http://www.example.org/article.html</link>
  940. </item>
  941. </channel>
  942. </rss>`
  943. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  944. if err != nil {
  945. t.Fatal(err)
  946. }
  947. if feed.Entries[0].Title != "Σ ß" {
  948. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  949. }
  950. }
  951. func TestParseItemTitleWithDoubleEncodedEntities(t *testing.T) {
  952. data := `<?xml version="1.0" encoding="utf-8"?>
  953. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  954. <channel>
  955. <link>https://example.org/</link>
  956. <title>Example</title>
  957. <item>
  958. <title>&amp;#39;Text&amp;#39;</title>
  959. <link>http://www.example.org/article.html</link>
  960. </item>
  961. </channel>
  962. </rss>`
  963. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  964. if err != nil {
  965. t.Fatal(err)
  966. }
  967. if feed.Entries[0].Title != "'Text'" {
  968. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  969. }
  970. }
  971. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  972. data := `<?xml version="1.0" encoding="utf-8"?>
  973. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  974. <channel>
  975. <link>https://example.org/a&b</link>
  976. <title>Example Feed</title>
  977. </channel>
  978. </rss>`
  979. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. if feed.SiteURL != "https://example.org/a&b" {
  984. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  985. }
  986. }
  987. func TestParseEntryWithMediaGroup(t *testing.T) {
  988. data := `<?xml version="1.0" encoding="utf-8"?>
  989. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  990. <channel>
  991. <title>My Example Feed</title>
  992. <link>http://example.org</link>
  993. <item>
  994. <title>Example Item</title>
  995. <link>http://www.example.org/entries/1</link>
  996. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  997. </enclosure>
  998. <media:group>
  999. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  1000. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  1001. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  1002. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1003. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  1004. <media:rating>nonadult</media:rating>
  1005. </media:group>
  1006. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  1007. </item>
  1008. </channel>
  1009. </rss>`
  1010. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1011. if err != nil {
  1012. t.Fatal(err)
  1013. }
  1014. if len(feed.Entries) != 1 {
  1015. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1016. }
  1017. if len(feed.Entries[0].Enclosures) != 6 {
  1018. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1019. }
  1020. expectedResults := []struct {
  1021. url string
  1022. mimeType string
  1023. size int64
  1024. }{
  1025. {"https://example.org/image.jpg", "image/*", 0},
  1026. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  1027. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  1028. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  1029. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  1030. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  1031. }
  1032. for index, enclosure := range feed.Entries[0].Enclosures {
  1033. if expectedResults[index].url != enclosure.URL {
  1034. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1035. }
  1036. if expectedResults[index].mimeType != enclosure.MimeType {
  1037. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1038. }
  1039. if expectedResults[index].size != enclosure.Size {
  1040. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1041. }
  1042. }
  1043. }
  1044. func TestParseEntryWithMediaContent(t *testing.T) {
  1045. data := `<?xml version="1.0" encoding="utf-8"?>
  1046. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1047. <channel>
  1048. <title>My Example Feed</title>
  1049. <link>http://example.org</link>
  1050. <item>
  1051. <title>Example Item</title>
  1052. <link>http://www.example.org/entries/1</link>
  1053. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1054. <media:content url="https://example.org/media1.jpg" medium="image">
  1055. <media:title type="html">Some Title for Media 1</media:title>
  1056. </media:content>
  1057. <media:content url="https://example.org/media2.jpg" medium="image" />
  1058. </item>
  1059. </channel>
  1060. </rss>`
  1061. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1062. if err != nil {
  1063. t.Fatal(err)
  1064. }
  1065. if len(feed.Entries) != 1 {
  1066. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1067. }
  1068. if len(feed.Entries[0].Enclosures) != 3 {
  1069. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1070. }
  1071. expectedResults := []struct {
  1072. url string
  1073. mimeType string
  1074. size int64
  1075. }{
  1076. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1077. {"https://example.org/media1.jpg", "image/*", 0},
  1078. {"https://example.org/media2.jpg", "image/*", 0},
  1079. }
  1080. for index, enclosure := range feed.Entries[0].Enclosures {
  1081. if expectedResults[index].url != enclosure.URL {
  1082. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1083. }
  1084. if expectedResults[index].mimeType != enclosure.MimeType {
  1085. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1086. }
  1087. if expectedResults[index].size != enclosure.Size {
  1088. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1089. }
  1090. }
  1091. }
  1092. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  1093. data := `<?xml version="1.0" encoding="utf-8"?>
  1094. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1095. <channel>
  1096. <title>My Example Feed</title>
  1097. <link>http://example.org</link>
  1098. <item>
  1099. <title>Example Item</title>
  1100. <link>http://www.example.org/entries/1</link>
  1101. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  1102. </item>
  1103. </channel>
  1104. </rss>`
  1105. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1106. if err != nil {
  1107. t.Fatal(err)
  1108. }
  1109. if len(feed.Entries) != 1 {
  1110. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1111. }
  1112. if len(feed.Entries[0].Enclosures) != 1 {
  1113. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1114. }
  1115. expectedResults := []struct {
  1116. url string
  1117. mimeType string
  1118. size int64
  1119. }{
  1120. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1121. }
  1122. for index, enclosure := range feed.Entries[0].Enclosures {
  1123. if expectedResults[index].url != enclosure.URL {
  1124. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1125. }
  1126. if expectedResults[index].mimeType != enclosure.MimeType {
  1127. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1128. }
  1129. if expectedResults[index].size != enclosure.Size {
  1130. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1131. }
  1132. }
  1133. }
  1134. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1135. data := `<?xml version="1.0" encoding="UTF-8"?>
  1136. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1137. <channel>
  1138. <title>Podcast Example</title>
  1139. <link>http://www.example.com/index.html</link>
  1140. <item>
  1141. <title>Podcast Episode</title>
  1142. <guid>http://example.com/episode.m4a</guid>
  1143. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1144. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1145. <itunes:summary>Episode Summary</itunes:summary>
  1146. </item>
  1147. </channel>
  1148. </rss>`
  1149. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1150. if err != nil {
  1151. t.Fatal(err)
  1152. }
  1153. if len(feed.Entries) != 1 {
  1154. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1155. }
  1156. expected := "Episode Summary"
  1157. result := feed.Entries[0].Content
  1158. if expected != result {
  1159. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1160. }
  1161. }
  1162. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1163. data := `<?xml version="1.0" encoding="UTF-8"?>
  1164. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1165. <channel>
  1166. <title>Podcast Example</title>
  1167. <link>http://www.example.com/index.html</link>
  1168. <item>
  1169. <title>Podcast Episode</title>
  1170. <guid>http://example.com/episode.m4a</guid>
  1171. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1172. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1173. </item>
  1174. </channel>
  1175. </rss>`
  1176. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1177. if err != nil {
  1178. t.Fatal(err)
  1179. }
  1180. if len(feed.Entries) != 1 {
  1181. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1182. }
  1183. expected := "Episode Subtitle"
  1184. result := feed.Entries[0].Content
  1185. if expected != result {
  1186. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1187. }
  1188. }
  1189. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1190. data := `<?xml version="1.0" encoding="UTF-8"?>
  1191. <rss version="2.0"
  1192. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1193. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1194. <channel>
  1195. <title>Podcast Example</title>
  1196. <link>http://www.example.com/index.html</link>
  1197. <item>
  1198. <title>Podcast Episode</title>
  1199. <guid>http://example.com/episode.m4a</guid>
  1200. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1201. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1202. <googleplay:description>Episode Description</googleplay:description>
  1203. </item>
  1204. </channel>
  1205. </rss>`
  1206. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1207. if err != nil {
  1208. t.Fatal(err)
  1209. }
  1210. if len(feed.Entries) != 1 {
  1211. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1212. }
  1213. expected := "Episode Description"
  1214. result := feed.Entries[0].Content
  1215. if expected != result {
  1216. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1217. }
  1218. }