parser_test.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 TestParseEntryWithInvalidCommentsURL(t *testing.T) {
  734. data := `<?xml version="1.0" encoding="utf-8"?>
  735. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  736. <channel>
  737. <link>https://example.org/</link>
  738. <item>
  739. <title>Item 1</title>
  740. <link>https://example.org/item1</link>
  741. <comments>
  742. Some text
  743. </comments>
  744. </item>
  745. </channel>
  746. </rss>`
  747. feed, err := Parse(bytes.NewBufferString(data))
  748. if err != nil {
  749. t.Fatal(err)
  750. }
  751. if feed.Entries[0].CommentsURL != "" {
  752. t.Errorf("Incorrect entry comments URL, got: %q", feed.Entries[0].CommentsURL)
  753. }
  754. }
  755. func TestParseInvalidXml(t *testing.T) {
  756. data := `garbage`
  757. _, err := Parse(bytes.NewBufferString(data))
  758. if err == nil {
  759. t.Error("Parse should returns an error")
  760. }
  761. }
  762. func TestParseWithHTMLEntity(t *testing.T) {
  763. data := `<?xml version="1.0" encoding="utf-8"?>
  764. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  765. <channel>
  766. <link>https://example.org/</link>
  767. <title>Example &nbsp; Feed</title>
  768. </channel>
  769. </rss>`
  770. feed, err := Parse(bytes.NewBufferString(data))
  771. if err != nil {
  772. t.Fatal(err)
  773. }
  774. if feed.Title != "Example \u00a0 Feed" {
  775. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  776. }
  777. }
  778. func TestParseWithInvalidCharacterEntity(t *testing.T) {
  779. data := `<?xml version="1.0" encoding="utf-8"?>
  780. <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  781. <channel>
  782. <link>https://example.org/a&b</link>
  783. <title>Example Feed</title>
  784. </channel>
  785. </rss>`
  786. feed, err := Parse(bytes.NewBufferString(data))
  787. if err != nil {
  788. t.Fatal(err)
  789. }
  790. if feed.SiteURL != "https://example.org/a&b" {
  791. t.Errorf(`Incorrect url, got: %q`, feed.SiteURL)
  792. }
  793. }
  794. func TestParseEntryWithMediaGroup(t *testing.T) {
  795. data := `<?xml version="1.0" encoding="utf-8"?>
  796. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  797. <channel>
  798. <title>My Example Feed</title>
  799. <link>http://example.org</link>
  800. <item>
  801. <title>Example Item</title>
  802. <link>http://www.example.org/entries/1</link>
  803. <enclosure type="application/x-bittorrent" url="https://example.org/file3.torrent" length="670053113">
  804. </enclosure>
  805. <media:group>
  806. <media:content type="application/x-bittorrent" url="https://example.org/file1.torrent"></media:content>
  807. <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content>
  808. <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content>
  809. <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content>
  810. <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content>
  811. <media:rating>nonadult</media:rating>
  812. </media:group>
  813. <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail>
  814. </item>
  815. </channel>
  816. </rss>`
  817. feed, err := Parse(bytes.NewBufferString(data))
  818. if err != nil {
  819. t.Fatal(err)
  820. }
  821. if len(feed.Entries) != 1 {
  822. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  823. }
  824. if len(feed.Entries[0].Enclosures) != 6 {
  825. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  826. }
  827. expectedResults := []struct {
  828. url string
  829. mimeType string
  830. size int64
  831. }{
  832. {"https://example.org/image.jpg", "image/*", 0},
  833. {"https://example.org/file3.torrent", "application/x-bittorrent", 670053113},
  834. {"https://example.org/file1.torrent", "application/x-bittorrent", 0},
  835. {"https://example.org/file2.torrent", "application/x-bittorrent", 0},
  836. {"https://example.org/file4.torrent", "application/x-bittorrent", 0},
  837. {"https://example.org/file5.torrent", "application/x-bittorrent", 42},
  838. }
  839. for index, enclosure := range feed.Entries[0].Enclosures {
  840. if expectedResults[index].url != enclosure.URL {
  841. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  842. }
  843. if expectedResults[index].mimeType != enclosure.MimeType {
  844. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  845. }
  846. if expectedResults[index].size != enclosure.Size {
  847. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  848. }
  849. }
  850. }
  851. func TestParseEntryWithMediaContent(t *testing.T) {
  852. data := `<?xml version="1.0" encoding="utf-8"?>
  853. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  854. <channel>
  855. <title>My Example Feed</title>
  856. <link>http://example.org</link>
  857. <item>
  858. <title>Example Item</title>
  859. <link>http://www.example.org/entries/1</link>
  860. <media:thumbnail url="https://example.org/thumbnail.jpg" />
  861. <media:content url="https://example.org/media1.jpg" medium="image">
  862. <media:title type="html">Some Title for Media 1</media:title>
  863. </media:content>
  864. <media:content url="https://example.org/media2.jpg" medium="image" />
  865. </item>
  866. </channel>
  867. </rss>`
  868. feed, err := Parse(bytes.NewBufferString(data))
  869. if err != nil {
  870. t.Fatal(err)
  871. }
  872. if len(feed.Entries) != 1 {
  873. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  874. }
  875. if len(feed.Entries[0].Enclosures) != 3 {
  876. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  877. }
  878. expectedResults := []struct {
  879. url string
  880. mimeType string
  881. size int64
  882. }{
  883. {"https://example.org/thumbnail.jpg", "image/*", 0},
  884. {"https://example.org/media1.jpg", "image/*", 0},
  885. {"https://example.org/media2.jpg", "image/*", 0},
  886. }
  887. for index, enclosure := range feed.Entries[0].Enclosures {
  888. if expectedResults[index].url != enclosure.URL {
  889. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  890. }
  891. if expectedResults[index].mimeType != enclosure.MimeType {
  892. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  893. }
  894. if expectedResults[index].size != enclosure.Size {
  895. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  896. }
  897. }
  898. }
  899. func TestParseEntryWithMediaPeerLink(t *testing.T) {
  900. data := `<?xml version="1.0" encoding="utf-8"?>
  901. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  902. <channel>
  903. <title>My Example Feed</title>
  904. <link>http://example.org</link>
  905. <item>
  906. <title>Example Item</title>
  907. <link>http://www.example.org/entries/1</link>
  908. <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" />
  909. </item>
  910. </channel>
  911. </rss>`
  912. feed, err := Parse(bytes.NewBufferString(data))
  913. if err != nil {
  914. t.Fatal(err)
  915. }
  916. if len(feed.Entries) != 1 {
  917. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  918. }
  919. if len(feed.Entries[0].Enclosures) != 1 {
  920. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  921. }
  922. expectedResults := []struct {
  923. url string
  924. mimeType string
  925. size int64
  926. }{
  927. {"http://www.example.org/file.torrent", "application/x-bittorrent", 0},
  928. }
  929. for index, enclosure := range feed.Entries[0].Enclosures {
  930. if expectedResults[index].url != enclosure.URL {
  931. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  932. }
  933. if expectedResults[index].mimeType != enclosure.MimeType {
  934. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  935. }
  936. if expectedResults[index].size != enclosure.Size {
  937. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  938. }
  939. }
  940. }
  941. func TestEntryDescriptionFromItunesSummary(t *testing.T) {
  942. data := `<?xml version="1.0" encoding="UTF-8"?>
  943. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  944. <channel>
  945. <title>Podcast Example</title>
  946. <link>http://www.example.com/index.html</link>
  947. <item>
  948. <title>Podcast Episode</title>
  949. <guid>http://example.com/episode.m4a</guid>
  950. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  951. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  952. <itunes:summary>Episode Summary</itunes:summary>
  953. </item>
  954. </channel>
  955. </rss>`
  956. feed, err := Parse(bytes.NewBufferString(data))
  957. if err != nil {
  958. t.Fatal(err)
  959. }
  960. if len(feed.Entries) != 1 {
  961. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  962. }
  963. expected := "Episode Summary"
  964. result := feed.Entries[0].Content
  965. if expected != result {
  966. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  967. }
  968. }
  969. func TestEntryDescriptionFromItunesSubtitle(t *testing.T) {
  970. data := `<?xml version="1.0" encoding="UTF-8"?>
  971. <rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  972. <channel>
  973. <title>Podcast Example</title>
  974. <link>http://www.example.com/index.html</link>
  975. <item>
  976. <title>Podcast Episode</title>
  977. <guid>http://example.com/episode.m4a</guid>
  978. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  979. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  980. </item>
  981. </channel>
  982. </rss>`
  983. feed, err := Parse(bytes.NewBufferString(data))
  984. if err != nil {
  985. t.Fatal(err)
  986. }
  987. if len(feed.Entries) != 1 {
  988. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  989. }
  990. expected := "Episode Subtitle"
  991. result := feed.Entries[0].Content
  992. if expected != result {
  993. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  994. }
  995. }
  996. func TestEntryDescriptionFromGooglePlayDescription(t *testing.T) {
  997. data := `<?xml version="1.0" encoding="UTF-8"?>
  998. <rss version="2.0"
  999. xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"
  1000. xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  1001. <channel>
  1002. <title>Podcast Example</title>
  1003. <link>http://www.example.com/index.html</link>
  1004. <item>
  1005. <title>Podcast Episode</title>
  1006. <guid>http://example.com/episode.m4a</guid>
  1007. <pubDate>Tue, 08 Mar 2016 12:00:00 GMT</pubDate>
  1008. <itunes:subtitle>Episode Subtitle</itunes:subtitle>
  1009. <googleplay:description>Episode Description</googleplay:description>
  1010. </item>
  1011. </channel>
  1012. </rss>`
  1013. feed, err := Parse(bytes.NewBufferString(data))
  1014. if err != nil {
  1015. t.Fatal(err)
  1016. }
  1017. if len(feed.Entries) != 1 {
  1018. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  1019. }
  1020. expected := "Episode Description"
  1021. result := feed.Entries[0].Content
  1022. if expected != result {
  1023. t.Errorf(`Unexpected podcast content, got %q instead of %q`, result, expected)
  1024. }
  1025. }