atom_10_test.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // Copyright 2019 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 atom // import "miniflux.app/reader/atom"
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. )
  10. func TestParseAtomSample(t *testing.T) {
  11. data := `<?xml version="1.0" encoding="utf-8"?>
  12. <feed xmlns="http://www.w3.org/2005/Atom">
  13. <title>Example Feed</title>
  14. <link href="http://example.org/"/>
  15. <updated>2003-12-13T18:30:02Z</updated>
  16. <author>
  17. <name>John Doe</name>
  18. </author>
  19. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  20. <entry>
  21. <title>Atom-Powered Robots Run Amok</title>
  22. <link href="http://example.org/2003/12/13/atom03"/>
  23. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  24. <updated>2003-12-13T18:30:02Z</updated>
  25. <summary>Some text.</summary>
  26. </entry>
  27. </feed>`
  28. feed, err := Parse("http://example.org/feed.xml", bytes.NewBufferString(data))
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if feed.Title != "Example Feed" {
  33. t.Errorf("Incorrect title, got: %s", feed.Title)
  34. }
  35. if feed.FeedURL != "http://example.org/feed.xml" {
  36. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  37. }
  38. if feed.SiteURL != "http://example.org/" {
  39. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  40. }
  41. if len(feed.Entries) != 1 {
  42. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  43. }
  44. if !feed.Entries[0].Date.Equal(time.Date(2003, time.December, 13, 18, 30, 2, 0, time.UTC)) {
  45. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  46. }
  47. if feed.Entries[0].Hash != "3841e5cf232f5111fc5841e9eba5f4b26d95e7d7124902e0f7272729d65601a6" {
  48. t.Errorf("Incorrect entry hash, got: %s", feed.Entries[0].Hash)
  49. }
  50. if feed.Entries[0].URL != "http://example.org/2003/12/13/atom03" {
  51. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  52. }
  53. if feed.Entries[0].CommentsURL != "" {
  54. t.Errorf("Incorrect entry Comments URL, got: %s", feed.Entries[0].CommentsURL)
  55. }
  56. if feed.Entries[0].Title != "Atom-Powered Robots Run Amok" {
  57. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  58. }
  59. if feed.Entries[0].Content != "Some text." {
  60. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  61. }
  62. if feed.Entries[0].Author != "John Doe" {
  63. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  64. }
  65. }
  66. func TestParseFeedWithoutTitle(t *testing.T) {
  67. data := `<?xml version="1.0" encoding="utf-8"?>
  68. <feed xmlns="http://www.w3.org/2005/Atom">
  69. <link rel="alternate" type="text/html" href="https://example.org/"/>
  70. <link rel="self" type="application/atom+xml" href="https://example.org/feed"/>
  71. <updated>2003-12-13T18:30:02Z</updated>
  72. </feed>`
  73. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if feed.Title != "https://example.org/" {
  78. t.Errorf("Incorrect feed title, got: %s", feed.Title)
  79. }
  80. }
  81. func TestParseEntryWithoutTitle(t *testing.T) {
  82. data := `<?xml version="1.0" encoding="utf-8"?>
  83. <feed xmlns="http://www.w3.org/2005/Atom">
  84. <title>Example Feed</title>
  85. <link href="http://example.org/"/>
  86. <updated>2003-12-13T18:30:02Z</updated>
  87. <author>
  88. <name>John Doe</name>
  89. </author>
  90. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  91. <entry>
  92. <link href="http://example.org/2003/12/13/atom03"/>
  93. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  94. <updated>2003-12-13T18:30:02Z</updated>
  95. <summary>Some text.</summary>
  96. </entry>
  97. </feed>`
  98. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if feed.Entries[0].Title != "http://example.org/2003/12/13/atom03" {
  103. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  104. }
  105. }
  106. func TestParseFeedURL(t *testing.T) {
  107. data := `<?xml version="1.0" encoding="utf-8"?>
  108. <feed xmlns="http://www.w3.org/2005/Atom">
  109. <title>Example Feed</title>
  110. <link rel="alternate" type="text/html" href="https://example.org/"/>
  111. <link rel="self" type="application/atom+xml" href="https://example.org/feed"/>
  112. <updated>2003-12-13T18:30:02Z</updated>
  113. </feed>`
  114. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if feed.SiteURL != "https://example.org/" {
  119. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  120. }
  121. if feed.FeedURL != "https://example.org/feed" {
  122. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  123. }
  124. }
  125. func TestParseFeedWithRelativeURL(t *testing.T) {
  126. data := `<?xml version="1.0" encoding="utf-8"?>
  127. <feed xmlns="http://www.w3.org/2005/Atom">
  128. <title>Example Feed</title>
  129. <link href="/blog/atom.xml" rel="self" type="application/atom+xml"/>
  130. <link href="/blog"/>
  131. <entry>
  132. <title>Test</title>
  133. <link href="/blog/article.html"/>
  134. <link href="/blog/article.html" rel="alternate" type="text/html"/>
  135. <id>/blog/article.html</id>
  136. <updated>2003-12-13T18:30:02Z</updated>
  137. <summary>Some text.</summary>
  138. </entry>
  139. </feed>`
  140. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if feed.FeedURL != "https://example.org/blog/atom.xml" {
  145. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  146. }
  147. if feed.SiteURL != "https://example.org/blog" {
  148. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  149. }
  150. if feed.Entries[0].URL != "https://example.org/blog/article.html" {
  151. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  152. }
  153. }
  154. func TestParseEntryWithRelativeURL(t *testing.T) {
  155. data := `<?xml version="1.0" encoding="utf-8"?>
  156. <feed xmlns="http://www.w3.org/2005/Atom">
  157. <title>Example Feed</title>
  158. <link href="http://example.org/"/>
  159. <entry>
  160. <title>Test</title>
  161. <link href="something.html"/>
  162. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  163. <updated>2003-12-13T18:30:02Z</updated>
  164. <summary>Some text.</summary>
  165. </entry>
  166. </feed>`
  167. feed, err := Parse("https://example.net/", bytes.NewBufferString(data))
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if feed.Entries[0].URL != "http://example.org/something.html" {
  172. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  173. }
  174. }
  175. func TestParseEntryTitleWithWhitespaces(t *testing.T) {
  176. data := `<?xml version="1.0" encoding="utf-8"?>
  177. <feed xmlns="http://www.w3.org/2005/Atom">
  178. <title>Example Feed</title>
  179. <link href="http://example.org/"/>
  180. <entry>
  181. <title>
  182. Some Title
  183. </title>
  184. <link href="http://example.org/2003/12/13/atom03"/>
  185. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  186. <updated>2003-12-13T18:30:02Z</updated>
  187. <summary>Some text.</summary>
  188. </entry>
  189. </feed>`
  190. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. if feed.Entries[0].Title != "Some Title" {
  195. t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
  196. }
  197. }
  198. func TestParseEntryTitleWithHTMLAndCDATA(t *testing.T) {
  199. data := `<?xml version="1.0" encoding="utf-8"?>
  200. <feed xmlns="http://www.w3.org/2005/Atom">
  201. <title>Example Feed</title>
  202. <link href="http://example.org/"/>
  203. <entry>
  204. <title type="html"><![CDATA[Test &#8220;Test&#8221;]]></title>
  205. <link href="http://example.org/2003/12/13/atom03"/>
  206. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  207. <updated>2003-12-13T18:30:02Z</updated>
  208. <summary>Some text.</summary>
  209. </entry>
  210. </feed>`
  211. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. if feed.Entries[0].Title != "Test “Test”" {
  216. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  217. }
  218. }
  219. func TestParseEntryTitleWithHTML(t *testing.T) {
  220. data := `<?xml version="1.0" encoding="utf-8"?>
  221. <feed xmlns="http://www.w3.org/2005/Atom">
  222. <title>Example Feed</title>
  223. <link href="http://example.org/"/>
  224. <entry>
  225. <title type="html">&lt;code&gt;Test&lt;/code&gt; Test</title>
  226. <link href="http://example.org/2003/12/13/atom03"/>
  227. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  228. <updated>2003-12-13T18:30:02Z</updated>
  229. <summary>Some text.</summary>
  230. </entry>
  231. </feed>`
  232. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. if feed.Entries[0].Title != "<code>Test</code> Test" {
  237. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  238. }
  239. }
  240. func TestParseEntryTitleWithXHTML(t *testing.T) {
  241. data := `<?xml version="1.0" encoding="utf-8"?>
  242. <feed xmlns="http://www.w3.org/2005/Atom">
  243. <title>Example Feed</title>
  244. <link href="http://example.org/"/>
  245. <entry>
  246. <title type="xhtml"><code>Test</code> Test</title>
  247. <link href="http://example.org/2003/12/13/atom03"/>
  248. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  249. <updated>2003-12-13T18:30:02Z</updated>
  250. <summary>Some text.</summary>
  251. </entry>
  252. </feed>`
  253. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  254. if err != nil {
  255. t.Fatal(err)
  256. }
  257. if feed.Entries[0].Title != "<code>Test</code> Test" {
  258. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  259. }
  260. }
  261. func TestParseEntryTitleWithNumericCharacterReference(t *testing.T) {
  262. data := `<?xml version="1.0" encoding="utf-8"?>
  263. <feed xmlns="http://www.w3.org/2005/Atom">
  264. <title>Example Feed</title>
  265. <link href="http://example.org/"/>
  266. <entry>
  267. <title>&#931; &#xDF;</title>
  268. <link href="http://example.org/2003/12/13/atom03"/>
  269. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  270. <updated>2003-12-13T18:30:02Z</updated>
  271. <summary>Some text.</summary>
  272. </entry>
  273. </feed>`
  274. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. if feed.Entries[0].Title != "Σ ß" {
  279. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  280. }
  281. }
  282. func TestParseEntryTitleWithDoubleEncodedEntities(t *testing.T) {
  283. data := `<?xml version="1.0" encoding="utf-8"?>
  284. <feed xmlns="http://www.w3.org/2005/Atom">
  285. <title>Example Feed</title>
  286. <link href="http://example.org/"/>
  287. <entry>
  288. <title>&amp;#39;AT&amp;amp;T&amp;#39;</title>
  289. <link href="http://example.org/2003/12/13/atom03"/>
  290. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  291. <updated>2003-12-13T18:30:02Z</updated>
  292. <summary>Some text.</summary>
  293. </entry>
  294. </feed>`
  295. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. if feed.Entries[0].Title != `'AT&T'` {
  300. t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
  301. }
  302. }
  303. func TestParseEntrySummaryWithXHTML(t *testing.T) {
  304. data := `<?xml version="1.0" encoding="utf-8"?>
  305. <feed xmlns="http://www.w3.org/2005/Atom">
  306. <title>Example Feed</title>
  307. <link href="http://example.org/"/>
  308. <entry>
  309. <title type="xhtml"><code>Test</code> Test</title>
  310. <link href="http://example.org/2003/12/13/atom03"/>
  311. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  312. <updated>2003-12-13T18:30:02Z</updated>
  313. <summary type="xhtml"><p>Some text.</p></summary>
  314. </entry>
  315. </feed>`
  316. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if feed.Entries[0].Content != "<p>Some text.</p>" {
  321. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  322. }
  323. }
  324. func TestParseEntrySummaryWithHTML(t *testing.T) {
  325. data := `<?xml version="1.0" encoding="utf-8"?>
  326. <feed xmlns="http://www.w3.org/2005/Atom">
  327. <title>Example Feed</title>
  328. <link href="http://example.org/"/>
  329. <entry>
  330. <title type="html">&lt;code&gt;Test&lt;/code&gt; Test</title>
  331. <link href="http://example.org/2003/12/13/atom03"/>
  332. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  333. <updated>2003-12-13T18:30:02Z</updated>
  334. <summary type="html"><![CDATA[<p>Some text.</p>]]></summary>
  335. </entry>
  336. </feed>`
  337. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. if feed.Entries[0].Content != "<p>Some text.</p>" {
  342. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  343. }
  344. }
  345. func TestParseEntrySummaryWithPlainText(t *testing.T) {
  346. data := `<?xml version="1.0" encoding="utf-8"?>
  347. <feed xmlns="http://www.w3.org/2005/Atom">
  348. <title>Example Feed</title>
  349. <link href="http://example.org/"/>
  350. <entry>
  351. <title type="html">&lt;code&gt;Test&lt;/code&gt; Test</title>
  352. <link href="http://example.org/2003/12/13/atom03"/>
  353. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  354. <updated>2003-12-13T18:30:02Z</updated>
  355. <summary type="text"><![CDATA[<Some text.>]]></summary>
  356. </entry>
  357. </feed>`
  358. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. if feed.Entries[0].Content != "<Some text.>" {
  363. t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
  364. }
  365. }
  366. func TestParseEntryWithAuthorName(t *testing.T) {
  367. data := `<?xml version="1.0" encoding="utf-8"?>
  368. <feed xmlns="http://www.w3.org/2005/Atom">
  369. <title>Example Feed</title>
  370. <link href="http://example.org/"/>
  371. <entry>
  372. <link href="http://example.org/2003/12/13/atom03"/>
  373. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  374. <updated>2003-12-13T18:30:02Z</updated>
  375. <summary>Some text.</summary>
  376. <author>
  377. <name>Me</name>
  378. <email>me@localhost</email>
  379. </author>
  380. </entry>
  381. </feed>`
  382. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  383. if err != nil {
  384. t.Fatal(err)
  385. }
  386. if feed.Entries[0].Author != "Me" {
  387. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  388. }
  389. }
  390. func TestParseEntryWithoutAuthorName(t *testing.T) {
  391. data := `<?xml version="1.0" encoding="utf-8"?>
  392. <feed xmlns="http://www.w3.org/2005/Atom">
  393. <title>Example Feed</title>
  394. <link href="http://example.org/"/>
  395. <entry>
  396. <link href="http://example.org/2003/12/13/atom03"/>
  397. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  398. <updated>2003-12-13T18:30:02Z</updated>
  399. <summary>Some text.</summary>
  400. <author>
  401. <name/>
  402. <email>me@localhost</email>
  403. </author>
  404. </entry>
  405. </feed>`
  406. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  407. if err != nil {
  408. t.Fatal(err)
  409. }
  410. if feed.Entries[0].Author != "me@localhost" {
  411. t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author)
  412. }
  413. }
  414. func TestParseEntryWithEnclosures(t *testing.T) {
  415. data := `<?xml version="1.0" encoding="utf-8"?>
  416. <feed xmlns="http://www.w3.org/2005/Atom">
  417. <id>http://www.example.org/myfeed</id>
  418. <title>My Podcast Feed</title>
  419. <updated>2005-07-15T12:00:00Z</updated>
  420. <author>
  421. <name>John Doe</name>
  422. </author>
  423. <link href="http://example.org" />
  424. <link rel="self" href="http://example.org/myfeed" />
  425. <entry>
  426. <id>http://www.example.org/entries/1</id>
  427. <title>Atom 1.0</title>
  428. <updated>2005-07-15T12:00:00Z</updated>
  429. <link href="http://www.example.org/entries/1" />
  430. <summary>An overview of Atom 1.0</summary>
  431. <link rel="enclosure"
  432. type="audio/mpeg"
  433. title="MP3"
  434. href="http://www.example.org/myaudiofile.mp3"
  435. length="1234" />
  436. <link rel="enclosure"
  437. type="application/x-bittorrent"
  438. title="BitTorrent"
  439. href="http://www.example.org/myaudiofile.torrent"
  440. length="4567" />
  441. <content type="xhtml">
  442. <div xmlns="http://www.w3.org/1999/xhtml">
  443. <h1>Show Notes</h1>
  444. <ul>
  445. <li>00:01:00 -- Introduction</li>
  446. <li>00:15:00 -- Talking about Atom 1.0</li>
  447. <li>00:30:00 -- Wrapping up</li>
  448. </ul>
  449. </div>
  450. </content>
  451. </entry>
  452. </feed>`
  453. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. if len(feed.Entries) != 1 {
  458. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  459. }
  460. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  461. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  462. }
  463. if len(feed.Entries[0].Enclosures) != 2 {
  464. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  465. }
  466. expectedResults := []struct {
  467. url string
  468. mimeType string
  469. size int64
  470. }{
  471. {"http://www.example.org/myaudiofile.mp3", "audio/mpeg", 1234},
  472. {"http://www.example.org/myaudiofile.torrent", "application/x-bittorrent", 4567},
  473. }
  474. for index, enclosure := range feed.Entries[0].Enclosures {
  475. if expectedResults[index].url != enclosure.URL {
  476. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  477. }
  478. if expectedResults[index].mimeType != enclosure.MimeType {
  479. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  480. }
  481. if expectedResults[index].size != enclosure.Size {
  482. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  483. }
  484. }
  485. }
  486. func TestParseEntryWithoutEnclosureURL(t *testing.T) {
  487. data := `<?xml version="1.0" encoding="utf-8"?>
  488. <feed xmlns="http://www.w3.org/2005/Atom">
  489. <id>http://www.example.org/myfeed</id>
  490. <title>My Podcast Feed</title>
  491. <updated>2005-07-15T12:00:00Z</updated>
  492. <link href="http://example.org" />
  493. <link rel="self" href="http://example.org/myfeed" />
  494. <entry>
  495. <id>http://www.example.org/entries/1</id>
  496. <title>Atom 1.0</title>
  497. <updated>2005-07-15T12:00:00Z</updated>
  498. <link href="http://www.example.org/entries/1" />
  499. <summary>An overview of Atom 1.0</summary>
  500. <link rel="enclosure" href="" length="0" />
  501. <content type="xhtml">Test</content>
  502. </entry>
  503. </feed>`
  504. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  505. if err != nil {
  506. t.Fatal(err)
  507. }
  508. if len(feed.Entries) != 1 {
  509. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  510. }
  511. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  512. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  513. }
  514. if len(feed.Entries[0].Enclosures) != 0 {
  515. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  516. }
  517. }
  518. func TestParseEntryWithPublished(t *testing.T) {
  519. data := `<?xml version="1.0" encoding="utf-8"?>
  520. <feed xmlns="http://www.w3.org/2005/Atom">
  521. <title>Example Feed</title>
  522. <link href="http://example.org/"/>
  523. <entry>
  524. <link href="http://example.org/2003/12/13/atom03"/>
  525. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  526. <published>2003-12-13T18:30:02Z</published>
  527. <summary>Some text.</summary>
  528. </entry>
  529. </feed>`
  530. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. if !feed.Entries[0].Date.Equal(time.Date(2003, time.December, 13, 18, 30, 2, 0, time.UTC)) {
  535. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  536. }
  537. }
  538. func TestParseEntryWithPublishedAndUpdated(t *testing.T) {
  539. data := `<?xml version="1.0" encoding="utf-8"?>
  540. <feed xmlns="http://www.w3.org/2005/Atom">
  541. <title>Example Feed</title>
  542. <link href="http://example.org/"/>
  543. <entry>
  544. <link href="http://example.org/2003/12/13/atom03"/>
  545. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  546. <published>2002-11-12T18:30:02Z</published>
  547. <updated>2003-12-13T18:30:02Z</updated>
  548. <summary>Some text.</summary>
  549. </entry>
  550. </feed>`
  551. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  552. if err != nil {
  553. t.Fatal(err)
  554. }
  555. if !feed.Entries[0].Date.Equal(time.Date(2002, time.November, 12, 18, 30, 2, 0, time.UTC)) {
  556. t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
  557. }
  558. }
  559. func TestParseInvalidXml(t *testing.T) {
  560. data := `garbage`
  561. _, err := Parse("https://example.org/", bytes.NewBufferString(data))
  562. if err == nil {
  563. t.Error("Parse should returns an error")
  564. }
  565. }
  566. func TestParseTitleWithSingleQuote(t *testing.T) {
  567. data := `
  568. <?xml version="1.0" encoding="utf-8"?>
  569. <feed xmlns="http://www.w3.org/2005/Atom">
  570. <title>' or ’</title>
  571. <link href="http://example.org/"/>
  572. </feed>
  573. `
  574. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  575. if err != nil {
  576. t.Fatal(err)
  577. }
  578. if feed.Title != "' or ’" {
  579. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  580. }
  581. }
  582. func TestParseTitleWithEncodedSingleQuote(t *testing.T) {
  583. data := `
  584. <?xml version="1.0" encoding="utf-8"?>
  585. <feed xmlns="http://www.w3.org/2005/Atom">
  586. <title type="html">Test&#39;s Blog</title>
  587. <link href="http://example.org/"/>
  588. </feed>
  589. `
  590. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  591. if err != nil {
  592. t.Fatal(err)
  593. }
  594. if feed.Title != "Test's Blog" {
  595. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  596. }
  597. }
  598. func TestParseTitleWithSingleQuoteAndHTMLType(t *testing.T) {
  599. data := `
  600. <?xml version="1.0" encoding="utf-8"?>
  601. <feed xmlns="http://www.w3.org/2005/Atom">
  602. <title type="html">O’Hara</title>
  603. <link href="http://example.org/"/>
  604. </feed>
  605. `
  606. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  607. if err != nil {
  608. t.Fatal(err)
  609. }
  610. if feed.Title != "O’Hara" {
  611. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  612. }
  613. }
  614. func TestParseWithHTMLEntity(t *testing.T) {
  615. data := `
  616. <?xml version="1.0" encoding="utf-8"?>
  617. <feed xmlns="http://www.w3.org/2005/Atom">
  618. <title>Example &nbsp; Feed</title>
  619. <link href="http://example.org/"/>
  620. </feed>
  621. `
  622. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  623. if err != nil {
  624. t.Fatal(err)
  625. }
  626. if feed.Title != "Example \u00a0 Feed" {
  627. t.Errorf(`Incorrect title, got: %q`, feed.Title)
  628. }
  629. }
  630. func TestParseWithInvalidCharacterEntity(t *testing.T) {
  631. data := `
  632. <?xml version="1.0" encoding="utf-8"?>
  633. <feed xmlns="http://www.w3.org/2005/Atom">
  634. <title>Example Feed</title>
  635. <link href="http://example.org/a&b"/>
  636. </feed>
  637. `
  638. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. if feed.SiteURL != "http://example.org/a&b" {
  643. t.Errorf(`Incorrect URL, got: %q`, feed.SiteURL)
  644. }
  645. }
  646. func TestParseMediaGroup(t *testing.T) {
  647. data := `<?xml version="1.0" encoding="utf-8"?>
  648. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
  649. <id>http://www.example.org/myfeed</id>
  650. <title>My Video Feed</title>
  651. <updated>2005-07-15T12:00:00Z</updated>
  652. <link href="http://example.org" />
  653. <link rel="self" href="http://example.org/myfeed" />
  654. <entry>
  655. <id>http://www.example.org/entries/1</id>
  656. <title>Some Video</title>
  657. <updated>2005-07-15T12:00:00Z</updated>
  658. <link href="http://www.example.org/entries/1" />
  659. <media:group>
  660. <media:title>Another title</media:title>
  661. <media:content url="https://www.youtube.com/v/abcd" type="application/x-shockwave-flash" width="640" height="390"/>
  662. <media:thumbnail url="https://example.org/thumbnail.jpg" width="480" height="360"/>
  663. <media:description>Some description
  664. A website: http://example.org/</media:description>
  665. </media:group>
  666. </entry>
  667. </feed>`
  668. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. if len(feed.Entries) != 1 {
  673. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  674. }
  675. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  676. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  677. }
  678. if feed.Entries[0].Content != `Some description<br>A website: <a href="http://example.org/">http://example.org/</a>` {
  679. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  680. }
  681. if len(feed.Entries[0].Enclosures) != 2 {
  682. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  683. }
  684. expectedResults := []struct {
  685. url string
  686. mimeType string
  687. size int64
  688. }{
  689. {"https://example.org/thumbnail.jpg", "image/*", 0},
  690. {"https://www.youtube.com/v/abcd", "application/x-shockwave-flash", 0},
  691. }
  692. for index, enclosure := range feed.Entries[0].Enclosures {
  693. if expectedResults[index].url != enclosure.URL {
  694. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  695. }
  696. if expectedResults[index].mimeType != enclosure.MimeType {
  697. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  698. }
  699. if expectedResults[index].size != enclosure.Size {
  700. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  701. }
  702. }
  703. }
  704. func TestParseMediaElements(t *testing.T) {
  705. data := `<?xml version="1.0" encoding="utf-8"?>
  706. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
  707. <id>http://www.example.org/myfeed</id>
  708. <title>My Video Feed</title>
  709. <updated>2005-07-15T12:00:00Z</updated>
  710. <link href="http://example.org" />
  711. <link rel="self" href="http://example.org/myfeed" />
  712. <entry>
  713. <id>http://www.example.org/entries/1</id>
  714. <title>Some Video</title>
  715. <updated>2005-07-15T12:00:00Z</updated>
  716. <link href="http://www.example.org/entries/1" />
  717. <media:title>Another title</media:title>
  718. <media:content url="https://www.youtube.com/v/abcd" type="application/x-shockwave-flash" width="640" height="390"/>
  719. <media:thumbnail url="https://example.org/thumbnail.jpg" width="480" height="360"/>
  720. <media:description>Some description
  721. A website: http://example.org/</media:description>
  722. </entry>
  723. </feed>`
  724. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  725. if err != nil {
  726. t.Fatal(err)
  727. }
  728. if len(feed.Entries) != 1 {
  729. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  730. }
  731. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  732. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  733. }
  734. if feed.Entries[0].Content != `Some description<br>A website: <a href="http://example.org/">http://example.org/</a>` {
  735. t.Errorf("Incorrect entry content, got: %q", feed.Entries[0].Content)
  736. }
  737. if len(feed.Entries[0].Enclosures) != 2 {
  738. t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures))
  739. }
  740. expectedResults := []struct {
  741. url string
  742. mimeType string
  743. size int64
  744. }{
  745. {"https://example.org/thumbnail.jpg", "image/*", 0},
  746. {"https://www.youtube.com/v/abcd", "application/x-shockwave-flash", 0},
  747. }
  748. for index, enclosure := range feed.Entries[0].Enclosures {
  749. if expectedResults[index].url != enclosure.URL {
  750. t.Errorf(`Unexpected enclosure URL, got %q instead of %q`, enclosure.URL, expectedResults[index].url)
  751. }
  752. if expectedResults[index].mimeType != enclosure.MimeType {
  753. t.Errorf(`Unexpected enclosure type, got %q instead of %q`, enclosure.MimeType, expectedResults[index].mimeType)
  754. }
  755. if expectedResults[index].size != enclosure.Size {
  756. t.Errorf(`Unexpected enclosure size, got %d instead of %d`, enclosure.Size, expectedResults[index].size)
  757. }
  758. }
  759. }
  760. func TestParseRepliesLinkRelationWithHTMLType(t *testing.T) {
  761. data := `<?xml version="1.0" encoding="utf-8"?>
  762. <feed xmlns="http://www.w3.org/2005/Atom"
  763. xmlns:thr="http://purl.org/syndication/thread/1.0">
  764. <id>http://www.example.org/myfeed</id>
  765. <title>My Example Feed</title>
  766. <updated>2005-07-28T12:00:00Z</updated>
  767. <link href="http://www.example.org/myfeed" />
  768. <author><name>James</name></author>
  769. <entry>
  770. <id>tag:entries.com,2005:1</id>
  771. <title>My original entry</title>
  772. <updated>2006-03-01T12:12:12Z</updated>
  773. <link href="http://www.example.org/entries/1" />
  774. <link rel="replies"
  775. type="application/atom+xml"
  776. href="http://www.example.org/mycommentsfeed.xml"
  777. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  778. <link rel="replies"
  779. type="text/html"
  780. href="http://www.example.org/comments.html"
  781. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  782. <summary>This is my original entry</summary>
  783. </entry>
  784. </feed>`
  785. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  786. if err != nil {
  787. t.Fatal(err)
  788. }
  789. if len(feed.Entries) != 1 {
  790. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  791. }
  792. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  793. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  794. }
  795. if feed.Entries[0].CommentsURL != "http://www.example.org/comments.html" {
  796. t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL)
  797. }
  798. }
  799. func TestParseRepliesLinkRelationWithXHTMLType(t *testing.T) {
  800. data := `<?xml version="1.0" encoding="utf-8"?>
  801. <feed xmlns="http://www.w3.org/2005/Atom"
  802. xmlns:thr="http://purl.org/syndication/thread/1.0">
  803. <id>http://www.example.org/myfeed</id>
  804. <title>My Example Feed</title>
  805. <updated>2005-07-28T12:00:00Z</updated>
  806. <link href="http://www.example.org/myfeed" />
  807. <author><name>James</name></author>
  808. <entry>
  809. <id>tag:entries.com,2005:1</id>
  810. <title>My original entry</title>
  811. <updated>2006-03-01T12:12:12Z</updated>
  812. <link href="http://www.example.org/entries/1" />
  813. <link rel="replies"
  814. type="application/atom+xml"
  815. href="http://www.example.org/mycommentsfeed.xml"
  816. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  817. <link rel="replies"
  818. type="application/xhtml+xml"
  819. href="http://www.example.org/comments.xhtml"
  820. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  821. <summary>This is my original entry</summary>
  822. </entry>
  823. </feed>`
  824. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  825. if err != nil {
  826. t.Fatal(err)
  827. }
  828. if len(feed.Entries) != 1 {
  829. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  830. }
  831. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  832. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  833. }
  834. if feed.Entries[0].CommentsURL != "http://www.example.org/comments.xhtml" {
  835. t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL)
  836. }
  837. }
  838. func TestParseRepliesLinkRelationWithNoType(t *testing.T) {
  839. data := `<?xml version="1.0" encoding="utf-8"?>
  840. <feed xmlns="http://www.w3.org/2005/Atom"
  841. xmlns:thr="http://purl.org/syndication/thread/1.0">
  842. <id>http://www.example.org/myfeed</id>
  843. <title>My Example Feed</title>
  844. <updated>2005-07-28T12:00:00Z</updated>
  845. <link href="http://www.example.org/myfeed" />
  846. <author><name>James</name></author>
  847. <entry>
  848. <id>tag:entries.com,2005:1</id>
  849. <title>My original entry</title>
  850. <updated>2006-03-01T12:12:12Z</updated>
  851. <link href="http://www.example.org/entries/1" />
  852. <link rel="replies"
  853. href="http://www.example.org/mycommentsfeed.xml"
  854. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  855. <summary>This is my original entry</summary>
  856. </entry>
  857. </feed>`
  858. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  859. if err != nil {
  860. t.Fatal(err)
  861. }
  862. if len(feed.Entries) != 1 {
  863. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  864. }
  865. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  866. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  867. }
  868. if feed.Entries[0].CommentsURL != "" {
  869. t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL)
  870. }
  871. }
  872. func TestAbsoluteCommentsURL(t *testing.T) {
  873. data := `<?xml version="1.0" encoding="utf-8"?>
  874. <feed xmlns="http://www.w3.org/2005/Atom"
  875. xmlns:thr="http://purl.org/syndication/thread/1.0">
  876. <id>http://www.example.org/myfeed</id>
  877. <title>My Example Feed</title>
  878. <updated>2005-07-28T12:00:00Z</updated>
  879. <link href="http://www.example.org/myfeed" />
  880. <author><name>James</name></author>
  881. <entry>
  882. <id>tag:entries.com,2005:1</id>
  883. <title>My original entry</title>
  884. <updated>2006-03-01T12:12:12Z</updated>
  885. <link href="http://www.example.org/entries/1" />
  886. <link rel="replies"
  887. type="text/html"
  888. href="invalid url"
  889. thr:count="10" thr:updated="2005-07-28T12:10:00Z" />
  890. <summary>This is my original entry</summary>
  891. </entry>
  892. </feed>`
  893. feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
  894. if err != nil {
  895. t.Fatal(err)
  896. }
  897. if len(feed.Entries) != 1 {
  898. t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
  899. }
  900. if feed.Entries[0].URL != "http://www.example.org/entries/1" {
  901. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  902. }
  903. if feed.Entries[0].CommentsURL != "" {
  904. t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL)
  905. }
  906. }