parser_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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(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 != "" {
  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(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(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 TestParseEntryWithoutLink(t *testing.T) {
  120. data := `<?xml version="1.0" encoding="utf-8"?>
  121. <rss version="2.0">
  122. <channel>
  123. <link>https://example.org/</link>
  124. <item>
  125. <guid isPermaLink="false">1234</guid>
  126. </item>
  127. </channel>
  128. </rss>`
  129. feed, err := Parse(bytes.NewBufferString(data))
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if feed.Entries[0].URL != "https://example.org/" {
  134. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  135. }
  136. if feed.Entries[0].Hash != "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4" {
  137. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  138. }
  139. }
  140. func TestParseEntryWithAtomLink(t *testing.T) {
  141. data := `<?xml version="1.0" encoding="utf-8"?>
  142. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  143. <channel>
  144. <link>https://example.org/</link>
  145. <item>
  146. <title>Test</title>
  147. <atom:link href="https://example.org/item" />
  148. </item>
  149. </channel>
  150. </rss>`
  151. feed, err := Parse(bytes.NewBufferString(data))
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. if feed.Entries[0].URL != "https://example.org/item" {
  156. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  157. }
  158. }
  159. func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
  160. data := `<?xml version="1.0" encoding="utf-8"?>
  161. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  162. <channel>
  163. <link>https://example.org/</link>
  164. <item>
  165. <title>Test</title>
  166. <atom:link rel="payment" href="https://example.org/a" />
  167. <atom:link rel="http://foobar.tld" href="https://example.org/b" />
  168. </item>
  169. </channel>
  170. </rss>`
  171. feed, err := Parse(bytes.NewBufferString(data))
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if feed.Entries[0].URL != "https://example.org/b" {
  176. t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
  177. }
  178. }
  179. func TestParseFeedURLWithAtomLink(t *testing.T) {
  180. data := `<?xml version="1.0" encoding="utf-8"?>
  181. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  182. <channel>
  183. <title>Example</title>
  184. <link>https://example.org/</link>
  185. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  186. </channel>
  187. </rss>`
  188. feed, err := Parse(bytes.NewBufferString(data))
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. if feed.FeedURL != "https://example.org/rss" {
  193. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  194. }
  195. if feed.SiteURL != "https://example.org/" {
  196. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  197. }
  198. }
  199. func TestParseFeedWithWebmaster(t *testing.T) {
  200. data := `<?xml version="1.0" encoding="utf-8"?>
  201. <rss version="2.0">
  202. <channel>
  203. <title>Example</title>
  204. <link>https://example.org/</link>
  205. <webMaster>webmaster@example.com</webMaster>
  206. <item>
  207. <title>Test</title>
  208. <link>https://example.org/item</link>
  209. </item>
  210. </channel>
  211. </rss>`
  212. feed, err := Parse(bytes.NewBufferString(data))
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. expected := "webmaster@example.com"
  217. result := feed.Entries[0].Author
  218. if result != expected {
  219. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  220. }
  221. }
  222. func TestParseFeedWithManagingEditor(t *testing.T) {
  223. data := `<?xml version="1.0" encoding="utf-8"?>
  224. <rss version="2.0">
  225. <channel>
  226. <title>Example</title>
  227. <link>https://example.org/</link>
  228. <webMaster>webmaster@example.com</webMaster>
  229. <managingEditor>editor@example.com</managingEditor>
  230. <item>
  231. <title>Test</title>
  232. <link>https://example.org/item</link>
  233. </item>
  234. </channel>
  235. </rss>`
  236. feed, err := Parse(bytes.NewBufferString(data))
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. expected := "editor@example.com"
  241. result := feed.Entries[0].Author
  242. if result != expected {
  243. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  244. }
  245. }
  246. func TestParseEntryWithAuthorAndInnerHTML(t *testing.T) {
  247. data := `<?xml version="1.0" encoding="utf-8"?>
  248. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  249. <channel>
  250. <title>Example</title>
  251. <link>https://example.org/</link>
  252. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  253. <item>
  254. <title>Test</title>
  255. <link>https://example.org/item</link>
  256. <author>by <a itemprop="url" class="author" rel="author" href="/author/foobar">Foo Bar</a></author>
  257. </item>
  258. </channel>
  259. </rss>`
  260. feed, err := Parse(bytes.NewBufferString(data))
  261. if err != nil {
  262. t.Fatal(err)
  263. }
  264. expected := "by Foo Bar"
  265. result := feed.Entries[0].Author
  266. if result != expected {
  267. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  268. }
  269. }
  270. func TestParseEntryWithNonStandardAtomAuthor(t *testing.T) {
  271. data := `<?xml version="1.0" encoding="utf-8"?>
  272. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  273. <channel>
  274. <title>Example</title>
  275. <link>https://example.org/</link>
  276. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  277. <item>
  278. <title>Test</title>
  279. <link>https://example.org/item</link>
  280. <author xmlns:author="http://www.w3.org/2005/Atom">
  281. <name>Foo Bar</name>
  282. <title>Vice President</title>
  283. <department/>
  284. <company>FooBar Inc.</company>
  285. </author>
  286. </item>
  287. </channel>
  288. </rss>`
  289. feed, err := Parse(bytes.NewBufferString(data))
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. expected := "Foo Bar"
  294. result := feed.Entries[0].Author
  295. if result != expected {
  296. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  297. }
  298. }
  299. func TestParseEntryWithAtomAuthorEmail(t *testing.T) {
  300. data := `<?xml version="1.0" encoding="utf-8"?>
  301. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  302. <channel>
  303. <title>Example</title>
  304. <link>https://example.org/</link>
  305. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  306. <item>
  307. <title>Test</title>
  308. <link>https://example.org/item</link>
  309. <atom:author>
  310. <email>author@example.org</email>
  311. </atom:author>
  312. </item>
  313. </channel>
  314. </rss>`
  315. feed, err := Parse(bytes.NewBufferString(data))
  316. if err != nil {
  317. t.Fatal(err)
  318. }
  319. expected := "author@example.org"
  320. result := feed.Entries[0].Author
  321. if result != expected {
  322. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  323. }
  324. }
  325. func TestParseEntryWithAtomAuthor(t *testing.T) {
  326. data := `<?xml version="1.0" encoding="utf-8"?>
  327. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  328. <channel>
  329. <title>Example</title>
  330. <link>https://example.org/</link>
  331. <atom:link href="https://example.org/rss" type="application/rss+xml" rel="self"></atom:link>
  332. <item>
  333. <title>Test</title>
  334. <link>https://example.org/item</link>
  335. <atom:author>
  336. <name>Foo Bar</name>
  337. </atom:author>
  338. </item>
  339. </channel>
  340. </rss>`
  341. feed, err := Parse(bytes.NewBufferString(data))
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. expected := "Foo Bar"
  346. result := feed.Entries[0].Author
  347. if result != expected {
  348. t.Errorf("Incorrect entry author, got: %q instead of %q", result, expected)
  349. }
  350. }
  351. func TestParseEntryWithDublinCoreAuthor(t *testing.T) {
  352. data := `<?xml version="1.0" encoding="utf-8"?>
  353. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  354. <channel>
  355. <title>Example</title>
  356. <link>https://example.org/</link>
  357. <item>
  358. <title>Test</title>
  359. <link>https://example.org/item</link>
  360. <dc:creator>Me (me@example.com)</dc:creator>
  361. </item>
  362. </channel>
  363. </rss>`
  364. feed, err := Parse(bytes.NewBufferString(data))
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. expected := "Me (me@example.com)"
  369. result := feed.Entries[0].Author
  370. if result != expected {
  371. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  372. }
  373. }
  374. func TestParseEntryWithItunesAuthor(t *testing.T) {
  375. data := `<?xml version="1.0" encoding="utf-8"?>
  376. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  377. <channel>
  378. <title>Example</title>
  379. <link>https://example.org/</link>
  380. <item>
  381. <title>Test</title>
  382. <link>https://example.org/item</link>
  383. <itunes:author>Someone</itunes:author>
  384. </item>
  385. </channel>
  386. </rss>`
  387. feed, err := Parse(bytes.NewBufferString(data))
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. expected := "Someone"
  392. result := feed.Entries[0].Author
  393. if result != expected {
  394. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  395. }
  396. }
  397. func TestParseFeedWithItunesAuthor(t *testing.T) {
  398. data := `<?xml version="1.0" encoding="utf-8"?>
  399. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  400. <channel>
  401. <title>Example</title>
  402. <link>https://example.org/</link>
  403. <itunes:author>Someone</itunes:author>
  404. <item>
  405. <title>Test</title>
  406. <link>https://example.org/item</link>
  407. </item>
  408. </channel>
  409. </rss>`
  410. feed, err := Parse(bytes.NewBufferString(data))
  411. if err != nil {
  412. t.Fatal(err)
  413. }
  414. expected := "Someone"
  415. result := feed.Entries[0].Author
  416. if result != expected {
  417. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  418. }
  419. }
  420. func TestParseFeedWithItunesOwner(t *testing.T) {
  421. data := `<?xml version="1.0" encoding="utf-8"?>
  422. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  423. <channel>
  424. <title>Example</title>
  425. <link>https://example.org/</link>
  426. <itunes:owner>
  427. <itunes:name>John Doe</itunes:name>
  428. <itunes:email>john.doe@example.com</itunes:email>
  429. </itunes:owner>
  430. <item>
  431. <title>Test</title>
  432. <link>https://example.org/item</link>
  433. </item>
  434. </channel>
  435. </rss>`
  436. feed, err := Parse(bytes.NewBufferString(data))
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. expected := "John Doe"
  441. result := feed.Entries[0].Author
  442. if result != expected {
  443. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  444. }
  445. }
  446. func TestParseFeedWithItunesOwnerEmail(t *testing.T) {
  447. data := `<?xml version="1.0" encoding="utf-8"?>
  448. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  449. <channel>
  450. <title>Example</title>
  451. <link>https://example.org/</link>
  452. <itunes:owner>
  453. <itunes:email>john.doe@example.com</itunes:email>
  454. </itunes:owner>
  455. <item>
  456. <title>Test</title>
  457. <link>https://example.org/item</link>
  458. </item>
  459. </channel>
  460. </rss>`
  461. feed, err := Parse(bytes.NewBufferString(data))
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. expected := "john.doe@example.com"
  466. result := feed.Entries[0].Author
  467. if result != expected {
  468. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  469. }
  470. }
  471. func TestParseEntryWithGooglePlayAuthor(t *testing.T) {
  472. data := `<?xml version="1.0" encoding="utf-8"?>
  473. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  474. <channel>
  475. <title>Example</title>
  476. <link>https://example.org/</link>
  477. <item>
  478. <title>Test</title>
  479. <link>https://example.org/item</link>
  480. <googleplay:author>Someone</googleplay:author>
  481. </item>
  482. </channel>
  483. </rss>`
  484. feed, err := Parse(bytes.NewBufferString(data))
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. expected := "Someone"
  489. result := feed.Entries[0].Author
  490. if result != expected {
  491. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  492. }
  493. }
  494. func TestParseFeedWithGooglePlayAuthor(t *testing.T) {
  495. data := `<?xml version="1.0" encoding="utf-8"?>
  496. <rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0">
  497. <channel>
  498. <title>Example</title>
  499. <link>https://example.org/</link>
  500. <googleplay:author>Someone</googleplay:author>
  501. <item>
  502. <title>Test</title>
  503. <link>https://example.org/item</link>
  504. </item>
  505. </channel>
  506. </rss>`
  507. feed, err := Parse(bytes.NewBufferString(data))
  508. if err != nil {
  509. t.Fatal(err)
  510. }
  511. expected := "Someone"
  512. result := feed.Entries[0].Author
  513. if result != expected {
  514. t.Errorf("Incorrect entry author, got %q instead of %q", result, expected)
  515. }
  516. }
  517. func TestParseEntryWithDublinCoreDate(t *testing.T) {
  518. data := `<?xml version="1.0" encoding="utf-8"?>
  519. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  520. <channel>
  521. <title>Example</title>
  522. <link>http://example.org/</link>
  523. <item>
  524. <title>Item 1</title>
  525. <link>http://example.org/item1</link>
  526. <description>Description.</description>
  527. <guid isPermaLink="false">UUID</guid>
  528. <dc:date>2002-09-29T23:40:06-05:00</dc:date>
  529. </item>
  530. </channel>
  531. </rss>`
  532. feed, err := Parse(bytes.NewBufferString(data))
  533. if err != nil {
  534. t.Fatal(err)
  535. }
  536. location, _ := time.LoadLocation("EST")
  537. expectedDate := time.Date(2002, time.September, 29, 23, 40, 06, 0, location)
  538. if !feed.Entries[0].Date.Equal(expectedDate) {
  539. t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
  540. }
  541. }
  542. func TestParseEntryWithContentEncoded(t *testing.T) {
  543. data := `<?xml version="1.0" encoding="utf-8"?>
  544. <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  545. <channel>
  546. <title>Example</title>
  547. <link>http://example.org/</link>
  548. <item>
  549. <title>Item 1</title>
  550. <link>http://example.org/item1</link>
  551. <description>Description.</description>
  552. <guid isPermaLink="false">UUID</guid>
  553. <content:encoded><![CDATA[<p><a href="http://www.example.org/">Example</a>.</p>]]></content:encoded>
  554. </item>
  555. </channel>
  556. </rss>`
  557. feed, err := Parse(bytes.NewBufferString(data))
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. if feed.Entries[0].Content != `<p><a href="http://www.example.org/">Example</a>.</p>` {
  562. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  563. }
  564. }
  565. func TestParseEntryWithFeedBurnerLink(t *testing.T) {
  566. data := `<?xml version="1.0" encoding="utf-8"?>
  567. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  568. <channel>
  569. <title>Example</title>
  570. <link>http://example.org/</link>
  571. <item>
  572. <title>Item 1</title>
  573. <link>http://example.org/item1</link>
  574. <feedburner:origLink>http://example.org/original</feedburner:origLink>
  575. </item>
  576. </channel>
  577. </rss>`
  578. feed, err := Parse(bytes.NewBufferString(data))
  579. if err != nil {
  580. t.Fatal(err)
  581. }
  582. if feed.Entries[0].URL != "http://example.org/original" {
  583. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].URL)
  584. }
  585. }
  586. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  587. data := `<?xml version="1.0" encoding="utf-8"?>
  588. <rss version="2.0">
  589. <channel>
  590. <title>Example</title>
  591. <link>http://example.org</link>
  592. <item>
  593. <title>
  594. Some Title
  595. </title>
  596. <link>http://www.example.org/entries/1</link>
  597. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  598. </item>
  599. </channel>
  600. </rss>`
  601. feed, err := Parse(bytes.NewBufferString(data))
  602. if err != nil {
  603. t.Fatal(err)
  604. }
  605. if feed.Entries[0].Title != "Some Title" {
  606. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  607. }
  608. }
  609. func TestParseEntryWithEnclosures(t *testing.T) {
  610. data := `<?xml version="1.0" encoding="utf-8"?>
  611. <rss version="2.0">
  612. <channel>
  613. <title>My Podcast Feed</title>
  614. <link>http://example.org</link>
  615. <author>some.email@example.org</author>
  616. <item>
  617. <title>Podcasting with RSS</title>
  618. <link>http://www.example.org/entries/1</link>
  619. <description>An overview of RSS podcasting</description>
  620. <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate>
  621. <guid isPermaLink="true">http://www.example.org/entries/1</guid>
  622. <enclosure url="http://www.example.org/myaudiofile.mp3"
  623. length="12345"
  624. type="audio/mpeg" />
  625. </item>
  626. </channel>
  627. </rss>`
  628. feed, err := Parse(bytes.NewBufferString(data))
  629. if err != nil {
  630. t.Fatal(err)
  631. }
  632. if len(feed.Entries) != 1 {
  633. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  634. }
  635. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  636. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  637. }
  638. if len(feed.Entries[0].Enclosures) != 1 {
  639. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  640. }
  641. if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" {
  642. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  643. }
  644. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  645. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  646. }
  647. if feed.Entries[0].Enclosures[0].Size != 12345 {
  648. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  649. }
  650. }
  651. func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
  652. data := `<?xml version="1.0" encoding="utf-8"?>
  653. <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  654. <channel>
  655. <title>My Example Feed</title>
  656. <link>http://example.org</link>
  657. <author>some.email@example.org</author>
  658. <item>
  659. <title>Example Item</title>
  660. <link>http://www.example.org/entries/1</link>
  661. <enclosure
  662. url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3"
  663. length="76192460"
  664. type="audio/mpeg" />
  665. <feedburner:origEnclosureLink>http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink>
  666. </item>
  667. </channel>
  668. </rss>`
  669. feed, err := Parse(bytes.NewBufferString(data))
  670. if err != nil {
  671. t.Fatal(err)
  672. }
  673. if len(feed.Entries) != 1 {
  674. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  675. }
  676. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  677. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  678. }
  679. if len(feed.Entries[0].Enclosures) != 1 {
  680. t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  681. }
  682. if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" {
  683. t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL)
  684. }
  685. if feed.Entries[0].Enclosures[0].MimeType != "audio/mpeg" {
  686. t.Errorf("Incorrect enclosure type, got: %s", feed.Entries[0].Enclosures[0].MimeType)
  687. }
  688. if feed.Entries[0].Enclosures[0].Size != 76192460 {
  689. t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
  690. }
  691. }
  692. func TestParseEntryWithRelativeURL(t *testing.T) {
  693. data := `<?xml version="1.0" encoding="utf-8"?>
  694. <rss version="2.0">
  695. <channel>
  696. <link>https://example.org/</link>
  697. <item>
  698. <link>item.html</link>
  699. </item>
  700. </channel>
  701. </rss>`
  702. feed, err := Parse(bytes.NewBufferString(data))
  703. if err != nil {
  704. t.Fatal(err)
  705. }
  706. if feed.Entries[0].Title != "https://example.org/item.html" {
  707. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  708. }
  709. }
  710. func TestParseEntryWithCommentsURL(t *testing.T) {
  711. data := `<?xml version="1.0" encoding="utf-8"?>
  712. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  713. <channel>
  714. <link>https://example.org/</link>
  715. <item>
  716. <title>Item 1</title>
  717. <link>https://example.org/item1</link>
  718. <comments>
  719. https://example.org/comments
  720. </comments>
  721. <slash:comments>42</slash:comments>
  722. </item>
  723. </channel>
  724. </rss>`
  725. feed, err := Parse(bytes.NewBufferString(data))
  726. if err != nil {
  727. t.Fatal(err)
  728. }
  729. if feed.Entries[0].CommentsURL != "https://example.org/comments" {
  730. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  731. }
  732. }
  733. func TestParseInvalidXml(t *testing.T) {
  734. data := `garbage`
  735. _, err := Parse(bytes.NewBufferString(data))
  736. if err == nil {
  737. t.Error("Parse should returns an error")
  738. }
  739. }
  740. func TestParseWithHTMLEntity(t *testing.T) {
  741. data := `<?xml version="1.0" encoding="utf-8"?>
  742. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  743. <channel>
  744. <link>https://example.org/</link>
  745. <title>Example &nbsp; Feed</title>
  746. </channel>
  747. </rss>`
  748. feed, err := Parse(bytes.NewBufferString(data))
  749. if err != nil {
  750. t.Fatal(err)
  751. }
  752. if feed.Title != "Example \u00a0 Feed" {
  753. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  754. }
  755. }
  756. func TestParseWithInvalidCharacterEntity(t *testing.T) {
  757. data := `<?xml version="1.0" encoding="utf-8"?>
  758. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  759. <channel>
  760. <link>https://example.org/a&b</link>
  761. <title>Example Feed</title>
  762. </channel>
  763. </rss>`
  764. feed, err := Parse(bytes.NewBufferString(data))
  765. if err != nil {
  766. t.Fatal(err)
  767. }
  768. if feed.SiteURL != "https://example.org/a&b" {
  769. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  770. }
  771. }
  772. func TestParseEntryWithMediaGroup(t *testing.T) {
  773. data := `<?xml version="1.0" encoding="utf-8"?>
  774. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  775. <channel>
  776. <title>My Example Feed</title>
  777. <link>http://example.org</link>
  778. <item>
  779. <title>Example Item</title>
  780. <link>http://www.example.org/entries/1</link>
  781. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  782. </enclosure>
  783. <media:group>
  784. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  785. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  786. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  787. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  788. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  789. <media:rating>nonadult</media:rating>
  790. </media:group>
  791. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  792. </item>
  793. </channel>
  794. </rss>`
  795. feed, err := Parse(bytes.NewBufferString(data))
  796. if err != nil {
  797. t.Fatal(err)
  798. }
  799. if len(feed.Entries) != 1 {
  800. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  801. }
  802. if len(feed.Entries[0].Enclosures) != 6 {
  803. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  804. }
  805. expectedResults := []struct {
  806. url string
  807. mimeType string
  808. size int64
  809. }{
  810. {"https://example.org/image.jpg", "image/*", 0},
  811. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  812. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  813. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  814. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  815. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  816. }
  817. for index, enclosure := range feed.Entries[0].Enclosures {
  818. if expectedResults[index].url != enclosure.URL {
  819. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  820. }
  821. if expectedResults[index].mimeType != enclosure.MimeType {
  822. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  823. }
  824. if expectedResults[index].size != enclosure.Size {
  825. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  826. }
  827. }
  828. }
  829. func TestParseEntryWithMediaContent(t *testing.T) {
  830. data := `<?xml version="1.0" encoding="utf-8"?>
  831. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  832. <channel>
  833. <title>My Example Feed</title>
  834. <link>http://example.org</link>
  835. <item>
  836. <title>Example Item</title>
  837. <link>http://www.example.org/entries/1</link>
  838. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  839. <media:content url="https://example.org/media1.jpg" medium="image">
  840. <media:title type="html">Some Title for Media 1</media:title>
  841. </media:content>
  842. <media:content url="https://example.org/media2.jpg" medium="image" />
  843. </item>
  844. </channel>
  845. </rss>`
  846. feed, err := Parse(bytes.NewBufferString(data))
  847. if err != nil {
  848. t.Fatal(err)
  849. }
  850. if len(feed.Entries) != 1 {
  851. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  852. }
  853. if len(feed.Entries[0].Enclosures) != 3 {
  854. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  855. }
  856. expectedResults := []struct {
  857. url string
  858. mimeType string
  859. size int64
  860. }{
  861. {"https://example.org/thumbnail.jpg", "image/*", 0},
  862. {"https://example.org/media1.jpg", "image/*", 0},
  863. {"https://example.org/media2.jpg", "image/*", 0},
  864. }
  865. for index, enclosure := range feed.Entries[0].Enclosures {
  866. if expectedResults[index].url != enclosure.URL {
  867. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  868. }
  869. if expectedResults[index].mimeType != enclosure.MimeType {
  870. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  871. }
  872. if expectedResults[index].size != enclosure.Size {
  873. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  874. }
  875. }
  876. }
  877. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  878. data := `<?xml version="1.0" encoding="utf-8"?>
  879. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  880. <channel>
  881. <title>My Example Feed</title>
  882. <link>http://example.org</link>
  883. <item>
  884. <title>Example Item</title>
  885. <link>http://www.example.org/entries/1</link>
  886. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  887. </item>
  888. </channel>
  889. </rss>`
  890. feed, err := Parse(bytes.NewBufferString(data))
  891. if err != nil {
  892. t.Fatal(err)
  893. }
  894. if len(feed.Entries) != 1 {
  895. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  896. }
  897. if len(feed.Entries[0].Enclosures) != 1 {
  898. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  899. }
  900. expectedResults := []struct {
  901. url string
  902. mimeType string
  903. size int64
  904. }{
  905. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  906. }
  907. for index, enclosure := range feed.Entries[0].Enclosures {
  908. if expectedResults[index].url != enclosure.URL {
  909. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  910. }
  911. if expectedResults[index].mimeType != enclosure.MimeType {
  912. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  913. }
  914. if expectedResults[index].size != enclosure.Size {
  915. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  916. }
  917. }
  918. }
  919. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  920. data := `<?xml version="1.0" encoding="UTF-8"?>
  921. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  922. <channel>
  923. <title>Podcast Example</title>
  924. <link>http://www.example.com/index.html</link>
  925. <item>
  926. <title>Podcast Episode</title>
  927. <guid>http://example.com/episode.m4a</guid>
  928. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  929. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  930. <itunes:summary>Episode Summary</itunes:summary>
  931. </item>
  932. </channel>
  933. </rss>`
  934. feed, err := Parse(bytes.NewBufferString(data))
  935. if err != nil {
  936. t.Fatal(err)
  937. }
  938. if len(feed.Entries) != 1 {
  939. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  940. }
  941. expected := "Episode Summary"
  942. result := feed.Entries[0].Content
  943. if expected != result {
  944. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  945. }
  946. }
  947. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  948. data := `<?xml version="1.0" encoding="UTF-8"?>
  949. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  950. <channel>
  951. <title>Podcast Example</title>
  952. <link>http://www.example.com/index.html</link>
  953. <item>
  954. <title>Podcast Episode</title>
  955. <guid>http://example.com/episode.m4a</guid>
  956. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  957. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  958. </item>
  959. </channel>
  960. </rss>`
  961. feed, err := Parse(bytes.NewBufferString(data))
  962. if err != nil {
  963. t.Fatal(err)
  964. }
  965. if len(feed.Entries) != 1 {
  966. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  967. }
  968. expected := "Episode Subtitle"
  969. result := feed.Entries[0].Content
  970. if expected != result {
  971. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  972. }
  973. }
  974. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  975. data := `<?xml version="1.0" encoding="UTF-8"?>
  976. <rss version="2.0"
  977. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  978. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  979. <channel>
  980. <title>Podcast Example</title>
  981. <link>http://www.example.com/index.html</link>
  982. <item>
  983. <title>Podcast Episode</title>
  984. <guid>http://example.com/episode.m4a</guid>
  985. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  986. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  987. <googleplay:description>Episode Description</googleplay:description>
  988. </item>
  989. </channel>
  990. </rss>`
  991. feed, err := Parse(bytes.NewBufferString(data))
  992. if err != nil {
  993. t.Fatal(err)
  994. }
  995. if len(feed.Entries) != 1 {
  996. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  997. }
  998. expected := "Episode Description"
  999. result := feed.Entries[0].Content
  1000. if expected != result {
  1001. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1002. }
  1003. }