parser_test.go 44 KB

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