parser_test.go 60 KB

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