parser_test.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rss // import "miniflux.app/v2/internal/reader/rss"
  4. import (
  5. "bytes"
  6. "testing"
  7. "time"
  8. )
  9. func TestParseRss2Sample(t *testing.T) {
  10. data := `
  11. <?xml version="1.0"?>
  12. <rss version="2.0">
  13. <channel>
  14. <title>Liftoff News</title>
  15. <link>http://liftoff.msfc.nasa.gov/</link>
  16. <description>Liftoff to Space Exploration.</description>
  17. <image>
  18. <url>http://liftoff.msfc.nasa.gov/HomePageXtra/MeatBall.gif</url>
  19. <title>NASA</title>
  20. <link>http://liftoff.msfc.nasa.gov/</link>
  21. </image>
  22. <language>en-us</language>
  23. <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
  24. <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
  25. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  26. <generator>Weblog Editor 2.0</generator>
  27. <managingEditor>editor@example.com</managingEditor>
  28. <webMaster>webmaster@example.com</webMaster>
  29. <item>
  30. <title>Star City</title>
  31. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  32. <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.</description>
  33. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  34. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  35. </item>
  36. <item>
  37. <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.</description>
  38. <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
  39. <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
  40. </item>
  41. <item>
  42. <title>The Engine That Does More</title>
  43. <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
  44. <description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
  45. <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
  46. <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
  47. </item>
  48. <item>
  49. <title>Astronauts' Dirty Laundry</title>
  50. <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
  51. <description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
  52. <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
  53. <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
  54. </item>
  55. </channel>
  56. </rss>`
  57. feed, err := Parse("http://liftoff.msfc.nasa.gov/rss.xml", bytes.NewReader([]byte(data)))
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if feed.Title != "Liftoff News" {
  62. t.Errorf("Incorrect title, got: %s", feed.Title)
  63. }
  64. if feed.FeedURL != "http://liftoff.msfc.nasa.gov/rss.xml" {
  65. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  66. }
  67. if feed.SiteURL != "http://liftoff.msfc.nasa.gov/" {
  68. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  69. }
  70. if feed.IconURL != "http://liftoff.msfc.nasa.gov/HomePageXtra/MeatBall.gif" {
  71. t.Errorf("Incorrect image URL, got: %s", feed.IconURL)
  72. }
  73. if len(feed.Entries) != 4 {
  74. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  75. }
  76. expectedDate := time.Date(2003, time.June, 3, 9, 39, 21, 0, time.UTC)
  77. if !feed.Entries[0].Date.Equal(expectedDate) {
  78. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  79. }
  80. if feed.Entries[0].Hash != "5b2b4ac2fe1786ddf0fd2da2f1b07f64e691264f41f2db3ea360f31bb6d9152b" {
  81. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  82. }
  83. if feed.Entries[0].URL != "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp" {
  84. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  85. }
  86. if feed.Entries[0].Title != "Star City" {
  87. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  88. }
  89. if feed.Entries[0].Content != `How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.` {
  90. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  91. }
  92. if feed.Entries[1].URL != "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572" {
  93. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[1].URL)
  94. }
  95. }
  96. func 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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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="alternate" href="https://example.org/b" />
  262. </item>
  263. </channel>
  264. </rss>`
  265. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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.NewReader([]byte(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. <![CDATA[by Foo Bar]]>
  376. </author>
  377. </item>
  378. </channel>
  379. </rss>`
  380. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(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 TestParseEntryWithAtomAuthorEmail(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. <atom:author>
  401. <email>author@example.org</email>
  402. </atom:author>
  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. expected := "author@example.org"
  411. result := feed.Entries[0].Author
  412. if result != expected {
  413. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  414. }
  415. }
  416. func TestParseEntryWithAtomAuthorName(t *testing.T) {
  417. data := `<?xml version="1.0" encoding="utf-8"?>
  418. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  419. <channel>
  420. <title>Example</title>
  421. <link>https://example.org/</link>
  422. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  423. <item>
  424. <title>Test</title>
  425. <link>https://example.org/item</link>
  426. <atom:author>
  427. <name>Foo Bar</name>
  428. </atom:author>
  429. </item>
  430. </channel>
  431. </rss>`
  432. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. expected := "Foo Bar"
  437. result := feed.Entries[0].Author
  438. if result != expected {
  439. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  440. }
  441. }
  442. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  443. data := `<?xml version="1.0" encoding="utf-8"?>
  444. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  445. <channel>
  446. <title>Example</title>
  447. <link>https://example.org/</link>
  448. <item>
  449. <title>Test</title>
  450. <link>https://example.org/item</link>
  451. <dc:creator>Me (me@example.com)</dc:creator>
  452. </item>
  453. </channel>
  454. </rss>`
  455. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  456. if err != nil {
  457. t.Fatal(err)
  458. }
  459. expected := "Me (me@example.com)"
  460. result := feed.Entries[0].Author
  461. if result != expected {
  462. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  463. }
  464. }
  465. func TestParseEntryWithItunesAuthor(t *testing.T) {
  466. data := `<?xml version="1.0" encoding="utf-8"?>
  467. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  468. <channel>
  469. <title>Example</title>
  470. <link>https://example.org/</link>
  471. <item>
  472. <title>Test</title>
  473. <link>https://example.org/item</link>
  474. <itunes:author>Someone</itunes:author>
  475. </item>
  476. </channel>
  477. </rss>`
  478. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  479. if err != nil {
  480. t.Fatal(err)
  481. }
  482. expected := "Someone"
  483. result := feed.Entries[0].Author
  484. if result != expected {
  485. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  486. }
  487. }
  488. func TestParseFeedWithItunesAuthor(t *testing.T) {
  489. data := `<?xml version="1.0" encoding="utf-8"?>
  490. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  491. <channel>
  492. <title>Example</title>
  493. <link>https://example.org/</link>
  494. <itunes:author>Someone</itunes:author>
  495. <item>
  496. <title>Test</title>
  497. <link>https://example.org/item</link>
  498. </item>
  499. </channel>
  500. </rss>`
  501. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  502. if err != nil {
  503. t.Fatal(err)
  504. }
  505. expected := "Someone"
  506. result := feed.Entries[0].Author
  507. if result != expected {
  508. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  509. }
  510. }
  511. func TestParseFeedWithItunesOwner(t *testing.T) {
  512. data := `<?xml version="1.0" encoding="utf-8"?>
  513. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  514. <channel>
  515. <title>Example</title>
  516. <link>https://example.org/</link>
  517. <itunes:owner>
  518. <itunes:name>John Doe</itunes:name>
  519. <itunes:email>john.doe@example.com</itunes:email>
  520. </itunes:owner>
  521. <item>
  522. <title>Test</title>
  523. <link>https://example.org/item</link>
  524. </item>
  525. </channel>
  526. </rss>`
  527. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  528. if err != nil {
  529. t.Fatal(err)
  530. }
  531. expected := "John Doe"
  532. result := feed.Entries[0].Author
  533. if result != expected {
  534. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  535. }
  536. }
  537. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  538. data := `<?xml version="1.0" encoding="utf-8"?>
  539. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  540. <channel>
  541. <title>Example</title>
  542. <link>https://example.org/</link>
  543. <itunes:owner>
  544. <itunes:email>john.doe@example.com</itunes:email>
  545. </itunes:owner>
  546. <item>
  547. <title>Test</title>
  548. <link>https://example.org/item</link>
  549. </item>
  550. </channel>
  551. </rss>`
  552. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. expected := "john.doe@example.com"
  557. result := feed.Entries[0].Author
  558. if result != expected {
  559. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  560. }
  561. }
  562. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  563. data := `<?xml version="1.0" encoding="utf-8"?>
  564. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  565. <channel>
  566. <title>Example</title>
  567. <link>https://example.org/</link>
  568. <item>
  569. <title>Test</title>
  570. <link>https://example.org/item</link>
  571. <googleplay:author>Someone</googleplay:author>
  572. </item>
  573. </channel>
  574. </rss>`
  575. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. expected := "Someone"
  580. result := feed.Entries[0].Author
  581. if result != expected {
  582. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  583. }
  584. }
  585. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  586. data := `<?xml version="1.0" encoding="utf-8"?>
  587. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  588. <channel>
  589. <title>Example</title>
  590. <link>https://example.org/</link>
  591. <googleplay:author>Someone</googleplay:author>
  592. <item>
  593. <title>Test</title>
  594. <link>https://example.org/item</link>
  595. </item>
  596. </channel>
  597. </rss>`
  598. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  599. if err != nil {
  600. t.Fatal(err)
  601. }
  602. expected := "Someone"
  603. result := feed.Entries[0].Author
  604. if result != expected {
  605. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  606. }
  607. }
  608. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  609. data := `<?xml version="1.0" encoding="utf-8"?>
  610. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  611. <channel>
  612. <title>Example</title>
  613. <link>http://example.org/</link>
  614. <item>
  615. <title>Item 1</title>
  616. <link>http://example.org/item1</link>
  617. <description>Description.</description>
  618. <guid isPermaLink="false">UUID</guid>
  619. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  620. </item>
  621. </channel>
  622. </rss>`
  623. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  624. if err != nil {
  625. t.Fatal(err)
  626. }
  627. location, _ := time.LoadLocation("EST")
  628. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  629. if !feed.Entries[0].Date.Equal(expectedDate) {
  630. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  631. }
  632. }
  633. func TestParseEntryWithContentEncoded(t *testing.T) {
  634. data := `<?xml version="1.0" encoding="utf-8"?>
  635. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  636. <channel>
  637. <title>Example</title>
  638. <link>http://example.org/</link>
  639. <item>
  640. <title>Item 1</title>
  641. <link>http://example.org/item1</link>
  642. <description>Description.</description>
  643. <guid isPermaLink="false">UUID</guid>
  644. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  645. </item>
  646. </channel>
  647. </rss>`
  648. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  649. if err != nil {
  650. t.Fatal(err)
  651. }
  652. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  653. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  654. }
  655. }
  656. // https://www.rssboard.org/rss-encoding-examples
  657. func TestParseEntryDescriptionWithEncodedHTMLTags(t *testing.T) {
  658. data := `<?xml version="1.0" encoding="utf-8"?>
  659. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  660. <channel>
  661. <title>Example</title>
  662. <link>http://example.org/</link>
  663. <item>
  664. <title>Item 1</title>
  665. <link>http://example.org/item1</link>
  666. <description>this is &lt;b&gt;bold&lt;/b&gt;</description>
  667. </item>
  668. </channel>
  669. </rss>`
  670. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  671. if err != nil {
  672. t.Fatal(err)
  673. }
  674. if feed.Entries[0].Content != `this is <b>bold</b>` {
  675. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  676. }
  677. }
  678. // https://www.rssboard.org/rss-encoding-examples
  679. func TestParseEntryWithDescriptionWithHTMLCDATA(t *testing.T) {
  680. data := `<?xml version="1.0" encoding="utf-8"?>
  681. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  682. <channel>
  683. <title>Example</title>
  684. <link>http://example.org/</link>
  685. <item>
  686. <title>Item 1</title>
  687. <link>http://example.org/item1</link>
  688. <description><![CDATA[this is <b>bold</b>]]></description>
  689. </item>
  690. </channel>
  691. </rss>`
  692. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  693. if err != nil {
  694. t.Fatal(err)
  695. }
  696. if feed.Entries[0].Content != `this is <b>bold</b>` {
  697. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  698. }
  699. }
  700. // https://www.rssboard.org/rss-encoding-examples
  701. func TestParseEntryDescriptionWithEncodingAngleBracketsInText(t *testing.T) {
  702. data := `<?xml version="1.0" encoding="utf-8"?>
  703. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  704. <channel>
  705. <title>Example</title>
  706. <link>http://example.org/</link>
  707. <item>
  708. <title>Item 1</title>
  709. <link>http://example.org/item1</link>
  710. <description>5 &amp;lt; 8, ticker symbol &amp;lt;BIGCO&amp;gt;</description>
  711. </item>
  712. </channel>
  713. </rss>`
  714. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  719. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  720. }
  721. }
  722. // https://www.rssboard.org/rss-encoding-examples
  723. func TestParseEntryDescriptionWithEncodingAngleBracketsWithinCDATASection(t *testing.T) {
  724. data := `<?xml version="1.0" encoding="utf-8"?>
  725. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  726. <channel>
  727. <title>Example</title>
  728. <link>http://example.org/</link>
  729. <item>
  730. <title>Item 1</title>
  731. <link>http://example.org/item1</link>
  732. <description><![CDATA[5 &lt; 8, ticker symbol &lt;BIGCO&gt;]]></description>
  733. </item>
  734. </channel>
  735. </rss>`
  736. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  737. if err != nil {
  738. t.Fatal(err)
  739. }
  740. if feed.Entries[0].Content != `5 &lt; 8, ticker symbol &lt;BIGCO&gt;` {
  741. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  742. }
  743. }
  744. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  745. data := `<?xml version="1.0" encoding="utf-8"?>
  746. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  747. <channel>
  748. <title>Example</title>
  749. <link>http://example.org/</link>
  750. <item>
  751. <title>Item 1</title>
  752. <link>http://example.org/item1</link>
  753. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  754. </item>
  755. </channel>
  756. </rss>`
  757. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  758. if err != nil {
  759. t.Fatal(err)
  760. }
  761. if feed.Entries[0].URL != "http://example.org/original" {
  762. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  763. }
  764. }
  765. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  766. data := `<?xml version="1.0" encoding="utf-8"?>
  767. <rss version="2.0">
  768. <channel>
  769. <title>Example</title>
  770. <link>http://example.org</link>
  771. <item>
  772. <title>
  773. Some Title
  774. </title>
  775. <link>http://www.example.org/entries/1</link>
  776. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  777. </item>
  778. </channel>
  779. </rss>`
  780. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. if feed.Entries[0].Title != "Some Title" {
  785. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  786. }
  787. }
  788. func TestParseEntryWithEnclosures(t *testing.T) {
  789. data := `<?xml version="1.0" encoding="utf-8"?>
  790. <rss version="2.0">
  791. <channel>
  792. <title>My Podcast Feed</title>
  793. <link>http://example.org</link>
  794. <author>some.email@example.org</author>
  795. <item>
  796. <title>Podcasting with RSS</title>
  797. <link>http://www.example.org/entries/1</link>
  798. <description>An overview of RSS podcasting</description>
  799. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  800. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  801. <enclosure url="http://www.example.org/myaudiofile.mp3"
  802. length="12345"
  803. type="audio/mpeg" />
  804. </item>
  805. </channel>
  806. </rss>`
  807. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  808. if err != nil {
  809. t.Fatal(err)
  810. }
  811. if len(feed.Entries) != 1 {
  812. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  813. }
  814. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  815. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  816. }
  817. if len(feed.Entries[0].Enclosures) != 1 {
  818. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  819. }
  820. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  821. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  822. }
  823. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  824. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  825. }
  826. if feed.Entries[0].Enclosures[0].Size != 12345 {
  827. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  828. }
  829. }
  830. func TestParseEntryWithIncorrectEnclosureLength(t *testing.T) {
  831. data := `<?xml version="1.0" encoding="utf-8"?>
  832. <rss version="2.0">
  833. <channel>
  834. <title>My Podcast Feed</title>
  835. <link>http://example.org</link>
  836. <author>some.email@example.org</author>
  837. <item>
  838. <title>Podcasting with RSS</title>
  839. <link>http://www.example.org/entries/1</link>
  840. <description>An overview of RSS podcasting</description>
  841. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  842. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  843. <enclosure url="http://www.example.org/myaudiofile.mp3" length="invalid" type="audio/mpeg" />
  844. <enclosure url="http://www.example.org/myaudiofile.wav" length=" " type="audio" />
  845. </item>
  846. </channel>
  847. </rss>`
  848. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  849. if err != nil {
  850. t.Fatal(err)
  851. }
  852. if len(feed.Entries) != 1 {
  853. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  854. }
  855. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  856. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  857. }
  858. if len(feed.Entries[0].Enclosures) != 2 {
  859. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  860. }
  861. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  862. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  863. }
  864. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  865. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  866. }
  867. if feed.Entries[0].Enclosures[0].Size != 0 {
  868. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  869. }
  870. if feed.Entries[0].Enclosures[1].Size != 0 {
  871. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  872. }
  873. }
  874. func TestParseEntryWithEmptyEnclosureURL(t *testing.T) {
  875. data := `<?xml version="1.0" encoding="utf-8"?>
  876. <rss version="2.0">
  877. <channel>
  878. <title>My Podcast Feed</title>
  879. <link>http://example.org</link>
  880. <author>some.email@example.org</author>
  881. <item>
  882. <title>Podcasting with RSS</title>
  883. <link>http://www.example.org/entries/1</link>
  884. <description>An overview of RSS podcasting</description>
  885. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  886. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  887. <enclosure url="" length="0"/>
  888. </item>
  889. </channel>
  890. </rss>`
  891. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  892. if err != nil {
  893. t.Fatal(err)
  894. }
  895. if len(feed.Entries) != 1 {
  896. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  897. }
  898. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  899. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  900. }
  901. if len(feed.Entries[0].Enclosures) != 0 {
  902. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  903. }
  904. }
  905. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  906. data := `<?xml version="1.0" encoding="utf-8"?>
  907. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  908. <channel>
  909. <title>My Example Feed</title>
  910. <link>http://example.org</link>
  911. <author>some.email@example.org</author>
  912. <item>
  913. <title>Example Item</title>
  914. <link>http://www.example.org/entries/1</link>
  915. <enclosure
  916. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  917. length="76192460"
  918. type="audio/mpeg" />
  919. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  920. </item>
  921. </channel>
  922. </rss>`
  923. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  924. if err != nil {
  925. t.Fatal(err)
  926. }
  927. if len(feed.Entries) != 1 {
  928. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  929. }
  930. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  931. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  932. }
  933. if len(feed.Entries[0].Enclosures) != 1 {
  934. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  935. }
  936. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  937. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  938. }
  939. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  940. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  941. }
  942. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  943. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  944. }
  945. }
  946. func TestParseEntryWithRelativeURL(t *testing.T) {
  947. data := `<?xml version="1.0" encoding="utf-8"?>
  948. <rss version="2.0">
  949. <channel>
  950. <link>https://example.org/</link>
  951. <item>
  952. <link>item.html</link>
  953. </item>
  954. </channel>
  955. </rss>`
  956. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  957. if err != nil {
  958. t.Fatal(err)
  959. }
  960. if feed.Entries[0].Title != "https://example.org/item.html" {
  961. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  962. }
  963. }
  964. func TestParseEntryWithCommentsURL(t *testing.T) {
  965. data := `<?xml version="1.0" encoding="utf-8"?>
  966. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  967. <channel>
  968. <link>https://example.org/</link>
  969. <item>
  970. <title>Item 1</title>
  971. <link>https://example.org/item1</link>
  972. <comments>
  973. https://example.org/comments
  974. </comments>
  975. <slash:comments>42</slash:comments>
  976. </item>
  977. </channel>
  978. </rss>`
  979. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  984. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  985. }
  986. }
  987. func TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  988. data := `<?xml version="1.0" encoding="utf-8"?>
  989. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  990. <channel>
  991. <link>https://example.org/</link>
  992. <item>
  993. <title>Item 1</title>
  994. <link>https://example.org/item1</link>
  995. <comments>
  996. Some text
  997. </comments>
  998. </item>
  999. </channel>
  1000. </rss>`
  1001. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1002. if err != nil {
  1003. t.Fatal(err)
  1004. }
  1005. if feed.Entries[0].CommentsURL != "" {
  1006. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  1007. }
  1008. }
  1009. func TestParseInvalidXml(t *testing.T) {
  1010. data := `garbage`
  1011. _, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1012. if err == nil {
  1013. t.Error("Parse should returns an error")
  1014. }
  1015. }
  1016. func TestParseFeedTitleWithHTMLEntity(t *testing.T) {
  1017. data := `<?xml version="1.0" encoding="utf-8"?>
  1018. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1019. <channel>
  1020. <link>https://example.org/</link>
  1021. <title>Example &nbsp; Feed</title>
  1022. </channel>
  1023. </rss>`
  1024. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1025. if err != nil {
  1026. t.Fatal(err)
  1027. }
  1028. if feed.Title != "Example \u00a0 Feed" {
  1029. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1030. }
  1031. }
  1032. func TestParseFeedTitleWithUnicodeEntityAndCdata(t *testing.T) {
  1033. data := `<?xml version="1.0" encoding="utf-8"?>
  1034. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1035. <channel>
  1036. <link>https://example.org/</link>
  1037. <title><![CDATA[Jenny&#8217;s Newsletter]]></title>
  1038. </channel>
  1039. </rss>`
  1040. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1041. if err != nil {
  1042. t.Fatal(err)
  1043. }
  1044. if feed.Title != `Jenny’s Newsletter` {
  1045. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  1046. }
  1047. }
  1048. func TestParseItemTitleWithHTMLEntity(t *testing.T) {
  1049. data := `<?xml version="1.0" encoding="utf-8"?>
  1050. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1051. <channel>
  1052. <link>https://example.org/</link>
  1053. <title>Example</title>
  1054. <item>
  1055. <title>&lt;/example&gt;</title>
  1056. <link>http://www.example.org/entries/1</link>
  1057. </item>
  1058. </channel>
  1059. </rss>`
  1060. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1061. if err != nil {
  1062. t.Fatal(err)
  1063. }
  1064. if feed.Entries[0].Title != "</example>" {
  1065. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1066. }
  1067. }
  1068. func TestParseItemTitleWithNumericCharacterReference(t *testing.T) {
  1069. data := `<?xml version="1.0" encoding="utf-8"?>
  1070. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1071. <channel>
  1072. <link>https://example.org/</link>
  1073. <title>Example</title>
  1074. <item>
  1075. <title>&#931; &#xDF;</title>
  1076. <link>http://www.example.org/article.html</link>
  1077. </item>
  1078. </channel>
  1079. </rss>`
  1080. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1081. if err != nil {
  1082. t.Fatal(err)
  1083. }
  1084. if feed.Entries[0].Title != "Σ ß" {
  1085. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1086. }
  1087. }
  1088. func TestParseItemTitleWithDoubleEncodedEntities(t *testing.T) {
  1089. data := `<?xml version="1.0" encoding="utf-8"?>
  1090. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1091. <channel>
  1092. <link>https://example.org/</link>
  1093. <title>Example</title>
  1094. <item>
  1095. <title>&amp;#39;Text&amp;#39;</title>
  1096. <link>http://www.example.org/article.html</link>
  1097. </item>
  1098. </channel>
  1099. </rss>`
  1100. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1101. if err != nil {
  1102. t.Fatal(err)
  1103. }
  1104. if feed.Entries[0].Title != "'Text'" {
  1105. t.Errorf(`Incorrect title, got: %q`, feed.Entries[0].Title)
  1106. }
  1107. }
  1108. func TestParseFeedLinkWithInvalidCharacterEntity(t *testing.T) {
  1109. data := `<?xml version="1.0" encoding="utf-8"?>
  1110. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  1111. <channel>
  1112. <link>https://example.org/a&b</link>
  1113. <title>Example Feed</title>
  1114. </channel>
  1115. </rss>`
  1116. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1117. if err != nil {
  1118. t.Fatal(err)
  1119. }
  1120. if feed.SiteURL != "https://example.org/a&b" {
  1121. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  1122. }
  1123. }
  1124. func TestParseEntryWithMediaGroup(t *testing.T) {
  1125. data := `<?xml version="1.0" encoding="utf-8"?>
  1126. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1127. <channel>
  1128. <title>My Example Feed</title>
  1129. <link>http://example.org</link>
  1130. <item>
  1131. <title>Example Item</title>
  1132. <link>http://www.example.org/entries/1</link>
  1133. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  1134. </enclosure>
  1135. <media:group>
  1136. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  1137. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  1138. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  1139. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  1140. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  1141. <media:rating>nonadult</media:rating>
  1142. </media:group>
  1143. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  1144. </item>
  1145. </channel>
  1146. </rss>`
  1147. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1148. if err != nil {
  1149. t.Fatal(err)
  1150. }
  1151. if len(feed.Entries) != 1 {
  1152. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1153. }
  1154. if len(feed.Entries[0].Enclosures) != 6 {
  1155. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1156. }
  1157. expectedResults := []struct {
  1158. url string
  1159. mimeType string
  1160. size int64
  1161. }{
  1162. {"https://example.org/image.jpg", "image/*", 0},
  1163. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  1164. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  1165. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  1166. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  1167. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  1168. }
  1169. for index, enclosure := range feed.Entries[0].Enclosures {
  1170. if expectedResults[index].url != enclosure.URL {
  1171. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1172. }
  1173. if expectedResults[index].mimeType != enclosure.MimeType {
  1174. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1175. }
  1176. if expectedResults[index].size != enclosure.Size {
  1177. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1178. }
  1179. }
  1180. }
  1181. func TestParseEntryWithMediaContent(t *testing.T) {
  1182. data := `<?xml version="1.0" encoding="utf-8"?>
  1183. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1184. <channel>
  1185. <title>My Example Feed</title>
  1186. <link>http://example.org</link>
  1187. <item>
  1188. <title>Example Item</title>
  1189. <link>http://www.example.org/entries/1</link>
  1190. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  1191. <media:content url="https://example.org/media1.jpg" medium="image">
  1192. <media:title type="html">Some Title for Media 1</media:title>
  1193. </media:content>
  1194. <media:content url="https://example.org/media2.jpg" medium="image" />
  1195. </item>
  1196. </channel>
  1197. </rss>`
  1198. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1199. if err != nil {
  1200. t.Fatal(err)
  1201. }
  1202. if len(feed.Entries) != 1 {
  1203. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1204. }
  1205. if len(feed.Entries[0].Enclosures) != 3 {
  1206. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1207. }
  1208. expectedResults := []struct {
  1209. url string
  1210. mimeType string
  1211. size int64
  1212. }{
  1213. {"https://example.org/thumbnail.jpg", "image/*", 0},
  1214. {"https://example.org/media1.jpg", "image/*", 0},
  1215. {"https://example.org/media2.jpg", "image/*", 0},
  1216. }
  1217. for index, enclosure := range feed.Entries[0].Enclosures {
  1218. if expectedResults[index].url != enclosure.URL {
  1219. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1220. }
  1221. if expectedResults[index].mimeType != enclosure.MimeType {
  1222. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1223. }
  1224. if expectedResults[index].size != enclosure.Size {
  1225. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1226. }
  1227. }
  1228. }
  1229. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  1230. data := `<?xml version="1.0" encoding="utf-8"?>
  1231. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1232. <channel>
  1233. <title>My Example Feed</title>
  1234. <link>http://example.org</link>
  1235. <item>
  1236. <title>Example Item</title>
  1237. <link>http://www.example.org/entries/1</link>
  1238. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  1239. </item>
  1240. </channel>
  1241. </rss>`
  1242. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1243. if err != nil {
  1244. t.Fatal(err)
  1245. }
  1246. if len(feed.Entries) != 1 {
  1247. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1248. }
  1249. if len(feed.Entries[0].Enclosures) != 1 {
  1250. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  1251. }
  1252. expectedResults := []struct {
  1253. url string
  1254. mimeType string
  1255. size int64
  1256. }{
  1257. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  1258. }
  1259. for index, enclosure := range feed.Entries[0].Enclosures {
  1260. if expectedResults[index].url != enclosure.URL {
  1261. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  1262. }
  1263. if expectedResults[index].mimeType != enclosure.MimeType {
  1264. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  1265. }
  1266. if expectedResults[index].size != enclosure.Size {
  1267. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  1268. }
  1269. }
  1270. }
  1271. func TestParseItunesDuration(t *testing.T) {
  1272. data := `<?xml version="1.0" encoding="UTF-8"?>
  1273. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1274. <channel>
  1275. <title>Podcast Example</title>
  1276. <link>http://www.example.com/index.html</link>
  1277. <item>
  1278. <title>Podcast Episode</title>
  1279. <guid>http://example.com/episode.m4a</guid>
  1280. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1281. <itunes:duration>1:23:45</itunes:duration>
  1282. </item>
  1283. </channel>
  1284. </rss>`
  1285. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1286. if err != nil {
  1287. t.Fatal(err)
  1288. }
  1289. expected := 83
  1290. result := feed.Entries[0].ReadingTime
  1291. if expected != result {
  1292. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1293. }
  1294. }
  1295. func TestParseIncorrectItunesDuration(t *testing.T) {
  1296. data := `<?xml version="1.0" encoding="UTF-8"?>
  1297. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1298. <channel>
  1299. <title>Podcast Example</title>
  1300. <link>http://www.example.com/index.html</link>
  1301. <item>
  1302. <title>Podcast Episode</title>
  1303. <guid>http://example.com/episode.m4a</guid>
  1304. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1305. <itunes:duration>invalid</itunes:duration>
  1306. </item>
  1307. </channel>
  1308. </rss>`
  1309. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1310. if err != nil {
  1311. t.Fatal(err)
  1312. }
  1313. expected := 0
  1314. result := feed.Entries[0].ReadingTime
  1315. if expected != result {
  1316. t.Errorf(`Unexpected podcast duration, got %d instead of %d`, result, expected)
  1317. }
  1318. }
  1319. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  1320. data := `<?xml version="1.0" encoding="UTF-8"?>
  1321. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1322. <channel>
  1323. <title>Podcast Example</title>
  1324. <link>http://www.example.com/index.html</link>
  1325. <item>
  1326. <title>Podcast Episode</title>
  1327. <guid>http://example.com/episode.m4a</guid>
  1328. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1329. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1330. <itunes:summary>Episode Summary</itunes:summary>
  1331. </item>
  1332. </channel>
  1333. </rss>`
  1334. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1335. if err != nil {
  1336. t.Fatal(err)
  1337. }
  1338. if len(feed.Entries) != 1 {
  1339. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1340. }
  1341. expected := "Episode Summary"
  1342. result := feed.Entries[0].Content
  1343. if expected != result {
  1344. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1345. }
  1346. }
  1347. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  1348. data := `<?xml version="1.0" encoding="UTF-8"?>
  1349. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1350. <channel>
  1351. <title>Podcast Example</title>
  1352. <link>http://www.example.com/index.html</link>
  1353. <item>
  1354. <title>Podcast Episode</title>
  1355. <guid>http://example.com/episode.m4a</guid>
  1356. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1357. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1358. </item>
  1359. </channel>
  1360. </rss>`
  1361. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1362. if err != nil {
  1363. t.Fatal(err)
  1364. }
  1365. if len(feed.Entries) != 1 {
  1366. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1367. }
  1368. expected := "Episode Subtitle"
  1369. result := feed.Entries[0].Content
  1370. if expected != result {
  1371. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1372. }
  1373. }
  1374. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  1375. data := `<?xml version="1.0" encoding="UTF-8"?>
  1376. <rss version="2.0"
  1377. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1378. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1379. <channel>
  1380. <title>Podcast Example</title>
  1381. <link>http://www.example.com/index.html</link>
  1382. <item>
  1383. <title>Podcast Episode</title>
  1384. <guid>http://example.com/episode.m4a</guid>
  1385. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1386. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1387. <googleplay:description>Episode Description</googleplay:description>
  1388. </item>
  1389. </channel>
  1390. </rss>`
  1391. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1392. if err != nil {
  1393. t.Fatal(err)
  1394. }
  1395. if len(feed.Entries) != 1 {
  1396. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1397. }
  1398. expected := "Episode Description"
  1399. result := feed.Entries[0].Content
  1400. if expected != result {
  1401. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1402. }
  1403. }
  1404. func TestParseEntryWithRSSDescriptionAndMediaDescription(t *testing.T) {
  1405. data := `<?xml version="1.0" encoding="UTF-8"?>
  1406. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  1407. <channel>
  1408. <title>Podcast Example</title>
  1409. <link>http://www.example.com/index.html</link>
  1410. <item>
  1411. <title>Entry Title</title>
  1412. <link>http://www.example.com/entries/1</link>
  1413. <description>Entry Description</description>
  1414. <media:description type="plain">Media Description</media:description>
  1415. </item>
  1416. </channel>
  1417. </rss>`
  1418. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1419. if err != nil {
  1420. t.Fatal(err)
  1421. }
  1422. if len(feed.Entries) != 1 {
  1423. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1424. }
  1425. expected := "Entry Description"
  1426. result := feed.Entries[0].Content
  1427. if expected != result {
  1428. t.Errorf(`Unexpected description, got %q instead of %q`, result, expected)
  1429. }
  1430. }
  1431. func TestParseFeedWithCategories(t *testing.T) {
  1432. data := `<?xml version="1.0" encoding="utf-8"?>
  1433. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1434. <channel>
  1435. <title>Example</title>
  1436. <link>https://example.org/</link>
  1437. <category>Category 1</category>
  1438. <category><![CDATA[Category 2]]></category>
  1439. <item>
  1440. <title>Test</title>
  1441. <link>https://example.org/item</link>
  1442. </item>
  1443. </channel>
  1444. </rss>`
  1445. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1446. if err != nil {
  1447. t.Fatal(err)
  1448. }
  1449. if len(feed.Entries[0].Tags) != 2 {
  1450. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1451. }
  1452. expected := []string{"Category 1", "Category 2"}
  1453. result := feed.Entries[0].Tags
  1454. for i, tag := range result {
  1455. if tag != expected[i] {
  1456. t.Errorf("Incorrect tag, got: %q", tag)
  1457. }
  1458. }
  1459. }
  1460. func TestParseEntryWithCategories(t *testing.T) {
  1461. data := `<?xml version="1.0" encoding="utf-8"?>
  1462. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  1463. <channel>
  1464. <title>Example</title>
  1465. <link>https://example.org/</link>
  1466. <category>Category 3</category>
  1467. <item>
  1468. <title>Test</title>
  1469. <link>https://example.org/item</link>
  1470. <category>Category 1</category>
  1471. <category><![CDATA[Category 2]]></category>
  1472. </item>
  1473. </channel>
  1474. </rss>`
  1475. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1476. if err != nil {
  1477. t.Fatal(err)
  1478. }
  1479. if len(feed.Entries[0].Tags) != 3 {
  1480. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1481. }
  1482. expected := []string{"Category 1", "Category 2", "Category 3"}
  1483. result := feed.Entries[0].Tags
  1484. for i, tag := range result {
  1485. if tag != expected[i] {
  1486. t.Errorf("Incorrect tag, got: %q", tag)
  1487. }
  1488. }
  1489. }
  1490. func TestParseFeedWithItunesCategories(t *testing.T) {
  1491. data := `<?xml version="1.0" encoding="utf-8"?>
  1492. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  1493. <channel>
  1494. <title>Example</title>
  1495. <link>https://example.org/</link>
  1496. <itunes:category text="Society &amp; Culture">
  1497. <itunes:category text="Documentary" />
  1498. </itunes:category>
  1499. <itunes:category text="Health">
  1500. <itunes:category text="Mental Health" />
  1501. </itunes:category>
  1502. <item>
  1503. <title>Test</title>
  1504. <link>https://example.org/item</link>
  1505. </item>
  1506. </channel>
  1507. </rss>`
  1508. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1509. if err != nil {
  1510. t.Fatal(err)
  1511. }
  1512. if len(feed.Entries[0].Tags) != 4 {
  1513. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1514. }
  1515. expected := []string{"Society & Culture", "Documentary", "Health", "Mental Health"}
  1516. result := feed.Entries[0].Tags
  1517. for i, tag := range result {
  1518. if tag != expected[i] {
  1519. t.Errorf("Incorrect tag, got: %q", tag)
  1520. }
  1521. }
  1522. }
  1523. func TestParseFeedWithGooglePlayCategory(t *testing.T) {
  1524. data := `<?xml version="1.0" encoding="utf-8"?>
  1525. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0">
  1526. <channel>
  1527. <title>Example</title>
  1528. <link>https://example.org/</link>
  1529. <gplay:category text="Art"></gplay:category>
  1530. <item>
  1531. <title>Test</title>
  1532. <link>https://example.org/item</link>
  1533. </item>
  1534. </channel>
  1535. </rss>`
  1536. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1537. if err != nil {
  1538. t.Fatal(err)
  1539. }
  1540. if len(feed.Entries[0].Tags) != 1 {
  1541. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1542. }
  1543. expected := []string{"Art"}
  1544. result := feed.Entries[0].Tags
  1545. for i, tag := range result {
  1546. if tag != expected[i] {
  1547. t.Errorf("Incorrect tag, got: %q", tag)
  1548. }
  1549. }
  1550. }
  1551. func TestParseEntryWithMediaCategories(t *testing.T) {
  1552. data := `<?xml version="1.0" encoding="utf-8"?>
  1553. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  1554. <channel>
  1555. <title>Example</title>
  1556. <link>https://example.org/</link>
  1557. <item>
  1558. <title>Test</title>
  1559. <link>https://example.org/item</link>
  1560. <media:category label="Visual Art">visual_art</media:category>
  1561. <media:category scheme="http://search.yahoo.com/mrss/category_ schema">music/artist/album/song</media:category>
  1562. <media:category scheme="urn:flickr:tags">ycantpark mobile</media:category>
  1563. <media:category scheme="http://dmoz.org" label="Ace Ventura - Pet Detective">Arts/Movies/Titles/A/Ace_Ventura_Series/Ace_Ventura_ -_Pet_Detective</media:category>
  1564. </item>
  1565. </channel>
  1566. </rss>`
  1567. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1568. if err != nil {
  1569. t.Fatal(err)
  1570. }
  1571. if len(feed.Entries[0].Tags) != 2 {
  1572. t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
  1573. }
  1574. expected := []string{"Visual Art", "Ace Ventura - Pet Detective"}
  1575. result := feed.Entries[0].Tags
  1576. for i, tag := range result {
  1577. if tag != expected[i] {
  1578. t.Errorf("Incorrect tag, got: %q", tag)
  1579. }
  1580. }
  1581. }
  1582. func TestParseFeedWithTTLField(t *testing.T) {
  1583. data := `<?xml version="1.0" encoding="utf-8"?>
  1584. <rss version="2.0">
  1585. <channel>
  1586. <title>Example</title>
  1587. <link>https://example.org/</link>
  1588. <ttl>60</ttl>
  1589. <item>
  1590. <title>Test</title>
  1591. <link>https://example.org/item</link>
  1592. </item>
  1593. </channel>
  1594. </rss>`
  1595. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1596. if err != nil {
  1597. t.Fatal(err)
  1598. }
  1599. if feed.TTL != 60 {
  1600. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1601. }
  1602. }
  1603. func TestParseFeedWithIncorrectTTLValue(t *testing.T) {
  1604. data := `<?xml version="1.0" encoding="utf-8"?>
  1605. <rss version="2.0">
  1606. <channel>
  1607. <title>Example</title>
  1608. <link>https://example.org/</link>
  1609. <ttl>invalid</ttl>
  1610. <item>
  1611. <title>Test</title>
  1612. <link>https://example.org/item</link>
  1613. </item>
  1614. </channel>
  1615. </rss>`
  1616. feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
  1617. if err != nil {
  1618. t.Fatal(err)
  1619. }
  1620. if feed.TTL != 0 {
  1621. t.Errorf("Incorrect TTL, got: %d", feed.TTL)
  1622. }
  1623. }