4
0

parser_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package parser // import "miniflux.app/v2/internal/reader/parser"
  4. import (
  5. "bytes"
  6. "os"
  7. "strings"
  8. "testing"
  9. "golang.org/x/text/encoding/unicode"
  10. )
  11. func TestParseFeedWithUTF16Encoding(t *testing.T) {
  12. document := `<?xml version="1.0" encoding="utf-16"?><rss version="2.0"><channel><title>Título</title><item><title>i</title></item></channel></rss>`
  13. data, err := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder().Bytes([]byte(document))
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. feed, err := ParseFeed("https://example.org/", bytes.NewReader(data))
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. if feed.Title != "Título" {
  22. t.Errorf(`Incorrect feed title, got: %q`, feed.Title)
  23. }
  24. if len(feed.Entries) != 1 {
  25. t.Errorf(`Incorrect number of entries, got: %d`, len(feed.Entries))
  26. }
  27. }
  28. func BenchmarkParse(b *testing.B) {
  29. var testCases = map[string][]string{
  30. "large_atom.xml": {"https://dustri.org/b", ""},
  31. "large_rss.xml": {"https://dustri.org/b", ""},
  32. "small_atom.xml": {"https://github.com/miniflux/v2/commits/main", ""},
  33. }
  34. for filename := range testCases {
  35. data, err := os.ReadFile("./testdata/" + filename)
  36. if err != nil {
  37. b.Fatalf(`Unable to read file %q: %v`, filename, err)
  38. }
  39. testCases[filename][1] = string(data)
  40. }
  41. for b.Loop() {
  42. for _, v := range testCases {
  43. ParseFeed(v[0], strings.NewReader(v[1]))
  44. }
  45. }
  46. }
  47. func FuzzParse(f *testing.F) {
  48. f.Add("https://z.org", `<?xml version="1.0" encoding="utf-8"?>
  49. <feed xmlns="http://www.w3.org/2005/Atom">
  50. <title>Example Feed</title>
  51. <link href="http://z.org/"/>
  52. <link href="/k"/>
  53. <updated>2003-12-13T18:30:02Z</updated>
  54. <author><name>John Doe</name></author>
  55. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  56. <entry>
  57. <title>a</title>
  58. <link href="http://example.org/b"/>
  59. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  60. <updated>2003-12-13T18:30:02Z</updated>
  61. <summary>c</summary>
  62. </entry>
  63. </feed>`)
  64. f.Add("https://z.org", `<?xml version="1.0"?>
  65. <rss version="2.0">
  66. <channel>
  67. <title>a</title>
  68. <link>http://z.org</link>
  69. <item>
  70. <title>a</title>
  71. <link>http://z.org</link>
  72. <description>d</description>
  73. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  74. <guid>l</guid>
  75. </item>
  76. </channel>
  77. </rss>`)
  78. f.Add("https://z.org", `<?xml version="1.0" encoding="utf-8"?>
  79. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">
  80. <channel>
  81. <title>a</title>
  82. <link>http://z.org/</link>
  83. </channel>
  84. <item>
  85. <title>a</title>
  86. <link>/</link>
  87. <description>c</description>
  88. </item>
  89. </rdf:RDF>`)
  90. f.Add("http://z.org", `{
  91. "version": "http://jsonfeed.org/version/1",
  92. "title": "a",
  93. "home_page_url": "http://z.org/",
  94. "feed_url": "http://z.org/a.json",
  95. "items": [
  96. {"id": "2","content_text": "a","url": "https://z.org/2"},
  97. {"id": "1","content_html": "<a","url":"http://z.org/1"}]}`)
  98. f.Fuzz(func(t *testing.T, url string, data string) {
  99. ParseFeed(url, strings.NewReader(data))
  100. })
  101. }
  102. func TestParseAtom03Feed(t *testing.T) {
  103. data := `<?xml version="1.0" encoding="utf-8"?>
  104. <feed version="0.3" xmlns="http://purl.org/atom/ns#">
  105. <title>dive into mark</title>
  106. <link rel="alternate" type="text/html" href="http://diveintomark.org/"/>
  107. <modified>2003-12-13T18:30:02Z</modified>
  108. <author><name>Mark Pilgrim</name></author>
  109. <entry>
  110. <title>Atom 0.3 snapshot</title>
  111. <link rel="alternate" type="text/html" href="http://diveintomark.org/2003/12/13/atom03"/>
  112. <id>tag:diveintomark.org,2003:3.2397</id>
  113. <issued>2003-12-13T08:29:29-04:00</issued>
  114. <modified>2003-12-13T18:30:02Z</modified>
  115. <summary type="text/plain">It&apos;s a test</summary>
  116. <content type="text/html" mode="escaped"><![CDATA[<p>HTML content</p>]]></content>
  117. </entry>
  118. </feed>`
  119. feed, err := ParseFeed("https://example.org/", strings.NewReader(data))
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. if feed.Title != "dive into mark" {
  124. t.Errorf("Incorrect title, got: %s", feed.Title)
  125. }
  126. }
  127. func TestParseAtom10Feed(t *testing.T) {
  128. data := `<?xml version="1.0" encoding="utf-8"?>
  129. <feed xmlns="http://www.w3.org/2005/Atom">
  130. <title>Example Feed</title>
  131. <link href="http://example.org/"/>
  132. <updated>2003-12-13T18:30:02Z</updated>
  133. <author>
  134. <name>John Doe</name>
  135. </author>
  136. <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  137. <entry>
  138. <title>Atom-Powered Robots Run Amok</title>
  139. <link href="http://example.org/2003/12/13/atom03"/>
  140. <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  141. <updated>2003-12-13T18:30:02Z</updated>
  142. <summary>Some text.</summary>
  143. </entry>
  144. </feed>`
  145. feed, err := ParseFeed("https://example.org/", strings.NewReader(data))
  146. if err != nil {
  147. t.Error(err)
  148. }
  149. if feed.Title != "Example Feed" {
  150. t.Errorf("Incorrect title, got: %s", feed.Title)
  151. }
  152. }
  153. func TestParseAtomFeedWithRelativeURL(t *testing.T) {
  154. data := `<?xml version="1.0" encoding="utf-8"?>
  155. <feed xmlns="http://www.w3.org/2005/Atom">
  156. <title>Example Feed</title>
  157. <link href="/blog/atom.xml" rel="self" type="application/atom+xml"/>
  158. <link href="/blog"/>
  159. <entry>
  160. <title>Test</title>
  161. <link href="/blog/article.html"/>
  162. <link href="/blog/article.html" rel="alternate" type="text/html"/>
  163. <id>/blog/article.html</id>
  164. <updated>2003-12-13T18:30:02Z</updated>
  165. <summary>Some text.</summary>
  166. </entry>
  167. </feed>`
  168. feed, err := ParseFeed("https://example.org/blog/atom.xml", strings.NewReader(data))
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. if feed.FeedURL != "https://example.org/blog/atom.xml" {
  173. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  174. }
  175. if feed.SiteURL != "https://example.org/blog" {
  176. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  177. }
  178. if feed.Entries[0].URL != "https://example.org/blog/article.html" {
  179. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  180. }
  181. }
  182. func TestParseRSS(t *testing.T) {
  183. data := `<?xml version="1.0"?>
  184. <rss version="2.0">
  185. <channel>
  186. <title>Liftoff News</title>
  187. <link>http://liftoff.msfc.nasa.gov/</link>
  188. <item>
  189. <title>Star City</title>
  190. <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
  191. <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>
  192. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  193. <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
  194. </item>
  195. </channel>
  196. </rss>`
  197. feed, err := ParseFeed("http://liftoff.msfc.nasa.gov/", strings.NewReader(data))
  198. if err != nil {
  199. t.Error(err)
  200. }
  201. if feed.Title != "Liftoff News" {
  202. t.Errorf("Incorrect title, got: %s", feed.Title)
  203. }
  204. }
  205. func TestParseRSSFeedWithRelativeURL(t *testing.T) {
  206. data := `<?xml version="1.0"?>
  207. <rss version="2.0">
  208. <channel>
  209. <title>Example Feed</title>
  210. <link>/blog</link>
  211. <item>
  212. <title>Example Entry</title>
  213. <link>/blog/article.html</link>
  214. <description>Something</description>
  215. <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
  216. <guid>1234</guid>
  217. </item>
  218. </channel>
  219. </rss>`
  220. feed, err := ParseFeed("http://example.org/rss.xml", strings.NewReader(data))
  221. if err != nil {
  222. t.Error(err)
  223. }
  224. if feed.Title != "Example Feed" {
  225. t.Errorf("Incorrect title, got: %s", feed.Title)
  226. }
  227. if feed.FeedURL != "http://example.org/rss.xml" {
  228. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  229. }
  230. if feed.SiteURL != "http://example.org/blog" {
  231. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  232. }
  233. if feed.Entries[0].URL != "http://example.org/blog/article.html" {
  234. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  235. }
  236. }
  237. func TestParseRDF(t *testing.T) {
  238. data := `<?xml version="1.0" encoding="utf-8"?>
  239. <rdf:RDF
  240. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  241. xmlns="http://purl.org/rss/1.0/"
  242. >
  243. <channel>
  244. <title>RDF Example</title>
  245. <link>http://example.org/</link>
  246. </channel>
  247. <item>
  248. <title>Title</title>
  249. <link>http://example.org/item</link>
  250. <description>Test</description>
  251. </item>
  252. </rdf:RDF>`
  253. feed, err := ParseFeed("http://example.org/", strings.NewReader(data))
  254. if err != nil {
  255. t.Error(err)
  256. }
  257. if feed.Title != "RDF Example" {
  258. t.Errorf("Incorrect title, got: %s", feed.Title)
  259. }
  260. }
  261. func TestParseRDFWithRelativeURL(t *testing.T) {
  262. data := `<?xml version="1.0" encoding="utf-8"?>
  263. <rdf:RDF
  264. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  265. xmlns="http://purl.org/rss/1.0/"
  266. >
  267. <channel>
  268. <title>RDF Example</title>
  269. <link>/blog</link>
  270. </channel>
  271. <item>
  272. <title>Title</title>
  273. <link>/blog/article.html</link>
  274. <description>Test</description>
  275. </item>
  276. </rdf:RDF>`
  277. feed, err := ParseFeed("http://example.org/rdf.xml", strings.NewReader(data))
  278. if err != nil {
  279. t.Error(err)
  280. }
  281. if feed.FeedURL != "http://example.org/rdf.xml" {
  282. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  283. }
  284. if feed.SiteURL != "http://example.org/blog" {
  285. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  286. }
  287. if feed.Entries[0].URL != "http://example.org/blog/article.html" {
  288. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  289. }
  290. }
  291. func TestParseJson(t *testing.T) {
  292. data := `{
  293. "version": "https://jsonfeed.org/version/1",
  294. "title": "My Example Feed",
  295. "home_page_url": "https://example.org/",
  296. "feed_url": "https://example.org/feed.json",
  297. "items": [
  298. {
  299. "id": "2",
  300. "content_text": "This is a second item.",
  301. "url": "https://example.org/second-item"
  302. },
  303. {
  304. "id": "1",
  305. "content_html": "<p>Hello, world!</p>",
  306. "url": "https://example.org/initial-post"
  307. }
  308. ]
  309. }`
  310. feed, err := ParseFeed("https://example.org/feed.json", strings.NewReader(data))
  311. if err != nil {
  312. t.Error(err)
  313. }
  314. if feed.Title != "My Example Feed" {
  315. t.Errorf("Incorrect title, got: %s", feed.Title)
  316. }
  317. }
  318. func TestParseJsonFeedWithRelativeURL(t *testing.T) {
  319. data := `{
  320. "version": "https://jsonfeed.org/version/1",
  321. "title": "My Example Feed",
  322. "home_page_url": "/blog",
  323. "feed_url": "/blog/feed.json",
  324. "items": [
  325. {
  326. "id": "2",
  327. "content_text": "This is a second item.",
  328. "url": "/blog/article.html"
  329. }
  330. ]
  331. }`
  332. feed, err := ParseFeed("https://example.org/blog/feed.json", strings.NewReader(data))
  333. if err != nil {
  334. t.Error(err)
  335. }
  336. if feed.Title != "My Example Feed" {
  337. t.Errorf("Incorrect title, got: %s", feed.Title)
  338. }
  339. if feed.FeedURL != "https://example.org/blog/feed.json" {
  340. t.Errorf("Incorrect feed URL, got: %s", feed.FeedURL)
  341. }
  342. if feed.SiteURL != "https://example.org/blog" {
  343. t.Errorf("Incorrect site URL, got: %s", feed.SiteURL)
  344. }
  345. if feed.Entries[0].URL != "https://example.org/blog/article.html" {
  346. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  347. }
  348. }
  349. func TestParseJsonFeedWithUTF8BOM(t *testing.T) {
  350. data := "\xEF\xBB\xBF" + `{
  351. "version": "https://jsonfeed.org/version/1.1",
  352. "title": "My Example Feed",
  353. "home_page_url": "https://example.org/",
  354. "items": [
  355. {
  356. "id": "1",
  357. "content_text": "Hello, world!",
  358. "url": "https://example.org/initial-post"
  359. }
  360. ]
  361. }`
  362. feed, err := ParseFeed("https://example.org/feed.json", strings.NewReader(data))
  363. if err != nil {
  364. t.Fatal(err)
  365. }
  366. if feed.Title != "My Example Feed" {
  367. t.Errorf("Incorrect title, got: %s", feed.Title)
  368. }
  369. if len(feed.Entries) != 1 {
  370. t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
  371. }
  372. if feed.Entries[0].URL != "https://example.org/initial-post" {
  373. t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
  374. }
  375. }
  376. func TestParseUnknownFeed(t *testing.T) {
  377. data := `
  378. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  379. <html xmlns="http://www.w3.org/1999/xhtml">
  380. <head>
  381. <title>Title of document</title>
  382. </head>
  383. <body>
  384. some content
  385. </body>
  386. </html>
  387. `
  388. _, err := ParseFeed("https://example.org/", strings.NewReader(data))
  389. if err == nil {
  390. t.Error("ParseFeed must returns an error")
  391. }
  392. }
  393. func TestParseEmptyFeed(t *testing.T) {
  394. _, err := ParseFeed("", strings.NewReader(""))
  395. if err == nil {
  396. t.Error("ParseFeed must returns an error")
  397. }
  398. }