parser_test.go 40 KB

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