parser_test.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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 TestParseEntryWithNonStandardAtomAuthor(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 xmlns:author="http://www.w3.org/2005/Atom">
  320. <name>Foo Bar</name>
  321. <title>Vice President</title>
  322. <department/>
  323. <company>FooBar Inc.</company>
  324. </author>
  325. </item>
  326. </channel>
  327. </rss>`
  328. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. expected := "Foo Bar"
  333. result := feed.Entries[0].Author
  334. if result != expected {
  335. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  336. }
  337. }
  338. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  339. data := `<?xml version="1.0" encoding="utf-8"?>
  340. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  341. <channel>
  342. <title>Example</title>
  343. <link>https://example.org/</link>
  344. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  345. <item>
  346. <title>Test</title>
  347. <link>https://example.org/item</link>
  348. <atom:author>
  349. <email>author@example.org</email>
  350. </atom: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 := "author@example.org"
  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 TestParseEntryWithAtomAuthor(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. <name>Foo Bar</name>
  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 := "Foo Bar"
  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 TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  391. data := `<?xml version="1.0" encoding="utf-8"?>
  392. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  393. <channel>
  394. <title>Example</title>
  395. <link>https://example.org/</link>
  396. <item>
  397. <title>Test</title>
  398. <link>https://example.org/item</link>
  399. <dc:creator>Me (me@example.com)</dc:creator>
  400. </item>
  401. </channel>
  402. </rss>`
  403. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  404. if err != nil {
  405. t.Fatal(err)
  406. }
  407. expected := "Me (me@example.com)"
  408. result := feed.Entries[0].Author
  409. if result != expected {
  410. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  411. }
  412. }
  413. func TestParseEntryWithItunesAuthor(t *testing.T) {
  414. data := `<?xml version="1.0" encoding="utf-8"?>
  415. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  416. <channel>
  417. <title>Example</title>
  418. <link>https://example.org/</link>
  419. <item>
  420. <title>Test</title>
  421. <link>https://example.org/item</link>
  422. <itunes:author>Someone</itunes:author>
  423. </item>
  424. </channel>
  425. </rss>`
  426. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  427. if err != nil {
  428. t.Fatal(err)
  429. }
  430. expected := "Someone"
  431. result := feed.Entries[0].Author
  432. if result != expected {
  433. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  434. }
  435. }
  436. func TestParseFeedWithItunesAuthor(t *testing.T) {
  437. data := `<?xml version="1.0" encoding="utf-8"?>
  438. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  439. <channel>
  440. <title>Example</title>
  441. <link>https://example.org/</link>
  442. <itunes:author>Someone</itunes:author>
  443. <item>
  444. <title>Test</title>
  445. <link>https://example.org/item</link>
  446. </item>
  447. </channel>
  448. </rss>`
  449. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. expected := "Someone"
  454. result := feed.Entries[0].Author
  455. if result != expected {
  456. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  457. }
  458. }
  459. func TestParseFeedWithItunesOwner(t *testing.T) {
  460. data := `<?xml version="1.0" encoding="utf-8"?>
  461. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  462. <channel>
  463. <title>Example</title>
  464. <link>https://example.org/</link>
  465. <itunes:owner>
  466. <itunes:name>John Doe</itunes:name>
  467. <itunes:email>john.doe@example.com</itunes:email>
  468. </itunes:owner>
  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 := "John Doe"
  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 TestParseFeedWithItunesOwnerEmail(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:email>john.doe@example.com</itunes:email>
  493. </itunes:owner>
  494. <item>
  495. <title>Test</title>
  496. <link>https://example.org/item</link>
  497. </item>
  498. </channel>
  499. </rss>`
  500. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  501. if err != nil {
  502. t.Fatal(err)
  503. }
  504. expected := "john.doe@example.com"
  505. result := feed.Entries[0].Author
  506. if result != expected {
  507. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  508. }
  509. }
  510. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  511. data := `<?xml version="1.0" encoding="utf-8"?>
  512. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  513. <channel>
  514. <title>Example</title>
  515. <link>https://example.org/</link>
  516. <item>
  517. <title>Test</title>
  518. <link>https://example.org/item</link>
  519. <googleplay:author>Someone</googleplay:author>
  520. </item>
  521. </channel>
  522. </rss>`
  523. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  524. if err != nil {
  525. t.Fatal(err)
  526. }
  527. expected := "Someone"
  528. result := feed.Entries[0].Author
  529. if result != expected {
  530. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  531. }
  532. }
  533. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  534. data := `<?xml version="1.0" encoding="utf-8"?>
  535. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  536. <channel>
  537. <title>Example</title>
  538. <link>https://example.org/</link>
  539. <googleplay:author>Someone</googleplay:author>
  540. <item>
  541. <title>Test</title>
  542. <link>https://example.org/item</link>
  543. </item>
  544. </channel>
  545. </rss>`
  546. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  547. if err != nil {
  548. t.Fatal(err)
  549. }
  550. expected := "Someone"
  551. result := feed.Entries[0].Author
  552. if result != expected {
  553. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  554. }
  555. }
  556. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  557. data := `<?xml version="1.0" encoding="utf-8"?>
  558. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  559. <channel>
  560. <title>Example</title>
  561. <link>http://example.org/</link>
  562. <item>
  563. <title>Item 1</title>
  564. <link>http://example.org/item1</link>
  565. <description>Description.</description>
  566. <guid isPermaLink="false">UUID</guid>
  567. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  568. </item>
  569. </channel>
  570. </rss>`
  571. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  572. if err != nil {
  573. t.Fatal(err)
  574. }
  575. location, _ := time.LoadLocation("EST")
  576. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  577. if !feed.Entries[0].Date.Equal(expectedDate) {
  578. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  579. }
  580. }
  581. func TestParseEntryWithContentEncoded(t *testing.T) {
  582. data := `<?xml version="1.0" encoding="utf-8"?>
  583. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  584. <channel>
  585. <title>Example</title>
  586. <link>http://example.org/</link>
  587. <item>
  588. <title>Item 1</title>
  589. <link>http://example.org/item1</link>
  590. <description>Description.</description>
  591. <guid isPermaLink="false">UUID</guid>
  592. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  593. </item>
  594. </channel>
  595. </rss>`
  596. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  597. if err != nil {
  598. t.Fatal(err)
  599. }
  600. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  601. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  602. }
  603. }
  604. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  605. data := `<?xml version="1.0" encoding="utf-8"?>
  606. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  607. <channel>
  608. <title>Example</title>
  609. <link>http://example.org/</link>
  610. <item>
  611. <title>Item 1</title>
  612. <link>http://example.org/item1</link>
  613. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  614. </item>
  615. </channel>
  616. </rss>`
  617. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  618. if err != nil {
  619. t.Fatal(err)
  620. }
  621. if feed.Entries[0].URL != "http://example.org/original" {
  622. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  623. }
  624. }
  625. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  626. data := `<?xml version="1.0" encoding="utf-8"?>
  627. <rss version="2.0">
  628. <channel>
  629. <title>Example</title>
  630. <link>http://example.org</link>
  631. <item>
  632. <title>
  633. Some Title
  634. </title>
  635. <link>http://www.example.org/entries/1</link>
  636. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  637. </item>
  638. </channel>
  639. </rss>`
  640. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  641. if err != nil {
  642. t.Fatal(err)
  643. }
  644. if feed.Entries[0].Title != "Some Title" {
  645. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  646. }
  647. }
  648. func TestParseEntryWithEnclosures(t *testing.T) {
  649. data := `<?xml version="1.0" encoding="utf-8"?>
  650. <rss version="2.0">
  651. <channel>
  652. <title>My Podcast Feed</title>
  653. <link>http://example.org</link>
  654. <author>some.email@example.org</author>
  655. <item>
  656. <title>Podcasting with RSS</title>
  657. <link>http://www.example.org/entries/1</link>
  658. <description>An overview of RSS podcasting</description>
  659. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  660. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  661. <enclosure url="http://www.example.org/myaudiofile.mp3"
  662. length="12345"
  663. type="audio/mpeg" />
  664. </item>
  665. </channel>
  666. </rss>`
  667. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  668. if err != nil {
  669. t.Fatal(err)
  670. }
  671. if len(feed.Entries) != 1 {
  672. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  673. }
  674. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  675. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  676. }
  677. if len(feed.Entries[0].Enclosures) != 1 {
  678. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  679. }
  680. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  681. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  682. }
  683. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  684. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  685. }
  686. if feed.Entries[0].Enclosures[0].Size != 12345 {
  687. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  688. }
  689. }
  690. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  691. data := `<?xml version="1.0" encoding="utf-8"?>
  692. <rss version="2.0">
  693. <channel>
  694. <title>My Podcast Feed</title>
  695. <link>http://example.org</link>
  696. <author>some.email@example.org</author>
  697. <item>
  698. <title>Podcasting with RSS</title>
  699. <link>http://www.example.org/entries/1</link>
  700. <description>An overview of RSS podcasting</description>
  701. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  702. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  703. <enclosure url="" length="0"/>
  704. </item>
  705. </channel>
  706. </rss>`
  707. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  708. if err != nil {
  709. t.Fatal(err)
  710. }
  711. if len(feed.Entries) != 1 {
  712. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  713. }
  714. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  715. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  716. }
  717. if len(feed.Entries[0].Enclosures) != 0 {
  718. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  719. }
  720. }
  721. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  722. data := `<?xml version="1.0" encoding="utf-8"?>
  723. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  724. <channel>
  725. <title>My Example Feed</title>
  726. <link>http://example.org</link>
  727. <author>some.email@example.org</author>
  728. <item>
  729. <title>Example Item</title>
  730. <link>http://www.example.org/entries/1</link>
  731. <enclosure
  732. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  733. length="76192460"
  734. type="audio/mpeg" />
  735. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  736. </item>
  737. </channel>
  738. </rss>`
  739. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  740. if err != nil {
  741. t.Fatal(err)
  742. }
  743. if len(feed.Entries) != 1 {
  744. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  745. }
  746. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  747. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  748. }
  749. if len(feed.Entries[0].Enclosures) != 1 {
  750. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  751. }
  752. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  753. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  754. }
  755. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  756. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  757. }
  758. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  759. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  760. }
  761. }
  762. func TestParseEntryWithRelativeURL(t *testing.T) {
  763. data := `<?xml version="1.0" encoding="utf-8"?>
  764. <rss version="2.0">
  765. <channel>
  766. <link>https://example.org/</link>
  767. <item>
  768. <link>item.html</link>
  769. </item>
  770. </channel>
  771. </rss>`
  772. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  773. if err != nil {
  774. t.Fatal(err)
  775. }
  776. if feed.Entries[0].Title != "https://example.org/item.html" {
  777. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  778. }
  779. }
  780. func TestParseEntryWithCommentsURL(t *testing.T) {
  781. data := `<?xml version="1.0" encoding="utf-8"?>
  782. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  783. <channel>
  784. <link>https://example.org/</link>
  785. <item>
  786. <title>Item 1</title>
  787. <link>https://example.org/item1</link>
  788. <comments>
  789. https://example.org/comments
  790. </comments>
  791. <slash:comments>42</slash:comments>
  792. </item>
  793. </channel>
  794. </rss>`
  795. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  796. if err != nil {
  797. t.Fatal(err)
  798. }
  799. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  800. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  801. }
  802. }
  803. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  804. data := `<?xml version="1.0" encoding="utf-8"?>
  805. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  806. <channel>
  807. <link>https://example.org/</link>
  808. <item>
  809. <title>Item 1</title>
  810. <link>https://example.org/item1</link>
  811. <comments>
  812. Some text
  813. </comments>
  814. </item>
  815. </channel>
  816. </rss>`
  817. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  818. if err != nil {
  819. t.Fatal(err)
  820. }
  821. if feed.Entries[0].CommentsURL != "" {
  822. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  823. }
  824. }
  825. func TestParseInvalidXml(t *testing.T) {
  826. data := `garbage`
  827. _, err := Parse("https://example.org/", bytes.NewBufferString(data))
  828. if err == nil {
  829. t.Error("Parse should returns an error")
  830. }
  831. }
  832. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  833. data := `<?xml version="1.0" encoding="utf-8"?>
  834. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  835. <channel>
  836. <link>https://example.org/</link>
  837. <title>Example &nbsp; Feed</title>
  838. </channel>
  839. </rss>`
  840. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  841. if err != nil {
  842. t.Fatal(err)
  843. }
  844. if feed.Title != "Example \u00a0 Feed" {
  845. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  846. }
  847. }
  848. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  849. data := `<?xml version="1.0" encoding="utf-8"?>
  850. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  851. <channel>
  852. <link>https://example.org/</link>
  853. <title>Example</title>
  854. <item>
  855. <title>&lt;/example&gt;</title>
  856. <link>http://www.example.org/entries/1</link>
  857. </item>
  858. </channel>
  859. </rss>`
  860. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  861. if err != nil {
  862. t.Fatal(err)
  863. }
  864. if feed.Entries[0].Title != "</example>" {
  865. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  866. }
  867. }
  868. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  869. data := `<?xml version="1.0" encoding="utf-8"?>
  870. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  871. <channel>
  872. <link>https://example.org/a&b</link>
  873. <title>Example Feed</title>
  874. </channel>
  875. </rss>`
  876. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  877. if err != nil {
  878. t.Fatal(err)
  879. }
  880. if feed.SiteURL != "https://example.org/a&b" {
  881. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  882. }
  883. }
  884. func TestParseEntryWithMediaGroup(t *testing.T) {
  885. data := `<?xml version="1.0" encoding="utf-8"?>
  886. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  887. <channel>
  888. <title>My Example Feed</title>
  889. <link>http://example.org</link>
  890. <item>
  891. <title>Example Item</title>
  892. <link>http://www.example.org/entries/1</link>
  893. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  894. </enclosure>
  895. <media:group>
  896. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  897. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  898. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  899. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  900. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  901. <media:rating>nonadult</media:rating>
  902. </media:group>
  903. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  904. </item>
  905. </channel>
  906. </rss>`
  907. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  908. if err != nil {
  909. t.Fatal(err)
  910. }
  911. if len(feed.Entries) != 1 {
  912. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  913. }
  914. if len(feed.Entries[0].Enclosures) != 6 {
  915. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  916. }
  917. expectedResults := []struct {
  918. url string
  919. mimeType string
  920. size int64
  921. }{
  922. {"https://example.org/image.jpg", "image/*", 0},
  923. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  924. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  925. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  926. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  927. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  928. }
  929. for index, enclosure := range feed.Entries[0].Enclosures {
  930. if expectedResults[index].url != enclosure.URL {
  931. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  932. }
  933. if expectedResults[index].mimeType != enclosure.MimeType {
  934. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  935. }
  936. if expectedResults[index].size != enclosure.Size {
  937. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  938. }
  939. }
  940. }
  941. func TestParseEntryWithMediaContent(t *testing.T) {
  942. data := `<?xml version="1.0" encoding="utf-8"?>
  943. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  944. <channel>
  945. <title>My Example Feed</title>
  946. <link>http://example.org</link>
  947. <item>
  948. <title>Example Item</title>
  949. <link>http://www.example.org/entries/1</link>
  950. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  951. <media:content url="https://example.org/media1.jpg" medium="image">
  952. <media:title type="html">Some Title for Media 1</media:title>
  953. </media:content>
  954. <media:content url="https://example.org/media2.jpg" medium="image" />
  955. </item>
  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 len(feed.Entries) != 1 {
  963. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  964. }
  965. if len(feed.Entries[0].Enclosures) != 3 {
  966. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  967. }
  968. expectedResults := []struct {
  969. url string
  970. mimeType string
  971. size int64
  972. }{
  973. {"https://example.org/thumbnail.jpg", "image/*", 0},
  974. {"https://example.org/media1.jpg", "image/*", 0},
  975. {"https://example.org/media2.jpg", "image/*", 0},
  976. }
  977. for index, enclosure := range feed.Entries[0].Enclosures {
  978. if expectedResults[index].url != enclosure.URL {
  979. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  980. }
  981. if expectedResults[index].mimeType != enclosure.MimeType {
  982. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  983. }
  984. if expectedResults[index].size != enclosure.Size {
  985. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  986. }
  987. }
  988. }
  989. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  990. data := `<?xml version="1.0" encoding="utf-8"?>
  991. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  992. <channel>
  993. <title>My Example Feed</title>
  994. <link>http://example.org</link>
  995. <item>
  996. <title>Example Item</title>
  997. <link>http://www.example.org/entries/1</link>
  998. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  999. </item>
  1000. </channel>
  1001. </rss>`
  1002. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1003. if err != nil {
  1004. t.Fatal(err)
  1005. }
  1006. if len(feed.Entries) != 1 {
  1007. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1008. }
  1009. if len(feed.Entries[0].Enclosures) != 1 {
  1010. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1011. }
  1012. expectedResults := []struct {
  1013. url string
  1014. mimeType string
  1015. size int64
  1016. }{
  1017. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1018. }
  1019. for index, enclosure := range feed.Entries[0].Enclosures {
  1020. if expectedResults[index].url != enclosure.URL {
  1021. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1022. }
  1023. if expectedResults[index].mimeType != enclosure.MimeType {
  1024. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1025. }
  1026. if expectedResults[index].size != enclosure.Size {
  1027. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1028. }
  1029. }
  1030. }
  1031. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1032. data := `<?xml version="1.0" encoding="UTF-8"?>
  1033. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1034. <channel>
  1035. <title>Podcast Example</title>
  1036. <link>http://www.example.com/index.html</link>
  1037. <item>
  1038. <title>Podcast Episode</title>
  1039. <guid>http://example.com/episode.m4a</guid>
  1040. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1041. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1042. <itunes:summary>Episode Summary</itunes:summary>
  1043. </item>
  1044. </channel>
  1045. </rss>`
  1046. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1047. if err != nil {
  1048. t.Fatal(err)
  1049. }
  1050. if len(feed.Entries) != 1 {
  1051. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1052. }
  1053. expected := "Episode Summary"
  1054. result := feed.Entries[0].Content
  1055. if expected != result {
  1056. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1057. }
  1058. }
  1059. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1060. data := `<?xml version="1.0" encoding="UTF-8"?>
  1061. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1062. <channel>
  1063. <title>Podcast Example</title>
  1064. <link>http://www.example.com/index.html</link>
  1065. <item>
  1066. <title>Podcast Episode</title>
  1067. <guid>http://example.com/episode.m4a</guid>
  1068. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1069. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1070. </item>
  1071. </channel>
  1072. </rss>`
  1073. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1074. if err != nil {
  1075. t.Fatal(err)
  1076. }
  1077. if len(feed.Entries) != 1 {
  1078. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1079. }
  1080. expected := "Episode Subtitle"
  1081. result := feed.Entries[0].Content
  1082. if expected != result {
  1083. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1084. }
  1085. }
  1086. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1087. data := `<?xml version="1.0" encoding="UTF-8"?>
  1088. <rss version="2.0"
  1089. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1090. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1091. <channel>
  1092. <title>Podcast Example</title>
  1093. <link>http://www.example.com/index.html</link>
  1094. <item>
  1095. <title>Podcast Episode</title>
  1096. <guid>http://example.com/episode.m4a</guid>
  1097. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1098. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1099. <googleplay:description>Episode Description</googleplay:description>
  1100. </item>
  1101. </channel>
  1102. </rss>`
  1103. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1104. if err != nil {
  1105. t.Fatal(err)
  1106. }
  1107. if len(feed.Entries) != 1 {
  1108. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1109. }
  1110. expected := "Episode Description"
  1111. result := feed.Entries[0].Content
  1112. if expected != result {
  1113. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1114. }
  1115. }