parser_test.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rss // import "miniflux.app/v2/internal/reader/rss"
  4. import (
  5. "bytes"
  6. "testing"
  7. "time"
  8. )
  9. func TestParseRss2Sample(t *testing.T) {
  10. data := `
  11. <?xml version="1.0"?>
  12. <rss version="2.0">
  13. <channel>
  14. <title>Liftoff News</title>
  15. <link>http://liftoff.msfc.nasa.gov/</link>
  16. <description>Liftoff to Space Exploration.</description>
  17. <image>
  18. <url>http://liftoff.msfc.nasa.gov/HomePageXtra/MeatBall.gif</url>
  19. <title>NASA</title>
  20. <link>http://liftoff.msfc.nasa.gov/</link>
  21. </image>
  22. <language>en-us</language>
  23. <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
  24. <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
  25. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  26. <generator>Weblog Editor 2.0</generator>
  27. <managingEditor>editor@example.com</managingEditor>
  28. <webMaster>webmaster@example.com</webMaster>
  29. <item>
  30. <title>Star City</title>
  31. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  32. <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>
  33. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  34. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  35. </item>
  36. <item>
  37. <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>
  38. <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
  39. <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
  40. </item>
  41. <item>
  42. <title>The Engine That Does More</title>
  43. <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
  44. <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>
  45. <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
  46. <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
  47. </item>
  48. <item>
  49. <title>Astronauts' Dirty Laundry</title>
  50. <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
  51. <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>
  52. <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
  53. <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
  54. </item>
  55. </channel>
  56. </rss>`
  57. feed, err := Parse("http://liftoff.msfc.nasa.gov/rss.xml", bytes.NewReader([]byte(data)))
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if feed.Title != "Liftoff News" {
  62. t.Errorf("Incorrect title, got: %s", feed.Title)
  63. }
  64. if feed.FeedURL != "http://liftoff.msfc.nasa.gov/rss.xml" {
  65. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  66. }
  67. if feed.SiteURL != "http://liftoff.msfc.nasa.gov/" {
  68. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  69. }
  70. if feed.IconURL != "http://liftoff.msfc.nasa.gov/HomePageXtra/MeatBall.gif" {
  71. t.Errorf("Incorrect image URL, got: %s", feed.IconURL)
  72. }
  73. if len(feed.Entries) != 4 {
  74. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  75. }
  76. expectedDate := time.Date(2003, time.June, 3, 9, 39, 21, 0, time.UTC)
  77. if !feed.Entries[0].Date.Equal(expectedDate) {
  78. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  79. }
  80. if feed.Entries[0].Hash != "5b2b4ac2fe1786ddf0fd2da2f1b07f64e691264f41f2db3ea360f31bb6d9152b" {
  81. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  82. }
  83. if feed.Entries[0].URL != "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp" {
  84. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  85. }
  86. if feed.Entries[0].Title != "Star City" {
  87. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  88. }
  89. 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>.` {
  90. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  91. }
  92. if feed.Entries[1].URL != "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572" {
  93. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  94. }
  95. }
  96. func TestParseFeedWithFeedURLWithTrailingSpace(t *testing.T) {
  97. data := `<?xml version="1.0"?>
  98. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  99. <channel>
  100. <title>Example</title>
  101. <link>https://example.org/</link>
  102. <atom:link href="https://example.org/rss " type="application/rss+xml" rel="self"></atom:link>
  103. <item>
  104. <title>Test</title>
  105. <link>https://example.org/item</link>
  106. </item>
  107. </channel>
  108. </rss>`
  109. feed, err := Parse("https://example.org/ ", bytes.NewReader([]byte(data)))
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. if feed.FeedURL != "https://example.org/rss" {
  114. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  115. }
  116. }
  117. func TestParseFeedWithRelativeFeedURL(t *testing.T) {
  118. data := `<?xml version="1.0"?>
  119. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  120. <channel>
  121. <title>Example</title>
  122. <link>https://example.org/</link>
  123. <atom:link href="/rss" type="application/rss+xml" rel="self"></atom:link>
  124. <item>
  125. <title>Test</title>
  126. <link>https://example.org/item</link>
  127. </item>
  128. </channel>
  129. </rss>`
  130. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if feed.FeedURL != "https://example.org/rss" {
  135. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  136. }
  137. }
  138. func TestParseFeedSiteURLWithTrailingSpace(t *testing.T) {
  139. data := `<?xml version="1.0"?>
  140. <rss version="2.0">
  141. <channel>
  142. <title>Example</title>
  143. <link>https://example.org/ </link>
  144. <item>
  145. <title>Test</title>
  146. <link>https://example.org/item</link>
  147. </item>
  148. </channel>
  149. </rss>`
  150. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. if feed.SiteURL != "https://example.org/" {
  155. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  156. }
  157. }
  158. func TestParseFeedWithRelativeSiteURL(t *testing.T) {
  159. data := `<?xml version="1.0"?>
  160. <rss version="2.0">
  161. <channel>
  162. <title>Example</title>
  163. <link>/example </link>
  164. <item>
  165. <title>Test</title>
  166. <link>https://example.org/item</link>
  167. </item>
  168. </channel>
  169. </rss>`
  170. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. if feed.SiteURL != "https://example.org/example" {
  175. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  176. }
  177. }
  178. func TestParseFeedWithoutTitle(t *testing.T) {
  179. data := `<?xml version="1.0" encoding="utf-8"?>
  180. <rss version="2.0">
  181. <channel>
  182. <link>https://example.org/</link>
  183. </channel>
  184. </rss>`
  185. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. if feed.Title != "https://example.org/" {
  190. t.Errorf("Incorrect feed title, got: %s", feed.Title)
  191. }
  192. }
  193. func TestParseEntryWithoutTitleAndDescription(t *testing.T) {
  194. data := `<?xml version="1.0" encoding="utf-8"?>
  195. <rss version="2.0">
  196. <channel>
  197. <link>https://example.org/</link>
  198. <item>
  199. <link>https://example.org/item</link>
  200. </item>
  201. </channel>
  202. </rss>`
  203. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. if feed.Entries[0].Title != "https://example.org/item" {
  208. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  209. }
  210. }
  211. func TestParseEntryWithoutTitleButWithDescription(t *testing.T) {
  212. data := `<?xml version="1.0" encoding="utf-8"?>
  213. <rss version="2.0">
  214. <channel>
  215. <link>https://example.org/</link>
  216. <item>
  217. <link>https://example.org/item</link>
  218. <description>
  219. This is the description
  220. </description>
  221. </item>
  222. </channel>
  223. </rss>`
  224. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. if feed.Entries[0].Title != "This is the description" {
  229. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  230. }
  231. }
  232. func TestParseEntryWithMediaTitle(t *testing.T) {
  233. data := `<?xml version="1.0" encoding="utf-8"?>
  234. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  235. <channel>
  236. <link>https://example.org/</link>
  237. <item>
  238. <title>Entry Title</title>
  239. <link>https://example.org/item</link>
  240. <media:title>Media Title</media:title>
  241. </item>
  242. </channel>
  243. </rss>`
  244. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. if feed.Entries[0].Title != "Entry Title" {
  249. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  250. }
  251. }
  252. func TestParseEntryWithDCTitleOnly(t *testing.T) {
  253. data := `<?xml version="1.0" encoding="utf-8"?>
  254. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  255. <channel>
  256. <link>https://example.org/</link>
  257. <item>
  258. <dc:title>Entry Title</dc:title>
  259. <link>https://example.org/item</link>
  260. </item>
  261. </channel>
  262. </rss>`
  263. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  264. if err != nil {
  265. t.Fatal(err)
  266. }
  267. if feed.Entries[0].Title != "Entry Title" {
  268. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  269. }
  270. }
  271. func TestParseEntryWithoutLink(t *testing.T) {
  272. data := `<?xml version="1.0" encoding="utf-8"?>
  273. <rss version="2.0">
  274. <channel>
  275. <link>https://example.org/</link>
  276. <item>
  277. <guid isPermaLink="false">1234</guid>
  278. </item>
  279. </channel>
  280. </rss>`
  281. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. if feed.Entries[0].URL != "https://example.org/" {
  286. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  287. }
  288. if feed.Entries[0].Hash != "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4" {
  289. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  290. }
  291. }
  292. func TestParseEntryWithOnlyGuidPermalink(t *testing.T) {
  293. data := `<?xml version="1.0" encoding="utf-8"?>
  294. <rss version="2.0">
  295. <channel>
  296. <link>https://example.org/</link>
  297. <item>
  298. <guid isPermaLink="true">https://example.org/some-article.html</guid>
  299. </item>
  300. <item>
  301. <guid>https://example.org/another-article.html</guid>
  302. </item>
  303. </channel>
  304. </rss>`
  305. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. if feed.Entries[0].URL != "https://example.org/some-article.html" {
  310. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  311. }
  312. if feed.Entries[1].URL != "https://example.org/another-article.html" {
  313. t.Errorf("Incorrect entry link, got: %s", feed.Entries[1].URL)
  314. }
  315. }
  316. func TestParseEntryWithAtomLink(t *testing.T) {
  317. data := `<?xml version="1.0" encoding="utf-8"?>
  318. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  319. <channel>
  320. <link>https://example.org/</link>
  321. <item>
  322. <title>Test</title>
  323. <atom:link href="https://example.org/item" />
  324. </item>
  325. </channel>
  326. </rss>`
  327. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  328. if err != nil {
  329. t.Fatal(err)
  330. }
  331. if feed.Entries[0].URL != "https://example.org/item" {
  332. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  333. }
  334. }
  335. func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
  336. data := `<?xml version="1.0" encoding="utf-8"?>
  337. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  338. <channel>
  339. <link>https://example.org/</link>
  340. <item>
  341. <title>Test</title>
  342. <atom:link rel="payment" href="https://example.org/a" />
  343. <atom:link rel="alternate" href="https://example.org/b" />
  344. </item>
  345. </channel>
  346. </rss>`
  347. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. if feed.Entries[0].URL != "https://example.org/b" {
  352. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  353. }
  354. }
  355. func TestParseFeedURLWithAtomLink(t *testing.T) {
  356. data := `<?xml version="1.0" encoding="utf-8"?>
  357. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  358. <channel>
  359. <title>Example</title>
  360. <link>https://example.org/</link>
  361. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  362. </channel>
  363. </rss>`
  364. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. if feed.FeedURL != "https://example.org/rss" {
  369. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  370. }
  371. if feed.SiteURL != "https://example.org/" {
  372. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  373. }
  374. }
  375. func TestParseFeedWithWebmaster(t *testing.T) {
  376. data := `<?xml version="1.0" encoding="utf-8"?>
  377. <rss version="2.0">
  378. <channel>
  379. <title>Example</title>
  380. <link>https://example.org/</link>
  381. <webMaster>webmaster@example.com</webMaster>
  382. <item>
  383. <title>Test</title>
  384. <link>https://example.org/item</link>
  385. </item>
  386. </channel>
  387. </rss>`
  388. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  389. if err != nil {
  390. t.Fatal(err)
  391. }
  392. expected := "webmaster@example.com"
  393. result := feed.Entries[0].Author
  394. if result != expected {
  395. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  396. }
  397. }
  398. func TestParseFeedWithManagingEditor(t *testing.T) {
  399. data := `<?xml version="1.0" encoding="utf-8"?>
  400. <rss version="2.0">
  401. <channel>
  402. <title>Example</title>
  403. <link>https://example.org/</link>
  404. <webMaster>webmaster@example.com</webMaster>
  405. <managingEditor>editor@example.com</managingEditor>
  406. <item>
  407. <title>Test</title>
  408. <link>https://example.org/item</link>
  409. </item>
  410. </channel>
  411. </rss>`
  412. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  413. if err != nil {
  414. t.Fatal(err)
  415. }
  416. expected := "editor@example.com"
  417. result := feed.Entries[0].Author
  418. if result != expected {
  419. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  420. }
  421. }
  422. func TestParseEntryWithAuthorAndInnerHTML(t *testing.T) {
  423. data := `<?xml version="1.0" encoding="utf-8"?>
  424. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  425. <channel>
  426. <title>Example</title>
  427. <link>https://example.org/</link>
  428. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  429. <item>
  430. <title>Test</title>
  431. <link>https://example.org/item</link>
  432. <author>by <a itemprop="url" class="author" rel="author" href="/author/foobar">Foo Bar</a></author>
  433. </item>
  434. </channel>
  435. </rss>`
  436. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. expected := "by Foo Bar"
  441. result := feed.Entries[0].Author
  442. if result != expected {
  443. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  444. }
  445. }
  446. func TestParseEntryWithAuthorAndCDATA(t *testing.T) {
  447. data := `<?xml version="1.0" encoding="utf-8"?>
  448. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  449. <channel>
  450. <title>Example</title>
  451. <link>https://example.org/</link>
  452. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  453. <item>
  454. <title>Test</title>
  455. <link>https://example.org/item</link>
  456. <author>
  457. <![CDATA[by Foo Bar]]>
  458. </author>
  459. </item>
  460. </channel>
  461. </rss>`
  462. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  463. if err != nil {
  464. t.Fatal(err)
  465. }
  466. expected := "by Foo Bar"
  467. result := feed.Entries[0].Author
  468. if result != expected {
  469. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  470. }
  471. }
  472. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  473. data := `<?xml version="1.0" encoding="utf-8"?>
  474. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  475. <channel>
  476. <title>Example</title>
  477. <link>https://example.org/</link>
  478. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  479. <item>
  480. <title>Test</title>
  481. <link>https://example.org/item</link>
  482. <atom:author>
  483. <email>author@example.org</email>
  484. </atom:author>
  485. </item>
  486. </channel>
  487. </rss>`
  488. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  489. if err != nil {
  490. t.Fatal(err)
  491. }
  492. expected := "author@example.org"
  493. result := feed.Entries[0].Author
  494. if result != expected {
  495. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  496. }
  497. }
  498. func TestParseEntryWithAtomAuthorName(t *testing.T) {
  499. data := `<?xml version="1.0" encoding="utf-8"?>
  500. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  501. <channel>
  502. <title>Example</title>
  503. <link>https://example.org/</link>
  504. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  505. <item>
  506. <title>Test</title>
  507. <link>https://example.org/item</link>
  508. <atom:author>
  509. <name>Foo Bar</name>
  510. </atom:author>
  511. </item>
  512. </channel>
  513. </rss>`
  514. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  515. if err != nil {
  516. t.Fatal(err)
  517. }
  518. expected := "Foo Bar"
  519. result := feed.Entries[0].Author
  520. if result != expected {
  521. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  522. }
  523. }
  524. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  525. data := `<?xml version="1.0" encoding="utf-8"?>
  526. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  527. <channel>
  528. <title>Example</title>
  529. <link>https://example.org/</link>
  530. <item>
  531. <title>Test</title>
  532. <link>https://example.org/item</link>
  533. <dc:creator>Me (me@example.com)</dc:creator>
  534. </item>
  535. </channel>
  536. </rss>`
  537. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  538. if err != nil {
  539. t.Fatal(err)
  540. }
  541. expected := "Me (me@example.com)"
  542. result := feed.Entries[0].Author
  543. if result != expected {
  544. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  545. }
  546. }
  547. func TestParseEntryWithItunesAuthor(t *testing.T) {
  548. data := `<?xml version="1.0" encoding="utf-8"?>
  549. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  550. <channel>
  551. <title>Example</title>
  552. <link>https://example.org/</link>
  553. <item>
  554. <title>Test</title>
  555. <link>https://example.org/item</link>
  556. <itunes:author>Someone</itunes:author>
  557. </item>
  558. </channel>
  559. </rss>`
  560. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  561. if err != nil {
  562. t.Fatal(err)
  563. }
  564. expected := "Someone"
  565. result := feed.Entries[0].Author
  566. if result != expected {
  567. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  568. }
  569. }
  570. func TestParseFeedWithItunesAuthor(t *testing.T) {
  571. data := `<?xml version="1.0" encoding="utf-8"?>
  572. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  573. <channel>
  574. <title>Example</title>
  575. <link>https://example.org/</link>
  576. <itunes:author>Someone</itunes:author>
  577. <item>
  578. <title>Test</title>
  579. <link>https://example.org/item</link>
  580. </item>
  581. </channel>
  582. </rss>`
  583. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  584. if err != nil {
  585. t.Fatal(err)
  586. }
  587. expected := "Someone"
  588. result := feed.Entries[0].Author
  589. if result != expected {
  590. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  591. }
  592. }
  593. func TestParseFeedWithItunesOwner(t *testing.T) {
  594. data := `<?xml version="1.0" encoding="utf-8"?>
  595. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  596. <channel>
  597. <title>Example</title>
  598. <link>https://example.org/</link>
  599. <itunes:owner>
  600. <itunes:name>John Doe</itunes:name>
  601. <itunes:email>john.doe@example.com</itunes:email>
  602. </itunes:owner>
  603. <item>
  604. <title>Test</title>
  605. <link>https://example.org/item</link>
  606. </item>
  607. </channel>
  608. </rss>`
  609. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  610. if err != nil {
  611. t.Fatal(err)
  612. }
  613. expected := "John Doe"
  614. result := feed.Entries[0].Author
  615. if result != expected {
  616. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  617. }
  618. }
  619. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  620. data := `<?xml version="1.0" encoding="utf-8"?>
  621. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  622. <channel>
  623. <title>Example</title>
  624. <link>https://example.org/</link>
  625. <itunes:owner>
  626. <itunes:email>john.doe@example.com</itunes:email>
  627. </itunes:owner>
  628. <item>
  629. <title>Test</title>
  630. <link>https://example.org/item</link>
  631. </item>
  632. </channel>
  633. </rss>`
  634. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  635. if err != nil {
  636. t.Fatal(err)
  637. }
  638. expected := "john.doe@example.com"
  639. result := feed.Entries[0].Author
  640. if result != expected {
  641. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  642. }
  643. }
  644. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  645. data := `<?xml version="1.0" encoding="utf-8"?>
  646. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  647. <channel>
  648. <title>Example</title>
  649. <link>https://example.org/</link>
  650. <item>
  651. <title>Test</title>
  652. <link>https://example.org/item</link>
  653. <googleplay:author>Someone</googleplay:author>
  654. </item>
  655. </channel>
  656. </rss>`
  657. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  658. if err != nil {
  659. t.Fatal(err)
  660. }
  661. expected := "Someone"
  662. result := feed.Entries[0].Author
  663. if result != expected {
  664. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  665. }
  666. }
  667. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  668. data := `<?xml version="1.0" encoding="utf-8"?>
  669. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  670. <channel>
  671. <title>Example</title>
  672. <link>https://example.org/</link>
  673. <googleplay:author>Someone</googleplay:author>
  674. <item>
  675. <title>Test</title>
  676. <link>https://example.org/item</link>
  677. </item>
  678. </channel>
  679. </rss>`
  680. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  681. if err != nil {
  682. t.Fatal(err)
  683. }
  684. expected := "Someone"
  685. result := feed.Entries[0].Author
  686. if result != expected {
  687. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  688. }
  689. }
  690. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  691. data := `<?xml version="1.0" encoding="utf-8"?>
  692. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  693. <channel>
  694. <title>Example</title>
  695. <link>http://example.org/</link>
  696. <item>
  697. <title>Item 1</title>
  698. <link>http://example.org/item1</link>
  699. <description>Description.</description>
  700. <guid isPermaLink="false">UUID</guid>
  701. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  702. </item>
  703. </channel>
  704. </rss>`
  705. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  706. if err != nil {
  707. t.Fatal(err)
  708. }
  709. location, _ := time.LoadLocation("EST")
  710. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  711. if !feed.Entries[0].Date.Equal(expectedDate) {
  712. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  713. }
  714. }
  715. func TestParseEntryWithContentEncoded(t *testing.T) {
  716. data := `<?xml version="1.0" encoding="utf-8"?>
  717. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  718. <channel>
  719. <title>Example</title>
  720. <link>http://example.org/</link>
  721. <item>
  722. <title>Item 1</title>
  723. <link>http://example.org/item1</link>
  724. <description>Description.</description>
  725. <guid isPermaLink="false">UUID</guid>
  726. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  727. </item>
  728. </channel>
  729. </rss>`
  730. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  731. if err != nil {
  732. t.Fatal(err)
  733. }
  734. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  735. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  736. }
  737. }
  738. // https://www.rssboard.org/rss-encoding-examples
  739. func TestParseEntryDescriptionWithEncodedHTMLTags(t *testing.T) {
  740. data := `<?xml version="1.0" encoding="utf-8"?>
  741. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  742. <channel>
  743. <title>Example</title>
  744. <link>http://example.org/</link>
  745. <item>
  746. <title>Item 1</title>
  747. <link>http://example.org/item1</link>
  748. <description>this is &lt;b&gt;bold&lt;/b&gt;</description>
  749. </item>
  750. </channel>
  751. </rss>`
  752. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  753. if err != nil {
  754. t.Fatal(err)
  755. }
  756. if feed.Entries[0].Content != `this is <b>bold</b>` {
  757. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  758. }
  759. }
  760. // https://www.rssboard.org/rss-encoding-examples
  761. func TestParseEntryWithDescriptionWithHTMLCDATA(t *testing.T) {
  762. data := `<?xml version="1.0" encoding="utf-8"?>
  763. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  764. <channel>
  765. <title>Example</title>
  766. <link>http://example.org/</link>
  767. <item>
  768. <title>Item 1</title>
  769. <link>http://example.org/item1</link>
  770. <description><![CDATA[this is <b>bold</b>]]></description>
  771. </item>
  772. </channel>
  773. </rss>`
  774. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  775. if err != nil {
  776. t.Fatal(err)
  777. }
  778. if feed.Entries[0].Content != `this is <b>bold</b>` {
  779. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  780. }
  781. }
  782. // https://www.rssboard.org/rss-encoding-examples
  783. func TestParseEntryDescriptionWithEncodingAngleBracketsInText(t *testing.T) {
  784. data := `<?xml version="1.0" encoding="utf-8"?>
  785. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  786. <channel>
  787. <title>Example</title>
  788. <link>http://example.org/</link>
  789. <item>
  790. <title>Item 1</title>
  791. <link>http://example.org/item1</link>
  792. <description>5 &amp;lt; 8, ticker symbol &amp;lt;BIGCO&amp;gt;</description>
  793. </item>
  794. </channel>
  795. </rss>`
  796. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  797. if err != nil {
  798. t.Fatal(err)
  799. }
  800. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  801. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  802. }
  803. }
  804. // https://www.rssboard.org/rss-encoding-examples
  805. func TestParseEntryDescriptionWithEncodingAngleBracketsWithinCDATASection(t *testing.T) {
  806. data := `<?xml version="1.0" encoding="utf-8"?>
  807. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  808. <channel>
  809. <title>Example</title>
  810. <link>http://example.org/</link>
  811. <item>
  812. <title>Item 1</title>
  813. <link>http://example.org/item1</link>
  814. <description><![CDATA[5 &lt; 8, ticker symbol &lt;BIGCO&gt;]]></description>
  815. </item>
  816. </channel>
  817. </rss>`
  818. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  819. if err != nil {
  820. t.Fatal(err)
  821. }
  822. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  823. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  824. }
  825. }
  826. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  827. data := `<?xml version="1.0" encoding="utf-8"?>
  828. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  829. <channel>
  830. <title>Example</title>
  831. <link>http://example.org/</link>
  832. <item>
  833. <title>Item 1</title>
  834. <link>http://example.org/item1</link>
  835. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  836. </item>
  837. </channel>
  838. </rss>`
  839. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  840. if err != nil {
  841. t.Fatal(err)
  842. }
  843. if feed.Entries[0].URL != "http://example.org/original" {
  844. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  845. }
  846. }
  847. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  848. data := `<?xml version="1.0" encoding="utf-8"?>
  849. <rss version="2.0">
  850. <channel>
  851. <title>Example</title>
  852. <link>http://example.org</link>
  853. <item>
  854. <title>
  855. Some Title
  856. </title>
  857. <link>http://www.example.org/entries/1</link>
  858. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  859. </item>
  860. </channel>
  861. </rss>`
  862. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  863. if err != nil {
  864. t.Fatal(err)
  865. }
  866. if feed.Entries[0].Title != "Some Title" {
  867. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  868. }
  869. }
  870. func TestParseEntryWithEnclosures(t *testing.T) {
  871. data := `<?xml version="1.0" encoding="utf-8"?>
  872. <rss version="2.0">
  873. <channel>
  874. <title>My Podcast Feed</title>
  875. <link>http://example.org</link>
  876. <author>some.email@example.org</author>
  877. <item>
  878. <title>Podcasting with RSS</title>
  879. <link>http://www.example.org/entries/1</link>
  880. <description>An overview of RSS podcasting</description>
  881. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  882. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  883. <enclosure url="http://www.example.org/myaudiofile.mp3"
  884. length="12345"
  885. type="audio/mpeg" />
  886. </item>
  887. </channel>
  888. </rss>`
  889. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  890. if err != nil {
  891. t.Fatal(err)
  892. }
  893. if len(feed.Entries) != 1 {
  894. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  895. }
  896. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  897. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  898. }
  899. if len(feed.Entries[0].Enclosures) != 1 {
  900. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  901. }
  902. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  903. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  904. }
  905. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  906. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  907. }
  908. if feed.Entries[0].Enclosures[0].Size != 12345 {
  909. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  910. }
  911. }
  912. func TestParseEntryWithIncorrectEnclosureLength(t *testing.T) {
  913. data := `<?xml version="1.0" encoding="utf-8"?>
  914. <rss version="2.0">
  915. <channel>
  916. <title>My Podcast Feed</title>
  917. <link>http://example.org</link>
  918. <author>some.email@example.org</author>
  919. <item>
  920. <title>Podcasting with RSS</title>
  921. <link>http://www.example.org/entries/1</link>
  922. <description>An overview of RSS podcasting</description>
  923. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  924. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  925. <enclosure url="http://www.example.org/myaudiofile.mp3" length="invalid" type="audio/mpeg" />
  926. <enclosure url="http://www.example.org/myaudiofile.wav" length=" " type="audio" />
  927. </item>
  928. </channel>
  929. </rss>`
  930. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  931. if err != nil {
  932. t.Fatal(err)
  933. }
  934. if len(feed.Entries) != 1 {
  935. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  936. }
  937. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  938. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  939. }
  940. if len(feed.Entries[0].Enclosures) != 2 {
  941. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  942. }
  943. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  944. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  945. }
  946. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  947. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  948. }
  949. if feed.Entries[0].Enclosures[0].Size != 0 {
  950. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  951. }
  952. if feed.Entries[0].Enclosures[1].Size != 0 {
  953. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  954. }
  955. }
  956. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  957. data := `<?xml version="1.0" encoding="utf-8"?>
  958. <rss version="2.0">
  959. <channel>
  960. <title>My Podcast Feed</title>
  961. <link>http://example.org</link>
  962. <author>some.email@example.org</author>
  963. <item>
  964. <title>Podcasting with RSS</title>
  965. <link>http://www.example.org/entries/1</link>
  966. <description>An overview of RSS podcasting</description>
  967. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  968. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  969. <enclosure url="" length="0"/>
  970. </item>
  971. </channel>
  972. </rss>`
  973. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  974. if err != nil {
  975. t.Fatal(err)
  976. }
  977. if len(feed.Entries) != 1 {
  978. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  979. }
  980. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  981. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  982. }
  983. if len(feed.Entries[0].Enclosures) != 0 {
  984. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  985. }
  986. }
  987. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  988. data := `<?xml version="1.0" encoding="utf-8"?>
  989. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  990. <channel>
  991. <title>My Example Feed</title>
  992. <link>http://example.org</link>
  993. <author>some.email@example.org</author>
  994. <item>
  995. <title>Example Item</title>
  996. <link>http://www.example.org/entries/1</link>
  997. <enclosure
  998. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  999. length="76192460"
  1000. type="audio/mpeg" />
  1001. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  1002. </item>
  1003. </channel>
  1004. </rss>`
  1005. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1006. if err != nil {
  1007. t.Fatal(err)
  1008. }
  1009. if len(feed.Entries) != 1 {
  1010. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1011. }
  1012. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  1013. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  1014. }
  1015. if len(feed.Entries[0].Enclosures) != 1 {
  1016. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1017. }
  1018. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  1019. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  1020. }
  1021. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  1022. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  1023. }
  1024. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  1025. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  1026. }
  1027. }
  1028. func TestParseEntryWithRelativeURL(t *testing.T) {
  1029. data := `<?xml version="1.0" encoding="utf-8"?>
  1030. <rss version="2.0">
  1031. <channel>
  1032. <link>https://example.org/</link>
  1033. <item>
  1034. <link>item.html</link>
  1035. </item>
  1036. </channel>
  1037. </rss>`
  1038. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1039. if err != nil {
  1040. t.Fatal(err)
  1041. }
  1042. if feed.Entries[0].Title != "https://example.org/item.html" {
  1043. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  1044. }
  1045. }
  1046. func TestParseEntryWithCommentsURL(t *testing.T) {
  1047. data := `<?xml version="1.0" encoding="utf-8"?>
  1048. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1049. <channel>
  1050. <link>https://example.org/</link>
  1051. <item>
  1052. <title>Item 1</title>
  1053. <link>https://example.org/item1</link>
  1054. <comments>
  1055. https://example.org/comments
  1056. </comments>
  1057. <slash:comments>42</slash:comments>
  1058. </item>
  1059. </channel>
  1060. </rss>`
  1061. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1062. if err != nil {
  1063. t.Fatal(err)
  1064. }
  1065. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  1066. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  1067. }
  1068. }
  1069. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  1070. data := `<?xml version="1.0" encoding="utf-8"?>
  1071. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1072. <channel>
  1073. <link>https://example.org/</link>
  1074. <item>
  1075. <title>Item 1</title>
  1076. <link>https://example.org/item1</link>
  1077. <comments>
  1078. Some text
  1079. </comments>
  1080. </item>
  1081. </channel>
  1082. </rss>`
  1083. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1084. if err != nil {
  1085. t.Fatal(err)
  1086. }
  1087. if feed.Entries[0].CommentsURL != "" {
  1088. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  1089. }
  1090. }
  1091. func TestParseInvalidXml(t *testing.T) {
  1092. data := `garbage`
  1093. _, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1094. if err == nil {
  1095. t.Error("Parse should returns an error")
  1096. }
  1097. }
  1098. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  1099. data := `<?xml version="1.0" encoding="utf-8"?>
  1100. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1101. <channel>
  1102. <link>https://example.org/</link>
  1103. <title>Example &nbsp; Feed</title>
  1104. </channel>
  1105. </rss>`
  1106. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1107. if err != nil {
  1108. t.Fatal(err)
  1109. }
  1110. if feed.Title != "Example \u00a0 Feed" {
  1111. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1112. }
  1113. }
  1114. func TestParseFeedTitleWithUnicodeEntityAndCdata(t *testing.T) {
  1115. data := `<?xml version="1.0" encoding="utf-8"?>
  1116. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1117. <channel>
  1118. <link>https://example.org/</link>
  1119. <title><![CDATA[Jenny&#8217;s Newsletter]]></title>
  1120. </channel>
  1121. </rss>`
  1122. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1123. if err != nil {
  1124. t.Fatal(err)
  1125. }
  1126. if feed.Title != `Jenny’s Newsletter` {
  1127. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1128. }
  1129. }
  1130. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  1131. data := `<?xml version="1.0" encoding="utf-8"?>
  1132. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1133. <channel>
  1134. <link>https://example.org/</link>
  1135. <title>Example</title>
  1136. <item>
  1137. <title>&lt;/example&gt;</title>
  1138. <link>http://www.example.org/entries/1</link>
  1139. </item>
  1140. </channel>
  1141. </rss>`
  1142. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1143. if err != nil {
  1144. t.Fatal(err)
  1145. }
  1146. if feed.Entries[0].Title != "</example>" {
  1147. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1148. }
  1149. }
  1150. func TestParseItemTitleWithNumericCharacterReference(t *testing.T) {
  1151. data := `<?xml version="1.0" encoding="utf-8"?>
  1152. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1153. <channel>
  1154. <link>https://example.org/</link>
  1155. <title>Example</title>
  1156. <item>
  1157. <title>&#931; &#xDF;</title>
  1158. <link>http://www.example.org/article.html</link>
  1159. </item>
  1160. </channel>
  1161. </rss>`
  1162. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1163. if err != nil {
  1164. t.Fatal(err)
  1165. }
  1166. if feed.Entries[0].Title != "Σ ß" {
  1167. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1168. }
  1169. }
  1170. func TestParseItemTitleWithDoubleEncodedEntities(t *testing.T) {
  1171. data := `<?xml version="1.0" encoding="utf-8"?>
  1172. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1173. <channel>
  1174. <link>https://example.org/</link>
  1175. <title>Example</title>
  1176. <item>
  1177. <title>&amp;#39;Text&amp;#39;</title>
  1178. <link>http://www.example.org/article.html</link>
  1179. </item>
  1180. </channel>
  1181. </rss>`
  1182. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1183. if err != nil {
  1184. t.Fatal(err)
  1185. }
  1186. if feed.Entries[0].Title != "'Text'" {
  1187. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1188. }
  1189. }
  1190. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  1191. data := `<?xml version="1.0" encoding="utf-8"?>
  1192. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1193. <channel>
  1194. <link>https://example.org/a&b</link>
  1195. <title>Example Feed</title>
  1196. </channel>
  1197. </rss>`
  1198. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1199. if err != nil {
  1200. t.Fatal(err)
  1201. }
  1202. if feed.SiteURL != "https://example.org/a&b" {
  1203. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  1204. }
  1205. }
  1206. func TestParseEntryWithMediaGroup(t *testing.T) {
  1207. data := `<?xml version="1.0" encoding="utf-8"?>
  1208. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1209. <channel>
  1210. <title>My Example Feed</title>
  1211. <link>http://example.org</link>
  1212. <item>
  1213. <title>Example Item</title>
  1214. <link>http://www.example.org/entries/1</link>
  1215. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  1216. </enclosure>
  1217. <media:group>
  1218. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  1219. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  1220. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  1221. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1222. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  1223. <media:rating>nonadult</media:rating>
  1224. </media:group>
  1225. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  1226. </item>
  1227. </channel>
  1228. </rss>`
  1229. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1230. if err != nil {
  1231. t.Fatal(err)
  1232. }
  1233. if len(feed.Entries) != 1 {
  1234. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1235. }
  1236. if len(feed.Entries[0].Enclosures) != 6 {
  1237. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1238. }
  1239. expectedResults := []struct {
  1240. url string
  1241. mimeType string
  1242. size int64
  1243. }{
  1244. {"https://example.org/image.jpg", "image/*", 0},
  1245. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  1246. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  1247. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  1248. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  1249. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  1250. }
  1251. for index, enclosure := range feed.Entries[0].Enclosures {
  1252. if expectedResults[index].url != enclosure.URL {
  1253. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1254. }
  1255. if expectedResults[index].mimeType != enclosure.MimeType {
  1256. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1257. }
  1258. if expectedResults[index].size != enclosure.Size {
  1259. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1260. }
  1261. }
  1262. }
  1263. func TestParseEntryWithMediaContent(t *testing.T) {
  1264. data := `<?xml version="1.0" encoding="utf-8"?>
  1265. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1266. <channel>
  1267. <title>My Example Feed</title>
  1268. <link>http://example.org</link>
  1269. <item>
  1270. <title>Example Item</title>
  1271. <link>http://www.example.org/entries/1</link>
  1272. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1273. <media:content url="https://example.org/media1.jpg" medium="image">
  1274. <media:title type="html">Some Title for Media 1</media:title>
  1275. </media:content>
  1276. <media:content url="https://example.org/media2.jpg" medium="image" />
  1277. </item>
  1278. </channel>
  1279. </rss>`
  1280. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1281. if err != nil {
  1282. t.Fatal(err)
  1283. }
  1284. if len(feed.Entries) != 1 {
  1285. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1286. }
  1287. if len(feed.Entries[0].Enclosures) != 3 {
  1288. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1289. }
  1290. expectedResults := []struct {
  1291. url string
  1292. mimeType string
  1293. size int64
  1294. }{
  1295. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1296. {"https://example.org/media1.jpg", "image/*", 0},
  1297. {"https://example.org/media2.jpg", "image/*", 0},
  1298. }
  1299. for index, enclosure := range feed.Entries[0].Enclosures {
  1300. if expectedResults[index].url != enclosure.URL {
  1301. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1302. }
  1303. if expectedResults[index].mimeType != enclosure.MimeType {
  1304. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1305. }
  1306. if expectedResults[index].size != enclosure.Size {
  1307. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1308. }
  1309. }
  1310. }
  1311. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  1312. data := `<?xml version="1.0" encoding="utf-8"?>
  1313. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1314. <channel>
  1315. <title>My Example Feed</title>
  1316. <link>http://example.org</link>
  1317. <item>
  1318. <title>Example Item</title>
  1319. <link>http://www.example.org/entries/1</link>
  1320. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  1321. </item>
  1322. </channel>
  1323. </rss>`
  1324. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1325. if err != nil {
  1326. t.Fatal(err)
  1327. }
  1328. if len(feed.Entries) != 1 {
  1329. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1330. }
  1331. if len(feed.Entries[0].Enclosures) != 1 {
  1332. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1333. }
  1334. expectedResults := []struct {
  1335. url string
  1336. mimeType string
  1337. size int64
  1338. }{
  1339. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1340. }
  1341. for index, enclosure := range feed.Entries[0].Enclosures {
  1342. if expectedResults[index].url != enclosure.URL {
  1343. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1344. }
  1345. if expectedResults[index].mimeType != enclosure.MimeType {
  1346. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1347. }
  1348. if expectedResults[index].size != enclosure.Size {
  1349. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1350. }
  1351. }
  1352. }
  1353. func TestParseItunesDuration(t *testing.T) {
  1354. data := `<?xml version="1.0" encoding="UTF-8"?>
  1355. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1356. <channel>
  1357. <title>Podcast Example</title>
  1358. <link>http://www.example.com/index.html</link>
  1359. <item>
  1360. <title>Podcast Episode</title>
  1361. <guid>http://example.com/episode.m4a</guid>
  1362. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1363. <itunes:duration>1:23:45</itunes:duration>
  1364. </item>
  1365. </channel>
  1366. </rss>`
  1367. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1368. if err != nil {
  1369. t.Fatal(err)
  1370. }
  1371. expected := 83
  1372. result := feed.Entries[0].ReadingTime
  1373. if expected != result {
  1374. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1375. }
  1376. }
  1377. func TestParseIncorrectItunesDuration(t *testing.T) {
  1378. data := `<?xml version="1.0" encoding="UTF-8"?>
  1379. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1380. <channel>
  1381. <title>Podcast Example</title>
  1382. <link>http://www.example.com/index.html</link>
  1383. <item>
  1384. <title>Podcast Episode</title>
  1385. <guid>http://example.com/episode.m4a</guid>
  1386. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1387. <itunes:duration>invalid</itunes:duration>
  1388. </item>
  1389. </channel>
  1390. </rss>`
  1391. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1392. if err != nil {
  1393. t.Fatal(err)
  1394. }
  1395. expected := 0
  1396. result := feed.Entries[0].ReadingTime
  1397. if expected != result {
  1398. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1399. }
  1400. }
  1401. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1402. data := `<?xml version="1.0" encoding="UTF-8"?>
  1403. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1404. <channel>
  1405. <title>Podcast Example</title>
  1406. <link>http://www.example.com/index.html</link>
  1407. <item>
  1408. <title>Podcast Episode</title>
  1409. <guid>http://example.com/episode.m4a</guid>
  1410. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1411. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1412. <itunes:summary>Episode Summary</itunes:summary>
  1413. </item>
  1414. </channel>
  1415. </rss>`
  1416. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1417. if err != nil {
  1418. t.Fatal(err)
  1419. }
  1420. if len(feed.Entries) != 1 {
  1421. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1422. }
  1423. expected := "Episode Summary"
  1424. result := feed.Entries[0].Content
  1425. if expected != result {
  1426. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1427. }
  1428. }
  1429. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1430. data := `<?xml version="1.0" encoding="UTF-8"?>
  1431. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1432. <channel>
  1433. <title>Podcast Example</title>
  1434. <link>http://www.example.com/index.html</link>
  1435. <item>
  1436. <title>Podcast Episode</title>
  1437. <guid>http://example.com/episode.m4a</guid>
  1438. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1439. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1440. </item>
  1441. </channel>
  1442. </rss>`
  1443. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1444. if err != nil {
  1445. t.Fatal(err)
  1446. }
  1447. if len(feed.Entries) != 1 {
  1448. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1449. }
  1450. expected := "Episode Subtitle"
  1451. result := feed.Entries[0].Content
  1452. if expected != result {
  1453. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1454. }
  1455. }
  1456. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1457. data := `<?xml version="1.0" encoding="UTF-8"?>
  1458. <rss version="2.0"
  1459. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1460. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1461. <channel>
  1462. <title>Podcast Example</title>
  1463. <link>http://www.example.com/index.html</link>
  1464. <item>
  1465. <title>Podcast Episode</title>
  1466. <guid>http://example.com/episode.m4a</guid>
  1467. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1468. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1469. <googleplay:description>Episode Description</googleplay:description>
  1470. </item>
  1471. </channel>
  1472. </rss>`
  1473. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1474. if err != nil {
  1475. t.Fatal(err)
  1476. }
  1477. if len(feed.Entries) != 1 {
  1478. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1479. }
  1480. expected := "Episode Description"
  1481. result := feed.Entries[0].Content
  1482. if expected != result {
  1483. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1484. }
  1485. }
  1486. func TestParseEntryWithRSSDescriptionAndMediaDescription(t *testing.T) {
  1487. data := `<?xml version="1.0" encoding="UTF-8"?>
  1488. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1489. <channel>
  1490. <title>Podcast Example</title>
  1491. <link>http://www.example.com/index.html</link>
  1492. <item>
  1493. <title>Entry Title</title>
  1494. <link>http://www.example.com/entries/1</link>
  1495. <description>Entry Description</description>
  1496. <media:description type="plain">Media Description</media:description>
  1497. </item>
  1498. </channel>
  1499. </rss>`
  1500. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1501. if err != nil {
  1502. t.Fatal(err)
  1503. }
  1504. if len(feed.Entries) != 1 {
  1505. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1506. }
  1507. expected := "Entry Description"
  1508. result := feed.Entries[0].Content
  1509. if expected != result {
  1510. t.Errorf(`Unexpected description, got %q instead of %q`, result, expected)
  1511. }
  1512. }
  1513. func TestParseFeedWithCategories(t *testing.T) {
  1514. data := `<?xml version="1.0" encoding="utf-8"?>
  1515. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1516. <channel>
  1517. <title>Example</title>
  1518. <link>https://example.org/</link>
  1519. <category>Category 1</category>
  1520. <category><![CDATA[Category 2]]></category>
  1521. <item>
  1522. <title>Test</title>
  1523. <link>https://example.org/item</link>
  1524. </item>
  1525. </channel>
  1526. </rss>`
  1527. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1528. if err != nil {
  1529. t.Fatal(err)
  1530. }
  1531. if len(feed.Entries[0].Tags) != 2 {
  1532. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1533. }
  1534. expected := []string{"Category 1", "Category 2"}
  1535. result := feed.Entries[0].Tags
  1536. for i, tag := range result {
  1537. if tag != expected[i] {
  1538. t.Errorf("Incorrect tag, got: %q", tag)
  1539. }
  1540. }
  1541. }
  1542. func TestParseEntryWithCategories(t *testing.T) {
  1543. data := `<?xml version="1.0" encoding="utf-8"?>
  1544. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1545. <channel>
  1546. <title>Example</title>
  1547. <link>https://example.org/</link>
  1548. <category>Category 3</category>
  1549. <item>
  1550. <title>Test</title>
  1551. <link>https://example.org/item</link>
  1552. <category>Category 1</category>
  1553. <category><![CDATA[Category 2]]></category>
  1554. </item>
  1555. </channel>
  1556. </rss>`
  1557. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1558. if err != nil {
  1559. t.Fatal(err)
  1560. }
  1561. if len(feed.Entries[0].Tags) != 3 {
  1562. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1563. }
  1564. expected := []string{"Category 1", "Category 2", "Category 3"}
  1565. result := feed.Entries[0].Tags
  1566. for i, tag := range result {
  1567. if tag != expected[i] {
  1568. t.Errorf("Incorrect tag, got: %q", tag)
  1569. }
  1570. }
  1571. }
  1572. func TestParseFeedWithItunesCategories(t *testing.T) {
  1573. data := `<?xml version="1.0" encoding="utf-8"?>
  1574. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  1575. <channel>
  1576. <title>Example</title>
  1577. <link>https://example.org/</link>
  1578. <itunes:category text="Society &amp; Culture">
  1579. <itunes:category text="Documentary" />
  1580. </itunes:category>
  1581. <itunes:category text="Health">
  1582. <itunes:category text="Mental Health" />
  1583. </itunes:category>
  1584. <item>
  1585. <title>Test</title>
  1586. <link>https://example.org/item</link>
  1587. </item>
  1588. </channel>
  1589. </rss>`
  1590. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1591. if err != nil {
  1592. t.Fatal(err)
  1593. }
  1594. if len(feed.Entries[0].Tags) != 4 {
  1595. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1596. }
  1597. expected := []string{"Society & Culture", "Documentary", "Health", "Mental Health"}
  1598. result := feed.Entries[0].Tags
  1599. for i, tag := range result {
  1600. if tag != expected[i] {
  1601. t.Errorf("Incorrect tag, got: %q", tag)
  1602. }
  1603. }
  1604. }
  1605. func TestParseFeedWithGooglePlayCategory(t *testing.T) {
  1606. data := `<?xml version="1.0" encoding="utf-8"?>
  1607. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0">
  1608. <channel>
  1609. <title>Example</title>
  1610. <link>https://example.org/</link>
  1611. <gplay:category text="Art"></gplay:category>
  1612. <item>
  1613. <title>Test</title>
  1614. <link>https://example.org/item</link>
  1615. </item>
  1616. </channel>
  1617. </rss>`
  1618. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1619. if err != nil {
  1620. t.Fatal(err)
  1621. }
  1622. if len(feed.Entries[0].Tags) != 1 {
  1623. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1624. }
  1625. expected := []string{"Art"}
  1626. result := feed.Entries[0].Tags
  1627. for i, tag := range result {
  1628. if tag != expected[i] {
  1629. t.Errorf("Incorrect tag, got: %q", tag)
  1630. }
  1631. }
  1632. }
  1633. func TestParseEntryWithMediaCategories(t *testing.T) {
  1634. data := `<?xml version="1.0" encoding="utf-8"?>
  1635. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  1636. <channel>
  1637. <title>Example</title>
  1638. <link>https://example.org/</link>
  1639. <item>
  1640. <title>Test</title>
  1641. <link>https://example.org/item</link>
  1642. <media:category label="Visual Art">visual_art</media:category>
  1643. <media:category scheme="http://search.yahoo.com/mrss/category_ schema">music/artist/album/song</media:category>
  1644. <media:category scheme="urn:flickr:tags">ycantpark mobile</media:category>
  1645. <media:category scheme="http://dmoz.org" label="Ace Ventura - Pet Detective">Arts/Movies/Titles/A/Ace_Ventura_Series/Ace_Ventura_ -_Pet_Detective</media:category>
  1646. </item>
  1647. </channel>
  1648. </rss>`
  1649. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1650. if err != nil {
  1651. t.Fatal(err)
  1652. }
  1653. if len(feed.Entries[0].Tags) != 2 {
  1654. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1655. }
  1656. expected := []string{"Visual Art", "Ace Ventura - Pet Detective"}
  1657. result := feed.Entries[0].Tags
  1658. for i, tag := range result {
  1659. if tag != expected[i] {
  1660. t.Errorf("Incorrect tag, got: %q", tag)
  1661. }
  1662. }
  1663. }
  1664. func TestParseFeedWithTTLField(t *testing.T) {
  1665. data := `<?xml version="1.0" encoding="utf-8"?>
  1666. <rss version="2.0">
  1667. <channel>
  1668. <title>Example</title>
  1669. <link>https://example.org/</link>
  1670. <ttl>60</ttl>
  1671. <item>
  1672. <title>Test</title>
  1673. <link>https://example.org/item</link>
  1674. </item>
  1675. </channel>
  1676. </rss>`
  1677. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1678. if err != nil {
  1679. t.Fatal(err)
  1680. }
  1681. if feed.TTL != 60 {
  1682. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1683. }
  1684. }
  1685. func TestParseFeedWithIncorrectTTLValue(t *testing.T) {
  1686. data := `<?xml version="1.0" encoding="utf-8"?>
  1687. <rss version="2.0">
  1688. <channel>
  1689. <title>Example</title>
  1690. <link>https://example.org/</link>
  1691. <ttl>invalid</ttl>
  1692. <item>
  1693. <title>Test</title>
  1694. <link>https://example.org/item</link>
  1695. </item>
  1696. </channel>
  1697. </rss>`
  1698. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1699. if err != nil {
  1700. t.Fatal(err)
  1701. }
  1702. if feed.TTL != 0 {
  1703. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1704. }
  1705. }