parser_test.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  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 TestParseEntryWithoutLinkAndWithoutGUID(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. <title>Item 1</title>
  299. </item>
  300. <item>
  301. <title>Item 2</title>
  302. <pubDate>Wed, 02 Oct 2002 08:00:00 GMT</pubDate>
  303. </item>
  304. </channel>
  305. </rss>`
  306. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. if len(feed.Entries) != 2 {
  311. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  312. }
  313. if feed.Entries[0].Hash != "c5ddfeffb275254140796b8c080f372d65ebb1b0590e238b191f595d5fcd32ca" {
  314. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  315. }
  316. if feed.Entries[1].Hash != "0a937478f9bdbfca2de5cdeeb5ee7b09678a3330fc7cc5b05169a50d4516c9a3" {
  317. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[1].Hash)
  318. }
  319. }
  320. func TestParseEntryWithOnlyGuidPermalink(t *testing.T) {
  321. data := `<?xml version="1.0" encoding="utf-8"?>
  322. <rss version="2.0">
  323. <channel>
  324. <link>https://example.org/</link>
  325. <item>
  326. <guid isPermaLink="true">https://example.org/some-article.html</guid>
  327. </item>
  328. <item>
  329. <guid>https://example.org/another-article.html</guid>
  330. </item>
  331. </channel>
  332. </rss>`
  333. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. if feed.Entries[0].URL != "https://example.org/some-article.html" {
  338. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  339. }
  340. if feed.Entries[1].URL != "https://example.org/another-article.html" {
  341. t.Errorf("Incorrect entry link, got: %s", feed.Entries[1].URL)
  342. }
  343. }
  344. func TestParseEntryWithAtomLink(t *testing.T) {
  345. data := `<?xml version="1.0" encoding="utf-8"?>
  346. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  347. <channel>
  348. <link>https://example.org/</link>
  349. <item>
  350. <title>Test</title>
  351. <atom:link href="https://example.org/item" />
  352. </item>
  353. </channel>
  354. </rss>`
  355. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. if feed.Entries[0].URL != "https://example.org/item" {
  360. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  361. }
  362. }
  363. func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
  364. data := `<?xml version="1.0" encoding="utf-8"?>
  365. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  366. <channel>
  367. <link>https://example.org/</link>
  368. <item>
  369. <title>Test</title>
  370. <atom:link rel="payment" href="https://example.org/a" />
  371. <atom:link rel="alternate" href="https://example.org/b" />
  372. </item>
  373. </channel>
  374. </rss>`
  375. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. if feed.Entries[0].URL != "https://example.org/b" {
  380. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  381. }
  382. }
  383. func TestParseFeedURLWithAtomLink(t *testing.T) {
  384. data := `<?xml version="1.0" encoding="utf-8"?>
  385. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  386. <channel>
  387. <title>Example</title>
  388. <link>https://example.org/</link>
  389. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  390. </channel>
  391. </rss>`
  392. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  393. if err != nil {
  394. t.Fatal(err)
  395. }
  396. if feed.FeedURL != "https://example.org/rss" {
  397. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  398. }
  399. if feed.SiteURL != "https://example.org/" {
  400. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  401. }
  402. }
  403. func TestParseFeedWithWebmaster(t *testing.T) {
  404. data := `<?xml version="1.0" encoding="utf-8"?>
  405. <rss version="2.0">
  406. <channel>
  407. <title>Example</title>
  408. <link>https://example.org/</link>
  409. <webMaster>webmaster@example.com</webMaster>
  410. <item>
  411. <title>Test</title>
  412. <link>https://example.org/item</link>
  413. </item>
  414. </channel>
  415. </rss>`
  416. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. expected := "webmaster@example.com"
  421. result := feed.Entries[0].Author
  422. if result != expected {
  423. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  424. }
  425. }
  426. func TestParseFeedWithManagingEditor(t *testing.T) {
  427. data := `<?xml version="1.0" encoding="utf-8"?>
  428. <rss version="2.0">
  429. <channel>
  430. <title>Example</title>
  431. <link>https://example.org/</link>
  432. <webMaster>webmaster@example.com</webMaster>
  433. <managingEditor>editor@example.com</managingEditor>
  434. <item>
  435. <title>Test</title>
  436. <link>https://example.org/item</link>
  437. </item>
  438. </channel>
  439. </rss>`
  440. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  441. if err != nil {
  442. t.Fatal(err)
  443. }
  444. expected := "editor@example.com"
  445. result := feed.Entries[0].Author
  446. if result != expected {
  447. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  448. }
  449. }
  450. func TestParseEntryWithAuthorAndInnerHTML(t *testing.T) {
  451. data := `<?xml version="1.0" encoding="utf-8"?>
  452. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  453. <channel>
  454. <title>Example</title>
  455. <link>https://example.org/</link>
  456. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  457. <item>
  458. <title>Test</title>
  459. <link>https://example.org/item</link>
  460. <author>by <a itemprop="url" class="author" rel="author" href="/author/foobar">Foo Bar</a></author>
  461. </item>
  462. </channel>
  463. </rss>`
  464. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  465. if err != nil {
  466. t.Fatal(err)
  467. }
  468. expected := "by Foo Bar"
  469. result := feed.Entries[0].Author
  470. if result != expected {
  471. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  472. }
  473. }
  474. func TestParseEntryWithAuthorAndCDATA(t *testing.T) {
  475. data := `<?xml version="1.0" encoding="utf-8"?>
  476. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  477. <channel>
  478. <title>Example</title>
  479. <link>https://example.org/</link>
  480. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  481. <item>
  482. <title>Test</title>
  483. <link>https://example.org/item</link>
  484. <author>
  485. <![CDATA[by Foo Bar]]>
  486. </author>
  487. </item>
  488. </channel>
  489. </rss>`
  490. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. expected := "by Foo Bar"
  495. result := feed.Entries[0].Author
  496. if result != expected {
  497. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  498. }
  499. }
  500. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  501. data := `<?xml version="1.0" encoding="utf-8"?>
  502. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  503. <channel>
  504. <title>Example</title>
  505. <link>https://example.org/</link>
  506. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  507. <item>
  508. <title>Test</title>
  509. <link>https://example.org/item</link>
  510. <atom:author>
  511. <email>author@example.org</email>
  512. </atom:author>
  513. </item>
  514. </channel>
  515. </rss>`
  516. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  517. if err != nil {
  518. t.Fatal(err)
  519. }
  520. expected := "author@example.org"
  521. result := feed.Entries[0].Author
  522. if result != expected {
  523. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  524. }
  525. }
  526. func TestParseEntryWithAtomAuthorName(t *testing.T) {
  527. data := `<?xml version="1.0" encoding="utf-8"?>
  528. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  529. <channel>
  530. <title>Example</title>
  531. <link>https://example.org/</link>
  532. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  533. <item>
  534. <title>Test</title>
  535. <link>https://example.org/item</link>
  536. <atom:author>
  537. <name>Foo Bar</name>
  538. </atom:author>
  539. </item>
  540. </channel>
  541. </rss>`
  542. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  543. if err != nil {
  544. t.Fatal(err)
  545. }
  546. expected := "Foo Bar"
  547. result := feed.Entries[0].Author
  548. if result != expected {
  549. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  550. }
  551. }
  552. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  553. data := `<?xml version="1.0" encoding="utf-8"?>
  554. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  555. <channel>
  556. <title>Example</title>
  557. <link>https://example.org/</link>
  558. <item>
  559. <title>Test</title>
  560. <link>https://example.org/item</link>
  561. <dc:creator>Me (me@example.com)</dc:creator>
  562. </item>
  563. </channel>
  564. </rss>`
  565. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. expected := "Me (me@example.com)"
  570. result := feed.Entries[0].Author
  571. if result != expected {
  572. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  573. }
  574. }
  575. func TestParseEntryWithItunesAuthor(t *testing.T) {
  576. data := `<?xml version="1.0" encoding="utf-8"?>
  577. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  578. <channel>
  579. <title>Example</title>
  580. <link>https://example.org/</link>
  581. <item>
  582. <title>Test</title>
  583. <link>https://example.org/item</link>
  584. <itunes:author>Someone</itunes:author>
  585. </item>
  586. </channel>
  587. </rss>`
  588. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  589. if err != nil {
  590. t.Fatal(err)
  591. }
  592. expected := "Someone"
  593. result := feed.Entries[0].Author
  594. if result != expected {
  595. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  596. }
  597. }
  598. func TestParseFeedWithItunesAuthor(t *testing.T) {
  599. data := `<?xml version="1.0" encoding="utf-8"?>
  600. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  601. <channel>
  602. <title>Example</title>
  603. <link>https://example.org/</link>
  604. <itunes:author>Someone</itunes:author>
  605. <item>
  606. <title>Test</title>
  607. <link>https://example.org/item</link>
  608. </item>
  609. </channel>
  610. </rss>`
  611. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  612. if err != nil {
  613. t.Fatal(err)
  614. }
  615. expected := "Someone"
  616. result := feed.Entries[0].Author
  617. if result != expected {
  618. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  619. }
  620. }
  621. func TestParseFeedWithItunesOwner(t *testing.T) {
  622. data := `<?xml version="1.0" encoding="utf-8"?>
  623. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  624. <channel>
  625. <title>Example</title>
  626. <link>https://example.org/</link>
  627. <itunes:owner>
  628. <itunes:name>John Doe</itunes:name>
  629. <itunes:email>john.doe@example.com</itunes:email>
  630. </itunes:owner>
  631. <item>
  632. <title>Test</title>
  633. <link>https://example.org/item</link>
  634. </item>
  635. </channel>
  636. </rss>`
  637. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  638. if err != nil {
  639. t.Fatal(err)
  640. }
  641. expected := "John Doe"
  642. result := feed.Entries[0].Author
  643. if result != expected {
  644. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  645. }
  646. }
  647. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  648. data := `<?xml version="1.0" encoding="utf-8"?>
  649. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  650. <channel>
  651. <title>Example</title>
  652. <link>https://example.org/</link>
  653. <itunes:owner>
  654. <itunes:email>john.doe@example.com</itunes:email>
  655. </itunes:owner>
  656. <item>
  657. <title>Test</title>
  658. <link>https://example.org/item</link>
  659. </item>
  660. </channel>
  661. </rss>`
  662. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  663. if err != nil {
  664. t.Fatal(err)
  665. }
  666. expected := "john.doe@example.com"
  667. result := feed.Entries[0].Author
  668. if result != expected {
  669. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  670. }
  671. }
  672. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  673. data := `<?xml version="1.0" encoding="utf-8"?>
  674. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  675. <channel>
  676. <title>Example</title>
  677. <link>https://example.org/</link>
  678. <item>
  679. <title>Test</title>
  680. <link>https://example.org/item</link>
  681. <googleplay:author>Someone</googleplay:author>
  682. </item>
  683. </channel>
  684. </rss>`
  685. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. expected := "Someone"
  690. result := feed.Entries[0].Author
  691. if result != expected {
  692. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  693. }
  694. }
  695. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  696. data := `<?xml version="1.0" encoding="utf-8"?>
  697. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  698. <channel>
  699. <title>Example</title>
  700. <link>https://example.org/</link>
  701. <googleplay:author>Someone</googleplay:author>
  702. <item>
  703. <title>Test</title>
  704. <link>https://example.org/item</link>
  705. </item>
  706. </channel>
  707. </rss>`
  708. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  709. if err != nil {
  710. t.Fatal(err)
  711. }
  712. expected := "Someone"
  713. result := feed.Entries[0].Author
  714. if result != expected {
  715. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  716. }
  717. }
  718. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  719. data := `<?xml version="1.0" encoding="utf-8"?>
  720. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  721. <channel>
  722. <title>Example</title>
  723. <link>http://example.org/</link>
  724. <item>
  725. <title>Item 1</title>
  726. <link>http://example.org/item1</link>
  727. <description>Description.</description>
  728. <guid isPermaLink="false">UUID</guid>
  729. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  730. </item>
  731. </channel>
  732. </rss>`
  733. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. location, _ := time.LoadLocation("EST")
  738. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  739. if !feed.Entries[0].Date.Equal(expectedDate) {
  740. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  741. }
  742. }
  743. func TestParseEntryWithContentEncoded(t *testing.T) {
  744. data := `<?xml version="1.0" encoding="utf-8"?>
  745. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  746. <channel>
  747. <title>Example</title>
  748. <link>http://example.org/</link>
  749. <item>
  750. <title>Item 1</title>
  751. <link>http://example.org/item1</link>
  752. <description>Description.</description>
  753. <guid isPermaLink="false">UUID</guid>
  754. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  755. </item>
  756. </channel>
  757. </rss>`
  758. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  759. if err != nil {
  760. t.Fatal(err)
  761. }
  762. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  763. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  764. }
  765. }
  766. // https://www.rssboard.org/rss-encoding-examples
  767. func TestParseEntryDescriptionWithEncodedHTMLTags(t *testing.T) {
  768. data := `<?xml version="1.0" encoding="utf-8"?>
  769. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  770. <channel>
  771. <title>Example</title>
  772. <link>http://example.org/</link>
  773. <item>
  774. <title>Item 1</title>
  775. <link>http://example.org/item1</link>
  776. <description>this is &lt;b&gt;bold&lt;/b&gt;</description>
  777. </item>
  778. </channel>
  779. </rss>`
  780. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. if feed.Entries[0].Content != `this is <b>bold</b>` {
  785. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  786. }
  787. }
  788. // https://www.rssboard.org/rss-encoding-examples
  789. func TestParseEntryWithDescriptionWithHTMLCDATA(t *testing.T) {
  790. data := `<?xml version="1.0" encoding="utf-8"?>
  791. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  792. <channel>
  793. <title>Example</title>
  794. <link>http://example.org/</link>
  795. <item>
  796. <title>Item 1</title>
  797. <link>http://example.org/item1</link>
  798. <description><![CDATA[this is <b>bold</b>]]></description>
  799. </item>
  800. </channel>
  801. </rss>`
  802. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  803. if err != nil {
  804. t.Fatal(err)
  805. }
  806. if feed.Entries[0].Content != `this is <b>bold</b>` {
  807. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  808. }
  809. }
  810. // https://www.rssboard.org/rss-encoding-examples
  811. func TestParseEntryDescriptionWithEncodingAngleBracketsInText(t *testing.T) {
  812. data := `<?xml version="1.0" encoding="utf-8"?>
  813. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  814. <channel>
  815. <title>Example</title>
  816. <link>http://example.org/</link>
  817. <item>
  818. <title>Item 1</title>
  819. <link>http://example.org/item1</link>
  820. <description>5 &amp;lt; 8, ticker symbol &amp;lt;BIGCO&amp;gt;</description>
  821. </item>
  822. </channel>
  823. </rss>`
  824. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  825. if err != nil {
  826. t.Fatal(err)
  827. }
  828. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  829. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  830. }
  831. }
  832. // https://www.rssboard.org/rss-encoding-examples
  833. func TestParseEntryDescriptionWithEncodingAngleBracketsWithinCDATASection(t *testing.T) {
  834. data := `<?xml version="1.0" encoding="utf-8"?>
  835. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  836. <channel>
  837. <title>Example</title>
  838. <link>http://example.org/</link>
  839. <item>
  840. <title>Item 1</title>
  841. <link>http://example.org/item1</link>
  842. <description><![CDATA[5 &lt; 8, ticker symbol &lt;BIGCO&gt;]]></description>
  843. </item>
  844. </channel>
  845. </rss>`
  846. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  847. if err != nil {
  848. t.Fatal(err)
  849. }
  850. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  851. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  852. }
  853. }
  854. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  855. data := `<?xml version="1.0" encoding="utf-8"?>
  856. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  857. <channel>
  858. <title>Example</title>
  859. <link>http://example.org/</link>
  860. <item>
  861. <title>Item 1</title>
  862. <link>http://example.org/item1</link>
  863. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  864. </item>
  865. </channel>
  866. </rss>`
  867. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  868. if err != nil {
  869. t.Fatal(err)
  870. }
  871. if feed.Entries[0].URL != "http://example.org/original" {
  872. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  873. }
  874. }
  875. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  876. data := `<?xml version="1.0" encoding="utf-8"?>
  877. <rss version="2.0">
  878. <channel>
  879. <title>Example</title>
  880. <link>http://example.org</link>
  881. <item>
  882. <title>
  883. Some Title
  884. </title>
  885. <link>http://www.example.org/entries/1</link>
  886. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  887. </item>
  888. </channel>
  889. </rss>`
  890. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. if feed.Entries[0].Title != "Some Title" {
  895. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  896. }
  897. }
  898. func TestParseEntryWithEnclosures(t *testing.T) {
  899. data := `<?xml version="1.0" encoding="utf-8"?>
  900. <rss version="2.0">
  901. <channel>
  902. <title>My Podcast Feed</title>
  903. <link>http://example.org</link>
  904. <author>some.email@example.org</author>
  905. <item>
  906. <title>Podcasting with RSS</title>
  907. <link>http://www.example.org/entries/1</link>
  908. <description>An overview of RSS podcasting</description>
  909. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  910. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  911. <enclosure url="http://www.example.org/myaudiofile.mp3"
  912. length="12345"
  913. type="audio/mpeg" />
  914. </item>
  915. </channel>
  916. </rss>`
  917. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  918. if err != nil {
  919. t.Fatal(err)
  920. }
  921. if len(feed.Entries) != 1 {
  922. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  923. }
  924. if len(feed.Entries[0].Enclosures) != 1 {
  925. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  926. }
  927. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  928. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  929. }
  930. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  931. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  932. }
  933. if feed.Entries[0].Enclosures[0].Size != 12345 {
  934. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  935. }
  936. }
  937. func TestParseEntryWithIncorrectEnclosureLength(t *testing.T) {
  938. data := `<?xml version="1.0" encoding="utf-8"?>
  939. <rss version="2.0">
  940. <channel>
  941. <title>My Podcast Feed</title>
  942. <link>http://example.org</link>
  943. <author>some.email@example.org</author>
  944. <item>
  945. <title>Podcasting with RSS</title>
  946. <link>http://www.example.org/entries/1</link>
  947. <description>An overview of RSS podcasting</description>
  948. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  949. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  950. <enclosure url="http://www.example.org/myaudiofile.mp3" length="invalid" type="audio/mpeg" />
  951. <enclosure url="http://www.example.org/myaudiofile.wav" length=" " type="audio" />
  952. </item>
  953. </channel>
  954. </rss>`
  955. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  956. if err != nil {
  957. t.Fatal(err)
  958. }
  959. if len(feed.Entries) != 1 {
  960. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  961. }
  962. if len(feed.Entries[0].Enclosures) != 2 {
  963. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  964. }
  965. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  966. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  967. }
  968. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  969. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  970. }
  971. if feed.Entries[0].Enclosures[0].Size != 0 {
  972. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  973. }
  974. if feed.Entries[0].Enclosures[1].Size != 0 {
  975. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  976. }
  977. }
  978. func TestParseEntryWithDuplicatedEnclosureURL(t *testing.T) {
  979. data := `<?xml version="1.0" encoding="utf-8"?>
  980. <rss version="2.0">
  981. <channel>
  982. <title>My Podcast Feed</title>
  983. <link>http://example.org</link>
  984. <item>
  985. <title>Podcasting with RSS</title>
  986. <link>http://www.example.org/entries/1</link>
  987. <enclosure url="http://www.example.org/myaudiofile.mp3" type="audio/mpeg" />
  988. <enclosure url=" http://www.example.org/myaudiofile.mp3 " type="audio/mpeg" />
  989. </item>
  990. </channel>
  991. </rss>`
  992. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  993. if err != nil {
  994. t.Fatal(err)
  995. }
  996. if len(feed.Entries) != 1 {
  997. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  998. }
  999. if len(feed.Entries[0].Enclosures) != 1 {
  1000. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1001. }
  1002. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  1003. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  1004. }
  1005. }
  1006. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  1007. data := `<?xml version="1.0" encoding="utf-8"?>
  1008. <rss version="2.0">
  1009. <channel>
  1010. <title>My Podcast Feed</title>
  1011. <link>http://example.org</link>
  1012. <author>some.email@example.org</author>
  1013. <item>
  1014. <title>Podcasting with RSS</title>
  1015. <link>http://www.example.org/entries/1</link>
  1016. <description>An overview of RSS podcasting</description>
  1017. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  1018. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  1019. <enclosure url=" " length="0"/>
  1020. </item>
  1021. </channel>
  1022. </rss>`
  1023. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1024. if err != nil {
  1025. t.Fatal(err)
  1026. }
  1027. if len(feed.Entries) != 1 {
  1028. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  1029. }
  1030. if len(feed.Entries[0].Enclosures) != 0 {
  1031. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1032. }
  1033. }
  1034. func TestParseEntryWithRelativeEnclosureURL(t *testing.T) {
  1035. data := `<?xml version="1.0" encoding="utf-8"?>
  1036. <rss version="2.0">
  1037. <channel>
  1038. <title>My Podcast Feed</title>
  1039. <link>http://example.org</link>
  1040. <author>some.email@example.org</author>
  1041. <item>
  1042. <title>Podcasting with RSS</title>
  1043. <link>http://www.example.org/entries/1</link>
  1044. <description>An overview of RSS podcasting</description>
  1045. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  1046. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  1047. <enclosure url=" /files/file.mp3 "/>
  1048. </item>
  1049. </channel>
  1050. </rss>`
  1051. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1052. if err != nil {
  1053. t.Fatal(err)
  1054. }
  1055. if len(feed.Entries) != 1 {
  1056. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  1057. }
  1058. if len(feed.Entries[0].Enclosures) != 1 {
  1059. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1060. }
  1061. if feed.Entries[0].Enclosures[0].URL != "http://example.org/files/file.mp3" {
  1062. t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL)
  1063. }
  1064. }
  1065. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  1066. data := `<?xml version="1.0" encoding="utf-8"?>
  1067. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  1068. <channel>
  1069. <title>My Example Feed</title>
  1070. <link>http://example.org</link>
  1071. <author>some.email@example.org</author>
  1072. <item>
  1073. <title>Example Item</title>
  1074. <link>http://www.example.org/entries/1</link>
  1075. <enclosure
  1076. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  1077. length="76192460"
  1078. type="audio/mpeg" />
  1079. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  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 len(feed.Entries) != 1 {
  1088. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  1089. }
  1090. if len(feed.Entries[0].Enclosures) != 1 {
  1091. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1092. }
  1093. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  1094. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  1095. }
  1096. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  1097. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  1098. }
  1099. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  1100. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  1101. }
  1102. }
  1103. func TestParseEntryWithFeedBurnerEnclosuresAndRelativeURL(t *testing.T) {
  1104. data := `<?xml version="1.0" encoding="utf-8"?>
  1105. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  1106. <channel>
  1107. <title>My Example Feed</title>
  1108. <link>http://example.org</link>
  1109. <item>
  1110. <title>Example Item</title>
  1111. <link>http://www.example.org/entries/1</link>
  1112. <enclosure
  1113. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  1114. length="76192460"
  1115. type="audio/mpeg" />
  1116. <feedburner:origEnclosureLink>/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  1117. </item>
  1118. </channel>
  1119. </rss>`
  1120. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1121. if err != nil {
  1122. t.Fatal(err)
  1123. }
  1124. if len(feed.Entries) != 1 {
  1125. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1126. }
  1127. if len(feed.Entries[0].Enclosures) != 1 {
  1128. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1129. }
  1130. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  1131. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  1132. }
  1133. }
  1134. func TestParseEntryWithRelativeURL(t *testing.T) {
  1135. data := `<?xml version="1.0" encoding="utf-8"?>
  1136. <rss version="2.0">
  1137. <channel>
  1138. <link>https://example.org/</link>
  1139. <item>
  1140. <link>item.html</link>
  1141. </item>
  1142. </channel>
  1143. </rss>`
  1144. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1145. if err != nil {
  1146. t.Fatal(err)
  1147. }
  1148. if feed.Entries[0].Title != "https://example.org/item.html" {
  1149. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  1150. }
  1151. }
  1152. func TestParseEntryWithCommentsURL(t *testing.T) {
  1153. data := `<?xml version="1.0" encoding="utf-8"?>
  1154. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1155. <channel>
  1156. <link>https://example.org/</link>
  1157. <item>
  1158. <title>Item 1</title>
  1159. <link>https://example.org/item1</link>
  1160. <comments>
  1161. https://example.org/comments
  1162. </comments>
  1163. <slash:comments>42</slash:comments>
  1164. </item>
  1165. </channel>
  1166. </rss>`
  1167. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1168. if err != nil {
  1169. t.Fatal(err)
  1170. }
  1171. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  1172. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  1173. }
  1174. }
  1175. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  1176. data := `<?xml version="1.0" encoding="utf-8"?>
  1177. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1178. <channel>
  1179. <link>https://example.org/</link>
  1180. <item>
  1181. <title>Item 1</title>
  1182. <link>https://example.org/item1</link>
  1183. <comments>
  1184. Some text
  1185. </comments>
  1186. </item>
  1187. </channel>
  1188. </rss>`
  1189. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1190. if err != nil {
  1191. t.Fatal(err)
  1192. }
  1193. if feed.Entries[0].CommentsURL != "" {
  1194. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  1195. }
  1196. }
  1197. func TestParseInvalidXml(t *testing.T) {
  1198. data := `garbage`
  1199. _, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1200. if err == nil {
  1201. t.Error("Parse should returns an error")
  1202. }
  1203. }
  1204. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  1205. data := `<?xml version="1.0" encoding="utf-8"?>
  1206. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1207. <channel>
  1208. <link>https://example.org/</link>
  1209. <title>Example &nbsp; Feed</title>
  1210. </channel>
  1211. </rss>`
  1212. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1213. if err != nil {
  1214. t.Fatal(err)
  1215. }
  1216. if feed.Title != "Example \u00a0 Feed" {
  1217. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1218. }
  1219. }
  1220. func TestParseFeedTitleWithUnicodeEntityAndCdata(t *testing.T) {
  1221. data := `<?xml version="1.0" encoding="utf-8"?>
  1222. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1223. <channel>
  1224. <link>https://example.org/</link>
  1225. <title><![CDATA[Jenny&#8217;s Newsletter]]></title>
  1226. </channel>
  1227. </rss>`
  1228. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1229. if err != nil {
  1230. t.Fatal(err)
  1231. }
  1232. if feed.Title != `Jenny’s Newsletter` {
  1233. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1234. }
  1235. }
  1236. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  1237. data := `<?xml version="1.0" encoding="utf-8"?>
  1238. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1239. <channel>
  1240. <link>https://example.org/</link>
  1241. <title>Example</title>
  1242. <item>
  1243. <title>&lt;/example&gt;</title>
  1244. <link>http://www.example.org/entries/1</link>
  1245. </item>
  1246. </channel>
  1247. </rss>`
  1248. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1249. if err != nil {
  1250. t.Fatal(err)
  1251. }
  1252. if feed.Entries[0].Title != "</example>" {
  1253. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1254. }
  1255. }
  1256. func TestParseItemTitleWithNumericCharacterReference(t *testing.T) {
  1257. data := `<?xml version="1.0" encoding="utf-8"?>
  1258. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1259. <channel>
  1260. <link>https://example.org/</link>
  1261. <title>Example</title>
  1262. <item>
  1263. <title>&#931; &#xDF;</title>
  1264. <link>http://www.example.org/article.html</link>
  1265. </item>
  1266. </channel>
  1267. </rss>`
  1268. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1269. if err != nil {
  1270. t.Fatal(err)
  1271. }
  1272. if feed.Entries[0].Title != "Σ ß" {
  1273. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1274. }
  1275. }
  1276. func TestParseItemTitleWithDoubleEncodedEntities(t *testing.T) {
  1277. data := `<?xml version="1.0" encoding="utf-8"?>
  1278. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1279. <channel>
  1280. <link>https://example.org/</link>
  1281. <title>Example</title>
  1282. <item>
  1283. <title>&amp;#39;Text&amp;#39;</title>
  1284. <link>http://www.example.org/article.html</link>
  1285. </item>
  1286. </channel>
  1287. </rss>`
  1288. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1289. if err != nil {
  1290. t.Fatal(err)
  1291. }
  1292. if feed.Entries[0].Title != "'Text'" {
  1293. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1294. }
  1295. }
  1296. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  1297. data := `<?xml version="1.0" encoding="utf-8"?>
  1298. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1299. <channel>
  1300. <link>https://example.org/a&b</link>
  1301. <title>Example Feed</title>
  1302. </channel>
  1303. </rss>`
  1304. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1305. if err != nil {
  1306. t.Fatal(err)
  1307. }
  1308. if feed.SiteURL != "https://example.org/a&b" {
  1309. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  1310. }
  1311. }
  1312. func TestParseEntryWithMediaGroup(t *testing.T) {
  1313. data := `<?xml version="1.0" encoding="utf-8"?>
  1314. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1315. <channel>
  1316. <title>My Example Feed</title>
  1317. <link>https://example.org</link>
  1318. <item>
  1319. <title>Example Item</title>
  1320. <link>http://www.example.org/entries/1</link>
  1321. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  1322. </enclosure>
  1323. <media:group>
  1324. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  1325. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  1326. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  1327. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1328. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1329. <media:content type="application/x-bittorrent" url=" file5.torrent " fileSize="42"></media:content>
  1330. <media:content type="application/x-bittorrent" url=" " fileSize="42"></media:content>
  1331. <media:rating>nonadult</media:rating>
  1332. </media:group>
  1333. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  1334. </item>
  1335. </channel>
  1336. </rss>`
  1337. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1338. if err != nil {
  1339. t.Fatal(err)
  1340. }
  1341. if len(feed.Entries) != 1 {
  1342. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1343. }
  1344. if len(feed.Entries[0].Enclosures) != 6 {
  1345. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1346. }
  1347. expectedResults := []struct {
  1348. url string
  1349. mimeType string
  1350. size int64
  1351. }{
  1352. {"https://example.org/image.jpg", "image/*", 0},
  1353. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  1354. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  1355. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  1356. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  1357. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  1358. }
  1359. for index, enclosure := range feed.Entries[0].Enclosures {
  1360. if expectedResults[index].url != enclosure.URL {
  1361. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1362. }
  1363. if expectedResults[index].mimeType != enclosure.MimeType {
  1364. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1365. }
  1366. if expectedResults[index].size != enclosure.Size {
  1367. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1368. }
  1369. }
  1370. }
  1371. func TestParseEntryWithMediaContent(t *testing.T) {
  1372. data := `<?xml version="1.0" encoding="utf-8"?>
  1373. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1374. <channel>
  1375. <title>My Example Feed</title>
  1376. <link>https://example.org</link>
  1377. <item>
  1378. <title>Example Item</title>
  1379. <link>http://www.example.org/entries/1</link>
  1380. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1381. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1382. <media:thumbnail url=" thumbnail.jpg " />
  1383. <media:thumbnail url=" " />
  1384. <media:content url="https://example.org/media1.jpg" medium="image">
  1385. <media:title type="html">Some Title for Media 1</media:title>
  1386. </media:content>
  1387. <media:content url=" /media2.jpg " medium="image" />
  1388. <media:content url=" " medium="image" />
  1389. </item>
  1390. </channel>
  1391. </rss>`
  1392. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1393. if err != nil {
  1394. t.Fatal(err)
  1395. }
  1396. if len(feed.Entries) != 1 {
  1397. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  1398. }
  1399. if len(feed.Entries[0].Enclosures) != 4 {
  1400. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1401. }
  1402. expectedResults := []struct {
  1403. url string
  1404. mimeType string
  1405. size int64
  1406. }{
  1407. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1408. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1409. {"https://example.org/media1.jpg", "image/*", 0},
  1410. {"https://example.org/media2.jpg", "image/*", 0},
  1411. }
  1412. for index, enclosure := range feed.Entries[0].Enclosures {
  1413. if expectedResults[index].url != enclosure.URL {
  1414. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1415. }
  1416. if expectedResults[index].mimeType != enclosure.MimeType {
  1417. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1418. }
  1419. if expectedResults[index].size != enclosure.Size {
  1420. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1421. }
  1422. }
  1423. }
  1424. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  1425. data := `<?xml version="1.0" encoding="utf-8"?>
  1426. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1427. <channel>
  1428. <title>My Example Feed</title>
  1429. <link>https://website.example.org</link>
  1430. <item>
  1431. <title>Example Item</title>
  1432. <link>http://www.example.org/entries/1</link>
  1433. <media:peerLink type="application/x-bittorrent" href="https://www.example.org/file.torrent" />
  1434. <media:peerLink type="application/x-bittorrent" href="https://www.example.org/file.torrent" />
  1435. <media:peerLink type="application/x-bittorrent" href=" file2.torrent " />
  1436. <media:peerLink type="application/x-bittorrent" href=" " />
  1437. </item>
  1438. </channel>
  1439. </rss>`
  1440. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1441. if err != nil {
  1442. t.Fatal(err)
  1443. }
  1444. if len(feed.Entries) != 1 {
  1445. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  1446. }
  1447. if len(feed.Entries[0].Enclosures) != 2 {
  1448. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1449. }
  1450. expectedResults := []struct {
  1451. url string
  1452. mimeType string
  1453. size int64
  1454. }{
  1455. {"https://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1456. {"https://website.example.org/file2.torrent", "application/x-bittorrent", 0},
  1457. }
  1458. for index, enclosure := range feed.Entries[0].Enclosures {
  1459. if expectedResults[index].url != enclosure.URL {
  1460. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1461. }
  1462. if expectedResults[index].mimeType != enclosure.MimeType {
  1463. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1464. }
  1465. if expectedResults[index].size != enclosure.Size {
  1466. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1467. }
  1468. }
  1469. }
  1470. func TestParseItunesDuration(t *testing.T) {
  1471. data := `<?xml version="1.0" encoding="UTF-8"?>
  1472. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1473. <channel>
  1474. <title>Podcast Example</title>
  1475. <link>http://www.example.com/index.html</link>
  1476. <item>
  1477. <title>Podcast Episode</title>
  1478. <guid>http://example.com/episode.m4a</guid>
  1479. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1480. <itunes:duration>1:23:45</itunes:duration>
  1481. </item>
  1482. </channel>
  1483. </rss>`
  1484. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1485. if err != nil {
  1486. t.Fatal(err)
  1487. }
  1488. expected := 83
  1489. result := feed.Entries[0].ReadingTime
  1490. if expected != result {
  1491. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1492. }
  1493. }
  1494. func TestParseIncorrectItunesDuration(t *testing.T) {
  1495. data := `<?xml version="1.0" encoding="UTF-8"?>
  1496. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1497. <channel>
  1498. <title>Podcast Example</title>
  1499. <link>http://www.example.com/index.html</link>
  1500. <item>
  1501. <title>Podcast Episode</title>
  1502. <guid>http://example.com/episode.m4a</guid>
  1503. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1504. <itunes:duration>invalid</itunes:duration>
  1505. </item>
  1506. </channel>
  1507. </rss>`
  1508. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1509. if err != nil {
  1510. t.Fatal(err)
  1511. }
  1512. expected := 0
  1513. result := feed.Entries[0].ReadingTime
  1514. if expected != result {
  1515. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1516. }
  1517. }
  1518. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1519. data := `<?xml version="1.0" encoding="UTF-8"?>
  1520. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1521. <channel>
  1522. <title>Podcast Example</title>
  1523. <link>http://www.example.com/index.html</link>
  1524. <item>
  1525. <title>Podcast Episode</title>
  1526. <guid>http://example.com/episode.m4a</guid>
  1527. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1528. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1529. <itunes:summary>Episode Summary</itunes:summary>
  1530. </item>
  1531. </channel>
  1532. </rss>`
  1533. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1534. if err != nil {
  1535. t.Fatal(err)
  1536. }
  1537. if len(feed.Entries) != 1 {
  1538. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1539. }
  1540. expected := "Episode Summary"
  1541. result := feed.Entries[0].Content
  1542. if expected != result {
  1543. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1544. }
  1545. }
  1546. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1547. data := `<?xml version="1.0" encoding="UTF-8"?>
  1548. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1549. <channel>
  1550. <title>Podcast Example</title>
  1551. <link>http://www.example.com/index.html</link>
  1552. <item>
  1553. <title>Podcast Episode</title>
  1554. <guid>http://example.com/episode.m4a</guid>
  1555. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1556. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1557. </item>
  1558. </channel>
  1559. </rss>`
  1560. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1561. if err != nil {
  1562. t.Fatal(err)
  1563. }
  1564. if len(feed.Entries) != 1 {
  1565. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1566. }
  1567. expected := "Episode Subtitle"
  1568. result := feed.Entries[0].Content
  1569. if expected != result {
  1570. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1571. }
  1572. }
  1573. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1574. data := `<?xml version="1.0" encoding="UTF-8"?>
  1575. <rss version="2.0"
  1576. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1577. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1578. <channel>
  1579. <title>Podcast Example</title>
  1580. <link>http://www.example.com/index.html</link>
  1581. <item>
  1582. <title>Podcast Episode</title>
  1583. <guid>http://example.com/episode.m4a</guid>
  1584. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1585. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1586. <googleplay:description>Episode Description</googleplay:description>
  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) != 1 {
  1595. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1596. }
  1597. expected := "Episode Description"
  1598. result := feed.Entries[0].Content
  1599. if expected != result {
  1600. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1601. }
  1602. }
  1603. func TestParseEntryWithRSSDescriptionAndMediaDescription(t *testing.T) {
  1604. data := `<?xml version="1.0" encoding="UTF-8"?>
  1605. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1606. <channel>
  1607. <title>Podcast Example</title>
  1608. <link>http://www.example.com/index.html</link>
  1609. <item>
  1610. <title>Entry Title</title>
  1611. <link>http://www.example.com/entries/1</link>
  1612. <description>Entry Description</description>
  1613. <media:description type="plain">Media Description</media:description>
  1614. </item>
  1615. </channel>
  1616. </rss>`
  1617. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1618. if err != nil {
  1619. t.Fatal(err)
  1620. }
  1621. if len(feed.Entries) != 1 {
  1622. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1623. }
  1624. expected := "Entry Description"
  1625. result := feed.Entries[0].Content
  1626. if expected != result {
  1627. t.Errorf(`Unexpected description, got %q instead of %q`, result, expected)
  1628. }
  1629. }
  1630. func TestParseFeedWithCategories(t *testing.T) {
  1631. data := `<?xml version="1.0" encoding="utf-8"?>
  1632. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1633. <channel>
  1634. <title>Example</title>
  1635. <link>https://example.org/</link>
  1636. <category>Category 1</category>
  1637. <category><![CDATA[Category 2]]></category>
  1638. <item>
  1639. <title>Test</title>
  1640. <link>https://example.org/item</link>
  1641. </item>
  1642. </channel>
  1643. </rss>`
  1644. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1645. if err != nil {
  1646. t.Fatal(err)
  1647. }
  1648. if len(feed.Entries[0].Tags) != 2 {
  1649. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1650. }
  1651. expected := []string{"Category 1", "Category 2"}
  1652. result := feed.Entries[0].Tags
  1653. for i, tag := range result {
  1654. if tag != expected[i] {
  1655. t.Errorf("Incorrect tag, got: %q", tag)
  1656. }
  1657. }
  1658. }
  1659. func TestParseEntryWithCategories(t *testing.T) {
  1660. data := `<?xml version="1.0" encoding="utf-8"?>
  1661. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1662. <channel>
  1663. <title>Example</title>
  1664. <link>https://example.org/</link>
  1665. <category>Category 3</category>
  1666. <item>
  1667. <title>Test</title>
  1668. <link>https://example.org/item</link>
  1669. <category>Category 1</category>
  1670. <category><![CDATA[Category 2]]></category>
  1671. </item>
  1672. </channel>
  1673. </rss>`
  1674. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1675. if err != nil {
  1676. t.Fatal(err)
  1677. }
  1678. if len(feed.Entries[0].Tags) != 2 {
  1679. t.Fatalf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1680. }
  1681. expected := []string{"Category 1", "Category 2"}
  1682. result := feed.Entries[0].Tags
  1683. for i, tag := range result {
  1684. if tag != expected[i] {
  1685. t.Errorf("Incorrect tag, got: %q", tag)
  1686. }
  1687. }
  1688. }
  1689. func TestParseFeedWithItunesCategories(t *testing.T) {
  1690. data := `<?xml version="1.0" encoding="utf-8"?>
  1691. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  1692. <channel>
  1693. <title>Example</title>
  1694. <link>https://example.org/</link>
  1695. <itunes:category text="Society &amp; Culture">
  1696. <itunes:category text="Documentary" />
  1697. </itunes:category>
  1698. <itunes:category text="Health">
  1699. <itunes:category text="Mental Health" />
  1700. </itunes:category>
  1701. <item>
  1702. <title>Test</title>
  1703. <link>https://example.org/item</link>
  1704. </item>
  1705. </channel>
  1706. </rss>`
  1707. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1708. if err != nil {
  1709. t.Fatal(err)
  1710. }
  1711. if len(feed.Entries[0].Tags) != 4 {
  1712. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1713. }
  1714. expected := []string{"Society & Culture", "Documentary", "Health", "Mental Health"}
  1715. result := feed.Entries[0].Tags
  1716. for i, tag := range result {
  1717. if tag != expected[i] {
  1718. t.Errorf("Incorrect tag, got: %q", tag)
  1719. }
  1720. }
  1721. }
  1722. func TestParseFeedWithGooglePlayCategory(t *testing.T) {
  1723. data := `<?xml version="1.0" encoding="utf-8"?>
  1724. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0">
  1725. <channel>
  1726. <title>Example</title>
  1727. <link>https://example.org/</link>
  1728. <gplay:category text="Art"></gplay:category>
  1729. <item>
  1730. <title>Test</title>
  1731. <link>https://example.org/item</link>
  1732. </item>
  1733. </channel>
  1734. </rss>`
  1735. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1736. if err != nil {
  1737. t.Fatal(err)
  1738. }
  1739. if len(feed.Entries[0].Tags) != 1 {
  1740. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1741. }
  1742. expected := []string{"Art"}
  1743. result := feed.Entries[0].Tags
  1744. for i, tag := range result {
  1745. if tag != expected[i] {
  1746. t.Errorf("Incorrect tag, got: %q", tag)
  1747. }
  1748. }
  1749. }
  1750. func TestParseEntryWithMediaCategories(t *testing.T) {
  1751. data := `<?xml version="1.0" encoding="utf-8"?>
  1752. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  1753. <channel>
  1754. <title>Example</title>
  1755. <link>https://example.org/</link>
  1756. <item>
  1757. <title>Test</title>
  1758. <link>https://example.org/item</link>
  1759. <media:category label="Visual Art">visual_art</media:category>
  1760. <media:category scheme="http://search.yahoo.com/mrss/category_ schema">music/artist/album/song</media:category>
  1761. <media:category scheme="urn:flickr:tags">ycantpark mobile</media:category>
  1762. <media:category scheme="http://dmoz.org" label="Ace Ventura - Pet Detective">Arts/Movies/Titles/A/Ace_Ventura_Series/Ace_Ventura_ -_Pet_Detective</media:category>
  1763. </item>
  1764. </channel>
  1765. </rss>`
  1766. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1767. if err != nil {
  1768. t.Fatal(err)
  1769. }
  1770. if len(feed.Entries[0].Tags) != 2 {
  1771. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1772. }
  1773. expected := []string{"Visual Art", "Ace Ventura - Pet Detective"}
  1774. result := feed.Entries[0].Tags
  1775. for i, tag := range result {
  1776. if tag != expected[i] {
  1777. t.Errorf("Incorrect tag, got: %q", tag)
  1778. }
  1779. }
  1780. }
  1781. func TestParseFeedWithTTLField(t *testing.T) {
  1782. data := `<?xml version="1.0" encoding="utf-8"?>
  1783. <rss version="2.0">
  1784. <channel>
  1785. <title>Example</title>
  1786. <link>https://example.org/</link>
  1787. <ttl>60</ttl>
  1788. <item>
  1789. <title>Test</title>
  1790. <link>https://example.org/item</link>
  1791. </item>
  1792. </channel>
  1793. </rss>`
  1794. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1795. if err != nil {
  1796. t.Fatal(err)
  1797. }
  1798. if feed.TTL != 60 {
  1799. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1800. }
  1801. }
  1802. func TestParseFeedWithIncorrectTTLValue(t *testing.T) {
  1803. data := `<?xml version="1.0" encoding="utf-8"?>
  1804. <rss version="2.0">
  1805. <channel>
  1806. <title>Example</title>
  1807. <link>https://example.org/</link>
  1808. <ttl>invalid</ttl>
  1809. <item>
  1810. <title>Test</title>
  1811. <link>https://example.org/item</link>
  1812. </item>
  1813. </channel>
  1814. </rss>`
  1815. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1816. if err != nil {
  1817. t.Fatal(err)
  1818. }
  1819. if feed.TTL != 0 {
  1820. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1821. }
  1822. }