parser_test.go 62 KB

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