parser_test.go 63 KB

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