parser_test.go 42 KB

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