parser_test.go 40 KB

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