parser_test.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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.NewBufferString(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 TestParseFeedWithoutTitle(t *testing.T) {
  97. data := `<?xml version="1.0" encoding="utf-8"?>
  98. <rss version="2.0">
  99. <channel>
  100. <link>https://example.org/</link>
  101. </channel>
  102. </rss>`
  103. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if feed.Title != "https://example.org/" {
  108. t.Errorf("Incorrect feed title, got: %s", feed.Title)
  109. }
  110. }
  111. func TestParseEntryWithoutTitleAndDescription(t *testing.T) {
  112. data := `<?xml version="1.0" encoding="utf-8"?>
  113. <rss version="2.0">
  114. <channel>
  115. <link>https://example.org/</link>
  116. <item>
  117. <link>https://example.org/item</link>
  118. </item>
  119. </channel>
  120. </rss>`
  121. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if feed.Entries[0].Title != "https://example.org/item" {
  126. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  127. }
  128. }
  129. func TestParseEntryWithoutTitleButWithDescription(t *testing.T) {
  130. data := `<?xml version="1.0" encoding="utf-8"?>
  131. <rss version="2.0">
  132. <channel>
  133. <link>https://example.org/</link>
  134. <item>
  135. <link>https://example.org/item</link>
  136. <description>
  137. This is the description
  138. </description>
  139. </item>
  140. </channel>
  141. </rss>`
  142. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. if feed.Entries[0].Title != "This is the description" {
  147. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  148. }
  149. }
  150. func TestParseEntryWithMediaTitle(t *testing.T) {
  151. data := `<?xml version="1.0" encoding="utf-8"?>
  152. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  153. <channel>
  154. <link>https://example.org/</link>
  155. <item>
  156. <title>Entry Title</title>
  157. <link>https://example.org/item</link>
  158. <media:title>Media Title</media:title>
  159. </item>
  160. </channel>
  161. </rss>`
  162. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. if feed.Entries[0].Title != "Entry Title" {
  167. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  168. }
  169. }
  170. func TestParseEntryWithDCTitleOnly(t *testing.T) {
  171. data := `<?xml version="1.0" encoding="utf-8"?>
  172. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  173. <channel>
  174. <link>https://example.org/</link>
  175. <item>
  176. <dc:title>Entry Title</dc:title>
  177. <link>https://example.org/item</link>
  178. </item>
  179. </channel>
  180. </rss>`
  181. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. if feed.Entries[0].Title != "Entry Title" {
  186. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  187. }
  188. }
  189. func TestParseEntryWithoutLink(t *testing.T) {
  190. data := `<?xml version="1.0" encoding="utf-8"?>
  191. <rss version="2.0">
  192. <channel>
  193. <link>https://example.org/</link>
  194. <item>
  195. <guid isPermaLink="false">1234</guid>
  196. </item>
  197. </channel>
  198. </rss>`
  199. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if feed.Entries[0].URL != "https://example.org/" {
  204. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  205. }
  206. if feed.Entries[0].Hash != "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4" {
  207. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  208. }
  209. }
  210. func TestParseEntryWithOnlyGuidPermalink(t *testing.T) {
  211. data := `<?xml version="1.0" encoding="utf-8"?>
  212. <rss version="2.0">
  213. <channel>
  214. <link>https://example.org/</link>
  215. <item>
  216. <guid isPermaLink="true">https://example.org/some-article.html</guid>
  217. </item>
  218. <item>
  219. <guid>https://example.org/another-article.html</guid>
  220. </item>
  221. </channel>
  222. </rss>`
  223. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. if feed.Entries[0].URL != "https://example.org/some-article.html" {
  228. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  229. }
  230. if feed.Entries[1].URL != "https://example.org/another-article.html" {
  231. t.Errorf("Incorrect entry link, got: %s", feed.Entries[1].URL)
  232. }
  233. }
  234. func TestParseEntryWithAtomLink(t *testing.T) {
  235. data := `<?xml version="1.0" encoding="utf-8"?>
  236. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  237. <channel>
  238. <link>https://example.org/</link>
  239. <item>
  240. <title>Test</title>
  241. <atom:link href="https://example.org/item" />
  242. </item>
  243. </channel>
  244. </rss>`
  245. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. if feed.Entries[0].URL != "https://example.org/item" {
  250. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  251. }
  252. }
  253. func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
  254. data := `<?xml version="1.0" encoding="utf-8"?>
  255. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  256. <channel>
  257. <link>https://example.org/</link>
  258. <item>
  259. <title>Test</title>
  260. <atom:link rel="payment" href="https://example.org/a" />
  261. <atom:link rel="http://foobar.tld" href="https://example.org/b" />
  262. </item>
  263. </channel>
  264. </rss>`
  265. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. if feed.Entries[0].URL != "https://example.org/b" {
  270. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  271. }
  272. }
  273. func TestParseFeedURLWithAtomLink(t *testing.T) {
  274. data := `<?xml version="1.0" encoding="utf-8"?>
  275. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  276. <channel>
  277. <title>Example</title>
  278. <link>https://example.org/</link>
  279. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  280. </channel>
  281. </rss>`
  282. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. if feed.FeedURL != "https://example.org/rss" {
  287. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  288. }
  289. if feed.SiteURL != "https://example.org/" {
  290. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  291. }
  292. }
  293. func TestParseFeedWithWebmaster(t *testing.T) {
  294. data := `<?xml version="1.0" encoding="utf-8"?>
  295. <rss version="2.0">
  296. <channel>
  297. <title>Example</title>
  298. <link>https://example.org/</link>
  299. <webMaster>webmaster@example.com</webMaster>
  300. <item>
  301. <title>Test</title>
  302. <link>https://example.org/item</link>
  303. </item>
  304. </channel>
  305. </rss>`
  306. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. expected := "webmaster@example.com"
  311. result := feed.Entries[0].Author
  312. if result != expected {
  313. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  314. }
  315. }
  316. func TestParseFeedWithManagingEditor(t *testing.T) {
  317. data := `<?xml version="1.0" encoding="utf-8"?>
  318. <rss version="2.0">
  319. <channel>
  320. <title>Example</title>
  321. <link>https://example.org/</link>
  322. <webMaster>webmaster@example.com</webMaster>
  323. <managingEditor>editor@example.com</managingEditor>
  324. <item>
  325. <title>Test</title>
  326. <link>https://example.org/item</link>
  327. </item>
  328. </channel>
  329. </rss>`
  330. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. expected := "editor@example.com"
  335. result := feed.Entries[0].Author
  336. if result != expected {
  337. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  338. }
  339. }
  340. func TestParseEntryWithAuthorAndInnerHTML(t *testing.T) {
  341. data := `<?xml version="1.0" encoding="utf-8"?>
  342. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  343. <channel>
  344. <title>Example</title>
  345. <link>https://example.org/</link>
  346. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  347. <item>
  348. <title>Test</title>
  349. <link>https://example.org/item</link>
  350. <author>by <a itemprop="url" class="author" rel="author" href="/author/foobar">Foo Bar</a></author>
  351. </item>
  352. </channel>
  353. </rss>`
  354. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  355. if err != nil {
  356. t.Fatal(err)
  357. }
  358. expected := "by Foo Bar"
  359. result := feed.Entries[0].Author
  360. if result != expected {
  361. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  362. }
  363. }
  364. func TestParseEntryWithAuthorAndCDATA(t *testing.T) {
  365. data := `<?xml version="1.0" encoding="utf-8"?>
  366. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  367. <channel>
  368. <title>Example</title>
  369. <link>https://example.org/</link>
  370. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  371. <item>
  372. <title>Test</title>
  373. <link>https://example.org/item</link>
  374. <author>
  375. by <![CDATA[Foo Bar]]>
  376. </author>
  377. </item>
  378. </channel>
  379. </rss>`
  380. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. expected := "by Foo Bar"
  385. result := feed.Entries[0].Author
  386. if result != expected {
  387. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  388. }
  389. }
  390. func TestParseEntryWithNonStandardAtomAuthor(t *testing.T) {
  391. data := `<?xml version="1.0" encoding="utf-8"?>
  392. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  393. <channel>
  394. <title>Example</title>
  395. <link>https://example.org/</link>
  396. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  397. <item>
  398. <title>Test</title>
  399. <link>https://example.org/item</link>
  400. <author xmlns:author="http://www.w3.org/2005/Atom">
  401. <name>Foo Bar</name>
  402. <title>Vice President</title>
  403. <department/>
  404. <company>FooBar Inc.</company>
  405. </author>
  406. </item>
  407. </channel>
  408. </rss>`
  409. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  410. if err != nil {
  411. t.Fatal(err)
  412. }
  413. expected := "Foo Bar"
  414. result := feed.Entries[0].Author
  415. if result != expected {
  416. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  417. }
  418. }
  419. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  420. data := `<?xml version="1.0" encoding="utf-8"?>
  421. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  422. <channel>
  423. <title>Example</title>
  424. <link>https://example.org/</link>
  425. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  426. <item>
  427. <title>Test</title>
  428. <link>https://example.org/item</link>
  429. <atom:author>
  430. <email>author@example.org</email>
  431. </atom:author>
  432. </item>
  433. </channel>
  434. </rss>`
  435. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  436. if err != nil {
  437. t.Fatal(err)
  438. }
  439. expected := "author@example.org"
  440. result := feed.Entries[0].Author
  441. if result != expected {
  442. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  443. }
  444. }
  445. func TestParseEntryWithAtomAuthor(t *testing.T) {
  446. data := `<?xml version="1.0" encoding="utf-8"?>
  447. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  448. <channel>
  449. <title>Example</title>
  450. <link>https://example.org/</link>
  451. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  452. <item>
  453. <title>Test</title>
  454. <link>https://example.org/item</link>
  455. <atom:author>
  456. <name>Foo Bar</name>
  457. </atom:author>
  458. </item>
  459. </channel>
  460. </rss>`
  461. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. expected := "Foo Bar"
  466. result := feed.Entries[0].Author
  467. if result != expected {
  468. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  469. }
  470. }
  471. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  472. data := `<?xml version="1.0" encoding="utf-8"?>
  473. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  474. <channel>
  475. <title>Example</title>
  476. <link>https://example.org/</link>
  477. <item>
  478. <title>Test</title>
  479. <link>https://example.org/item</link>
  480. <dc:creator>Me (me@example.com)</dc:creator>
  481. </item>
  482. </channel>
  483. </rss>`
  484. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. expected := "Me (me@example.com)"
  489. result := feed.Entries[0].Author
  490. if result != expected {
  491. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  492. }
  493. }
  494. func TestParseEntryWithItunesAuthor(t *testing.T) {
  495. data := `<?xml version="1.0" encoding="utf-8"?>
  496. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  497. <channel>
  498. <title>Example</title>
  499. <link>https://example.org/</link>
  500. <item>
  501. <title>Test</title>
  502. <link>https://example.org/item</link>
  503. <itunes:author>Someone</itunes:author>
  504. </item>
  505. </channel>
  506. </rss>`
  507. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  508. if err != nil {
  509. t.Fatal(err)
  510. }
  511. expected := "Someone"
  512. result := feed.Entries[0].Author
  513. if result != expected {
  514. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  515. }
  516. }
  517. func TestParseFeedWithItunesAuthor(t *testing.T) {
  518. data := `<?xml version="1.0" encoding="utf-8"?>
  519. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  520. <channel>
  521. <title>Example</title>
  522. <link>https://example.org/</link>
  523. <itunes:author>Someone</itunes:author>
  524. <item>
  525. <title>Test</title>
  526. <link>https://example.org/item</link>
  527. </item>
  528. </channel>
  529. </rss>`
  530. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. expected := "Someone"
  535. result := feed.Entries[0].Author
  536. if result != expected {
  537. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  538. }
  539. }
  540. func TestParseFeedWithItunesOwner(t *testing.T) {
  541. data := `<?xml version="1.0" encoding="utf-8"?>
  542. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  543. <channel>
  544. <title>Example</title>
  545. <link>https://example.org/</link>
  546. <itunes:owner>
  547. <itunes:name>John Doe</itunes:name>
  548. <itunes:email>john.doe@example.com</itunes:email>
  549. </itunes:owner>
  550. <item>
  551. <title>Test</title>
  552. <link>https://example.org/item</link>
  553. </item>
  554. </channel>
  555. </rss>`
  556. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  557. if err != nil {
  558. t.Fatal(err)
  559. }
  560. expected := "John Doe"
  561. result := feed.Entries[0].Author
  562. if result != expected {
  563. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  564. }
  565. }
  566. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  567. data := `<?xml version="1.0" encoding="utf-8"?>
  568. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  569. <channel>
  570. <title>Example</title>
  571. <link>https://example.org/</link>
  572. <itunes:owner>
  573. <itunes:email>john.doe@example.com</itunes:email>
  574. </itunes:owner>
  575. <item>
  576. <title>Test</title>
  577. <link>https://example.org/item</link>
  578. </item>
  579. </channel>
  580. </rss>`
  581. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  582. if err != nil {
  583. t.Fatal(err)
  584. }
  585. expected := "john.doe@example.com"
  586. result := feed.Entries[0].Author
  587. if result != expected {
  588. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  589. }
  590. }
  591. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  592. data := `<?xml version="1.0" encoding="utf-8"?>
  593. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  594. <channel>
  595. <title>Example</title>
  596. <link>https://example.org/</link>
  597. <item>
  598. <title>Test</title>
  599. <link>https://example.org/item</link>
  600. <googleplay:author>Someone</googleplay:author>
  601. </item>
  602. </channel>
  603. </rss>`
  604. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  605. if err != nil {
  606. t.Fatal(err)
  607. }
  608. expected := "Someone"
  609. result := feed.Entries[0].Author
  610. if result != expected {
  611. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  612. }
  613. }
  614. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  615. data := `<?xml version="1.0" encoding="utf-8"?>
  616. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  617. <channel>
  618. <title>Example</title>
  619. <link>https://example.org/</link>
  620. <googleplay:author>Someone</googleplay:author>
  621. <item>
  622. <title>Test</title>
  623. <link>https://example.org/item</link>
  624. </item>
  625. </channel>
  626. </rss>`
  627. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  628. if err != nil {
  629. t.Fatal(err)
  630. }
  631. expected := "Someone"
  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 TestParseEntryWithDublinCoreDate(t *testing.T) {
  638. data := `<?xml version="1.0" encoding="utf-8"?>
  639. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  640. <channel>
  641. <title>Example</title>
  642. <link>http://example.org/</link>
  643. <item>
  644. <title>Item 1</title>
  645. <link>http://example.org/item1</link>
  646. <description>Description.</description>
  647. <guid isPermaLink="false">UUID</guid>
  648. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  649. </item>
  650. </channel>
  651. </rss>`
  652. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  653. if err != nil {
  654. t.Fatal(err)
  655. }
  656. location, _ := time.LoadLocation("EST")
  657. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  658. if !feed.Entries[0].Date.Equal(expectedDate) {
  659. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  660. }
  661. }
  662. func TestParseEntryWithContentEncoded(t *testing.T) {
  663. data := `<?xml version="1.0" encoding="utf-8"?>
  664. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  665. <channel>
  666. <title>Example</title>
  667. <link>http://example.org/</link>
  668. <item>
  669. <title>Item 1</title>
  670. <link>http://example.org/item1</link>
  671. <description>Description.</description>
  672. <guid isPermaLink="false">UUID</guid>
  673. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  674. </item>
  675. </channel>
  676. </rss>`
  677. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  678. if err != nil {
  679. t.Fatal(err)
  680. }
  681. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  682. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  683. }
  684. }
  685. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  686. data := `<?xml version="1.0" encoding="utf-8"?>
  687. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  688. <channel>
  689. <title>Example</title>
  690. <link>http://example.org/</link>
  691. <item>
  692. <title>Item 1</title>
  693. <link>http://example.org/item1</link>
  694. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  695. </item>
  696. </channel>
  697. </rss>`
  698. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  699. if err != nil {
  700. t.Fatal(err)
  701. }
  702. if feed.Entries[0].URL != "http://example.org/original" {
  703. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  704. }
  705. }
  706. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  707. data := `<?xml version="1.0" encoding="utf-8"?>
  708. <rss version="2.0">
  709. <channel>
  710. <title>Example</title>
  711. <link>http://example.org</link>
  712. <item>
  713. <title>
  714. Some Title
  715. </title>
  716. <link>http://www.example.org/entries/1</link>
  717. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  718. </item>
  719. </channel>
  720. </rss>`
  721. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  722. if err != nil {
  723. t.Fatal(err)
  724. }
  725. if feed.Entries[0].Title != "Some Title" {
  726. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  727. }
  728. }
  729. func TestParseEntryWithEnclosures(t *testing.T) {
  730. data := `<?xml version="1.0" encoding="utf-8"?>
  731. <rss version="2.0">
  732. <channel>
  733. <title>My Podcast Feed</title>
  734. <link>http://example.org</link>
  735. <author>some.email@example.org</author>
  736. <item>
  737. <title>Podcasting with RSS</title>
  738. <link>http://www.example.org/entries/1</link>
  739. <description>An overview of RSS podcasting</description>
  740. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  741. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  742. <enclosure url="http://www.example.org/myaudiofile.mp3"
  743. length="12345"
  744. type="audio/mpeg" />
  745. </item>
  746. </channel>
  747. </rss>`
  748. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  749. if err != nil {
  750. t.Fatal(err)
  751. }
  752. if len(feed.Entries) != 1 {
  753. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  754. }
  755. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  756. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  757. }
  758. if len(feed.Entries[0].Enclosures) != 1 {
  759. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  760. }
  761. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  762. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  763. }
  764. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  765. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  766. }
  767. if feed.Entries[0].Enclosures[0].Size != 12345 {
  768. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  769. }
  770. }
  771. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  772. data := `<?xml version="1.0" encoding="utf-8"?>
  773. <rss version="2.0">
  774. <channel>
  775. <title>My Podcast Feed</title>
  776. <link>http://example.org</link>
  777. <author>some.email@example.org</author>
  778. <item>
  779. <title>Podcasting with RSS</title>
  780. <link>http://www.example.org/entries/1</link>
  781. <description>An overview of RSS podcasting</description>
  782. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  783. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  784. <enclosure url="" length="0"/>
  785. </item>
  786. </channel>
  787. </rss>`
  788. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  789. if err != nil {
  790. t.Fatal(err)
  791. }
  792. if len(feed.Entries) != 1 {
  793. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  794. }
  795. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  796. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  797. }
  798. if len(feed.Entries[0].Enclosures) != 0 {
  799. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  800. }
  801. }
  802. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  803. data := `<?xml version="1.0" encoding="utf-8"?>
  804. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  805. <channel>
  806. <title>My Example Feed</title>
  807. <link>http://example.org</link>
  808. <author>some.email@example.org</author>
  809. <item>
  810. <title>Example Item</title>
  811. <link>http://www.example.org/entries/1</link>
  812. <enclosure
  813. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  814. length="76192460"
  815. type="audio/mpeg" />
  816. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  817. </item>
  818. </channel>
  819. </rss>`
  820. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  821. if err != nil {
  822. t.Fatal(err)
  823. }
  824. if len(feed.Entries) != 1 {
  825. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  826. }
  827. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  828. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  829. }
  830. if len(feed.Entries[0].Enclosures) != 1 {
  831. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  832. }
  833. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  834. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  835. }
  836. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  837. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  838. }
  839. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  840. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  841. }
  842. }
  843. func TestParseEntryWithRelativeURL(t *testing.T) {
  844. data := `<?xml version="1.0" encoding="utf-8"?>
  845. <rss version="2.0">
  846. <channel>
  847. <link>https://example.org/</link>
  848. <item>
  849. <link>item.html</link>
  850. </item>
  851. </channel>
  852. </rss>`
  853. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  854. if err != nil {
  855. t.Fatal(err)
  856. }
  857. if feed.Entries[0].Title != "https://example.org/item.html" {
  858. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  859. }
  860. }
  861. func TestParseEntryWithCommentsURL(t *testing.T) {
  862. data := `<?xml version="1.0" encoding="utf-8"?>
  863. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  864. <channel>
  865. <link>https://example.org/</link>
  866. <item>
  867. <title>Item 1</title>
  868. <link>https://example.org/item1</link>
  869. <comments>
  870. https://example.org/comments
  871. </comments>
  872. <slash:comments>42</slash:comments>
  873. </item>
  874. </channel>
  875. </rss>`
  876. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  877. if err != nil {
  878. t.Fatal(err)
  879. }
  880. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  881. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  882. }
  883. }
  884. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  885. data := `<?xml version="1.0" encoding="utf-8"?>
  886. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  887. <channel>
  888. <link>https://example.org/</link>
  889. <item>
  890. <title>Item 1</title>
  891. <link>https://example.org/item1</link>
  892. <comments>
  893. Some text
  894. </comments>
  895. </item>
  896. </channel>
  897. </rss>`
  898. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  899. if err != nil {
  900. t.Fatal(err)
  901. }
  902. if feed.Entries[0].CommentsURL != "" {
  903. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  904. }
  905. }
  906. func TestParseInvalidXml(t *testing.T) {
  907. data := `garbage`
  908. _, err := Parse("https://example.org/", bytes.NewBufferString(data))
  909. if err == nil {
  910. t.Error("Parse should returns an error")
  911. }
  912. }
  913. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  914. data := `<?xml version="1.0" encoding="utf-8"?>
  915. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  916. <channel>
  917. <link>https://example.org/</link>
  918. <title>Example &nbsp; Feed</title>
  919. </channel>
  920. </rss>`
  921. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  922. if err != nil {
  923. t.Fatal(err)
  924. }
  925. if feed.Title != "Example \u00a0 Feed" {
  926. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  927. }
  928. }
  929. func TestParseFeedTitleWithUnicodeEntityAndCdata(t *testing.T) {
  930. data := `<?xml version="1.0" encoding="utf-8"?>
  931. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  932. <channel>
  933. <link>https://example.org/</link>
  934. <title><![CDATA[Jenny&#8217;s Newsletter]]></title>
  935. </channel>
  936. </rss>`
  937. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  938. if err != nil {
  939. t.Fatal(err)
  940. }
  941. if feed.Title != `Jenny’s Newsletter` {
  942. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  943. }
  944. }
  945. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  946. data := `<?xml version="1.0" encoding="utf-8"?>
  947. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  948. <channel>
  949. <link>https://example.org/</link>
  950. <title>Example</title>
  951. <item>
  952. <title>&lt;/example&gt;</title>
  953. <link>http://www.example.org/entries/1</link>
  954. </item>
  955. </channel>
  956. </rss>`
  957. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  958. if err != nil {
  959. t.Fatal(err)
  960. }
  961. if feed.Entries[0].Title != "</example>" {
  962. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  963. }
  964. }
  965. func TestParseItemTitleWithNumericCharacterReference(t *testing.T) {
  966. data := `<?xml version="1.0" encoding="utf-8"?>
  967. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  968. <channel>
  969. <link>https://example.org/</link>
  970. <title>Example</title>
  971. <item>
  972. <title>&#931; &#xDF;</title>
  973. <link>http://www.example.org/article.html</link>
  974. </item>
  975. </channel>
  976. </rss>`
  977. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  978. if err != nil {
  979. t.Fatal(err)
  980. }
  981. if feed.Entries[0].Title != "Σ ß" {
  982. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  983. }
  984. }
  985. func TestParseItemTitleWithDoubleEncodedEntities(t *testing.T) {
  986. data := `<?xml version="1.0" encoding="utf-8"?>
  987. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  988. <channel>
  989. <link>https://example.org/</link>
  990. <title>Example</title>
  991. <item>
  992. <title>&amp;#39;Text&amp;#39;</title>
  993. <link>http://www.example.org/article.html</link>
  994. </item>
  995. </channel>
  996. </rss>`
  997. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  998. if err != nil {
  999. t.Fatal(err)
  1000. }
  1001. if feed.Entries[0].Title != "'Text'" {
  1002. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1003. }
  1004. }
  1005. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  1006. data := `<?xml version="1.0" encoding="utf-8"?>
  1007. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1008. <channel>
  1009. <link>https://example.org/a&b</link>
  1010. <title>Example Feed</title>
  1011. </channel>
  1012. </rss>`
  1013. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1014. if err != nil {
  1015. t.Fatal(err)
  1016. }
  1017. if feed.SiteURL != "https://example.org/a&b" {
  1018. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  1019. }
  1020. }
  1021. func TestParseEntryWithMediaGroup(t *testing.T) {
  1022. data := `<?xml version="1.0" encoding="utf-8"?>
  1023. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1024. <channel>
  1025. <title>My Example Feed</title>
  1026. <link>http://example.org</link>
  1027. <item>
  1028. <title>Example Item</title>
  1029. <link>http://www.example.org/entries/1</link>
  1030. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  1031. </enclosure>
  1032. <media:group>
  1033. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  1034. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  1035. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  1036. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1037. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  1038. <media:rating>nonadult</media:rating>
  1039. </media:group>
  1040. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  1041. </item>
  1042. </channel>
  1043. </rss>`
  1044. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1045. if err != nil {
  1046. t.Fatal(err)
  1047. }
  1048. if len(feed.Entries) != 1 {
  1049. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1050. }
  1051. if len(feed.Entries[0].Enclosures) != 6 {
  1052. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1053. }
  1054. expectedResults := []struct {
  1055. url string
  1056. mimeType string
  1057. size int64
  1058. }{
  1059. {"https://example.org/image.jpg", "image/*", 0},
  1060. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  1061. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  1062. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  1063. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  1064. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  1065. }
  1066. for index, enclosure := range feed.Entries[0].Enclosures {
  1067. if expectedResults[index].url != enclosure.URL {
  1068. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1069. }
  1070. if expectedResults[index].mimeType != enclosure.MimeType {
  1071. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1072. }
  1073. if expectedResults[index].size != enclosure.Size {
  1074. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1075. }
  1076. }
  1077. }
  1078. func TestParseEntryWithMediaContent(t *testing.T) {
  1079. data := `<?xml version="1.0" encoding="utf-8"?>
  1080. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1081. <channel>
  1082. <title>My Example Feed</title>
  1083. <link>http://example.org</link>
  1084. <item>
  1085. <title>Example Item</title>
  1086. <link>http://www.example.org/entries/1</link>
  1087. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1088. <media:content url="https://example.org/media1.jpg" medium="image">
  1089. <media:title type="html">Some Title for Media 1</media:title>
  1090. </media:content>
  1091. <media:content url="https://example.org/media2.jpg" medium="image" />
  1092. </item>
  1093. </channel>
  1094. </rss>`
  1095. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1096. if err != nil {
  1097. t.Fatal(err)
  1098. }
  1099. if len(feed.Entries) != 1 {
  1100. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1101. }
  1102. if len(feed.Entries[0].Enclosures) != 3 {
  1103. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1104. }
  1105. expectedResults := []struct {
  1106. url string
  1107. mimeType string
  1108. size int64
  1109. }{
  1110. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1111. {"https://example.org/media1.jpg", "image/*", 0},
  1112. {"https://example.org/media2.jpg", "image/*", 0},
  1113. }
  1114. for index, enclosure := range feed.Entries[0].Enclosures {
  1115. if expectedResults[index].url != enclosure.URL {
  1116. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1117. }
  1118. if expectedResults[index].mimeType != enclosure.MimeType {
  1119. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1120. }
  1121. if expectedResults[index].size != enclosure.Size {
  1122. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1123. }
  1124. }
  1125. }
  1126. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  1127. data := `<?xml version="1.0" encoding="utf-8"?>
  1128. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1129. <channel>
  1130. <title>My Example Feed</title>
  1131. <link>http://example.org</link>
  1132. <item>
  1133. <title>Example Item</title>
  1134. <link>http://www.example.org/entries/1</link>
  1135. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  1136. </item>
  1137. </channel>
  1138. </rss>`
  1139. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1140. if err != nil {
  1141. t.Fatal(err)
  1142. }
  1143. if len(feed.Entries) != 1 {
  1144. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1145. }
  1146. if len(feed.Entries[0].Enclosures) != 1 {
  1147. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1148. }
  1149. expectedResults := []struct {
  1150. url string
  1151. mimeType string
  1152. size int64
  1153. }{
  1154. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1155. }
  1156. for index, enclosure := range feed.Entries[0].Enclosures {
  1157. if expectedResults[index].url != enclosure.URL {
  1158. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1159. }
  1160. if expectedResults[index].mimeType != enclosure.MimeType {
  1161. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1162. }
  1163. if expectedResults[index].size != enclosure.Size {
  1164. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1165. }
  1166. }
  1167. }
  1168. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1169. data := `<?xml version="1.0" encoding="UTF-8"?>
  1170. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1171. <channel>
  1172. <title>Podcast Example</title>
  1173. <link>http://www.example.com/index.html</link>
  1174. <item>
  1175. <title>Podcast Episode</title>
  1176. <guid>http://example.com/episode.m4a</guid>
  1177. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1178. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1179. <itunes:summary>Episode Summary</itunes:summary>
  1180. </item>
  1181. </channel>
  1182. </rss>`
  1183. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1184. if err != nil {
  1185. t.Fatal(err)
  1186. }
  1187. if len(feed.Entries) != 1 {
  1188. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1189. }
  1190. expected := "Episode Summary"
  1191. result := feed.Entries[0].Content
  1192. if expected != result {
  1193. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1194. }
  1195. }
  1196. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1197. data := `<?xml version="1.0" encoding="UTF-8"?>
  1198. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1199. <channel>
  1200. <title>Podcast Example</title>
  1201. <link>http://www.example.com/index.html</link>
  1202. <item>
  1203. <title>Podcast Episode</title>
  1204. <guid>http://example.com/episode.m4a</guid>
  1205. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1206. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1207. </item>
  1208. </channel>
  1209. </rss>`
  1210. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1211. if err != nil {
  1212. t.Fatal(err)
  1213. }
  1214. if len(feed.Entries) != 1 {
  1215. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1216. }
  1217. expected := "Episode Subtitle"
  1218. result := feed.Entries[0].Content
  1219. if expected != result {
  1220. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1221. }
  1222. }
  1223. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1224. data := `<?xml version="1.0" encoding="UTF-8"?>
  1225. <rss version="2.0"
  1226. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1227. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1228. <channel>
  1229. <title>Podcast Example</title>
  1230. <link>http://www.example.com/index.html</link>
  1231. <item>
  1232. <title>Podcast Episode</title>
  1233. <guid>http://example.com/episode.m4a</guid>
  1234. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1235. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1236. <googleplay:description>Episode Description</googleplay:description>
  1237. </item>
  1238. </channel>
  1239. </rss>`
  1240. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1241. if err != nil {
  1242. t.Fatal(err)
  1243. }
  1244. if len(feed.Entries) != 1 {
  1245. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1246. }
  1247. expected := "Episode Description"
  1248. result := feed.Entries[0].Content
  1249. if expected != result {
  1250. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1251. }
  1252. }
  1253. func TestParseEntryWithCategoryAndInnerHTML(t *testing.T) {
  1254. data := `<?xml version="1.0" encoding="utf-8"?>
  1255. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1256. <channel>
  1257. <title>Example</title>
  1258. <link>https://example.org/</link>
  1259. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  1260. <item>
  1261. <title>Test</title>
  1262. <link>https://example.org/item</link>
  1263. <category>Category 1</category>
  1264. <category>Category 2</category>
  1265. </item>
  1266. </channel>
  1267. </rss>`
  1268. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1269. if err != nil {
  1270. t.Fatal(err)
  1271. }
  1272. if len(feed.Entries[0].Tags) != 2 {
  1273. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1274. }
  1275. expected := "Category 2"
  1276. result := feed.Entries[0].Tags[1]
  1277. if result != expected {
  1278. t.Errorf("Incorrect entry category, got %q instead of %q", result, expected)
  1279. }
  1280. }
  1281. func TestParseEntryWithCategoryAndCDATA(t *testing.T) {
  1282. data := `<?xml version="1.0" encoding="utf-8"?>
  1283. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1284. <channel>
  1285. <title>Example</title>
  1286. <link>https://example.org/</link>
  1287. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  1288. <item>
  1289. <title>Test</title>
  1290. <link>https://example.org/item</link>
  1291. <author>
  1292. by <![CDATA[Foo Bar]]>
  1293. </author>
  1294. <category>Sample Category</category>
  1295. </item>
  1296. </channel>
  1297. </rss>`
  1298. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1299. if err != nil {
  1300. t.Fatal(err)
  1301. }
  1302. if len(feed.Entries[0].Tags) != 1 {
  1303. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1304. }
  1305. expected := "Sample Category"
  1306. result := feed.Entries[0].Tags[0]
  1307. if result != expected {
  1308. t.Errorf("Incorrect entry category, got %q instead of %q", result, expected)
  1309. }
  1310. }
  1311. func TestParseFeedWithTTLField(t *testing.T) {
  1312. data := `<?xml version="1.0" encoding="utf-8"?>
  1313. <rss version="2.0">
  1314. <channel>
  1315. <title>Example</title>
  1316. <link>https://example.org/</link>
  1317. <ttl>60</ttl>
  1318. <item>
  1319. <title>Test</title>
  1320. <link>https://example.org/item</link>
  1321. </item>
  1322. </channel>
  1323. </rss>`
  1324. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1325. if err != nil {
  1326. t.Fatal(err)
  1327. }
  1328. if feed.TTL != 60 {
  1329. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1330. }
  1331. }
  1332. func TestParseFeedWithIncorrectTTLValue(t *testing.T) {
  1333. data := `<?xml version="1.0" encoding="utf-8"?>
  1334. <rss version="2.0">
  1335. <channel>
  1336. <title>Example</title>
  1337. <link>https://example.org/</link>
  1338. <ttl>invalid</ttl>
  1339. <item>
  1340. <title>Test</title>
  1341. <link>https://example.org/item</link>
  1342. </item>
  1343. </channel>
  1344. </rss>`
  1345. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  1346. if err != nil {
  1347. t.Fatal(err)
  1348. }
  1349. if feed.TTL != 0 {
  1350. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1351. }
  1352. }