parser_test.go 62 KB

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