parser_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opml // import "miniflux.app/v2/internal/reader/opml"
  4. import (
  5. "bytes"
  6. "errors"
  7. "testing"
  8. )
  9. func TestParseOpmlWithoutCategories(t *testing.T) {
  10. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  11. <opml version="2.0">
  12. <head>
  13. <title>mySubscriptions.opml</title>
  14. </head>
  15. <body>
  16. <outline text="CNET News.com" description="Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media." htmlUrl="http://news.com.com/" language="unknown" title="CNET News.com" type="rss" version="RSS2" xmlUrl="http://news.com.com/2547-1_3-0-5.xml"/>
  17. <outline text="washingtonpost.com - Politics" description="Politics" htmlUrl="http://www.washingtonpost.com/wp-dyn/politics?nav=rss_politics" language="unknown" title="washingtonpost.com - Politics" type="rss" version="RSS2" xmlUrl="http://www.washingtonpost.com/wp-srv/politics/rssheadlines.xml"/>
  18. <outline text="Scobleizer: Microsoft Geek Blogger" description="Robert Scoble's look at geek and Microsoft life." htmlUrl="http://radio.weblogs.com/0001011/" language="unknown" title="Scobleizer: Microsoft Geek Blogger" type="rss" version="RSS2" xmlUrl="http://radio.weblogs.com/0001011/rss.xml"/>
  19. <outline text="Yahoo! News: Technology" description="Technology" htmlUrl="http://news.yahoo.com/news?tmpl=index&amp;cid=738" language="unknown" title="Yahoo! News: Technology" type="rss" version="RSS2" xmlUrl="http://rss.news.yahoo.com/rss/tech"/>
  20. <outline text="Workbench" description="Programming and publishing news and comment" htmlUrl="http://www.cadenhead.org/workbench/" language="unknown" title="Workbench" type="rss" version="RSS2" xmlUrl="http://www.cadenhead.org/workbench/rss.xml"/>
  21. <outline text="Christian Science Monitor | Top Stories" description="Read the front page stories of csmonitor.com." htmlUrl="http://csmonitor.com" language="unknown" title="Christian Science Monitor | Top Stories" type="rss" version="RSS" xmlUrl="http://www.csmonitor.com/rss/top.rss"/>
  22. <outline text="Dictionary.com Word of the Day" description="A new word is presented every day with its definition and example sentences from actual published works." htmlUrl="http://dictionary.reference.com/wordoftheday/" language="unknown" title="Dictionary.com Word of the Day" type="rss" version="RSS" xmlUrl="http://www.dictionary.com/wordoftheday/wotd.rss"/>
  23. <outline text="The Motley Fool" description="To Educate, Amuse, and Enrich" htmlUrl="http://www.fool.com" language="unknown" title="The Motley Fool" type="rss" version="RSS" xmlUrl="http://www.fool.com/xml/foolnews_rss091.xml"/>
  24. <outline text="InfoWorld: Top News" description="The latest on Top News from InfoWorld" htmlUrl="http://www.infoworld.com/news/index.html" language="unknown" title="InfoWorld: Top News" type="rss" version="RSS2" xmlUrl="http://www.infoworld.com/rss/news.xml"/>
  25. <outline text="NYT &gt; Business" description="Find breaking news &amp; business news on Wall Street, media &amp; advertising, international business, banking, interest rates, the stock market, currencies &amp; funds." htmlUrl="http://www.nytimes.com/pages/business/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Business" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Business.xml"/>
  26. <outline text="NYT &gt; Technology" description="" htmlUrl="http://www.nytimes.com/pages/technology/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Technology" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Technology.xml"/>
  27. <outline text="Scripting News" description="It's even worse than it appears." htmlUrl="http://www.scripting.com/" language="unknown" title="Scripting News" type="rss" version="RSS2" xmlUrl="http://www.scripting.com/rss.xml"/>
  28. <outline text="Wired News" description="Technology, and the way we do business, is changing the world we know. Wired News is a technology - and business-oriented news service feeding an intelligent, discerning audience. What role does technology play in the day-to-day living of your life? Wired News tells you. How has evolving technology changed the face of the international business world? Wired News puts you in the picture." htmlUrl="http://www.wired.com/" language="unknown" title="Wired News" type="rss" version="RSS" xmlUrl="http://www.wired.com/news_drop/netcenter/netcenter.rdf"/>
  29. </body>
  30. </opml>
  31. `
  32. var expected []subcription
  33. expected = append(expected, subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/", Description: "Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media."})
  34. subscriptions, err := parse(bytes.NewBufferString(data))
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if len(subscriptions) != 13 {
  39. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
  40. }
  41. if subscriptions[0] != expected[0] {
  42. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[0], expected[0])
  43. }
  44. }
  45. func TestParseOpmlWithCategories(t *testing.T) {
  46. data := `<?xml version="1.0" encoding="utf-8"?>
  47. <opml version="2.0">
  48. <head>
  49. <title>mySubscriptions.opml</title>
  50. </head>
  51. <body>
  52. <outline text="My Category 1">
  53. <outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  54. <outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
  55. </outline>
  56. <outline text="My Category 2">
  57. <outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
  58. </outline>
  59. </body>
  60. </opml>
  61. `
  62. var expected []subcription
  63. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
  64. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
  65. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})
  66. subscriptions, err := parse(bytes.NewBufferString(data))
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if len(subscriptions) != 3 {
  71. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  72. }
  73. for i := range len(subscriptions) {
  74. if subscriptions[i] != expected[i] {
  75. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  76. }
  77. }
  78. }
  79. func TestParseOpmlTrimsWhitespace(t *testing.T) {
  80. data := `<?xml version="1.0" encoding="utf-8"?>
  81. <opml version="2.0">
  82. <body>
  83. <outline text=" My Category ">
  84. <outline title=" Feed 1 " xmlUrl=" http://example.org/feed1/ " htmlUrl=" http://example.org/1 "/>
  85. <outline title=" " xmlUrl="&#10;http://example.org/feed2/&#9;"/>
  86. </outline>
  87. </body>
  88. </opml>
  89. `
  90. expected := []subcription{
  91. {Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category"},
  92. {Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: "My Category"},
  93. }
  94. subscriptions, err := parse(bytes.NewBufferString(data))
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. if len(subscriptions) != len(expected) {
  99. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), len(expected))
  100. }
  101. for i := range len(subscriptions) {
  102. if subscriptions[i] != expected[i] {
  103. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  104. }
  105. }
  106. }
  107. func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
  108. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  109. <opml version="2.0">
  110. <head>
  111. <title>mySubscriptions.opml</title>
  112. </head>
  113. <body>
  114. <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  115. <outline xmlUrl="http://example.org/feed2/"/>
  116. </body>
  117. </opml>
  118. `
  119. var expected []subcription
  120. expected = append(expected, subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  121. expected = append(expected, subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
  122. subscriptions, err := parse(bytes.NewBufferString(data))
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if len(subscriptions) != 2 {
  127. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  128. }
  129. for i := range len(subscriptions) {
  130. if subscriptions[i] != expected[i] {
  131. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  132. }
  133. }
  134. }
  135. func TestParseOpmlVersion1(t *testing.T) {
  136. data := `<?xml version="1.0"?>
  137. <opml version="1.0">
  138. <head>
  139. <title>mySubscriptions.opml</title>
  140. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  141. </head>
  142. <body>
  143. <outline title="Category 1">
  144. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  145. </outline>
  146. <outline title="Category 2">
  147. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  148. </outline>
  149. </body>
  150. </opml>
  151. `
  152. var expected []subcription
  153. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  154. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"})
  155. subscriptions, err := parse(bytes.NewBufferString(data))
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. if len(subscriptions) != 2 {
  160. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  161. }
  162. for i := range len(subscriptions) {
  163. if subscriptions[i] != expected[i] {
  164. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  165. }
  166. }
  167. }
  168. func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) {
  169. data := `<?xml version="1.0"?>
  170. <opml version="1.0">
  171. <head>
  172. <title>mySubscriptions.opml</title>
  173. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  174. </head>
  175. <body>
  176. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  177. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  178. </body>
  179. </opml>
  180. `
  181. var expected []subcription
  182. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  183. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})
  184. subscriptions, err := parse(bytes.NewBufferString(data))
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. if len(subscriptions) != 2 {
  189. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  190. }
  191. for i := range len(subscriptions) {
  192. if subscriptions[i] != expected[i] {
  193. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  194. }
  195. }
  196. }
  197. func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) {
  198. data := `<?xml version="1.0"?>
  199. <opml xmlns:rssowl="http://www.rssowl.org" version="1.1">
  200. <head>
  201. <title>RSSOwl Subscriptions</title>
  202. <dateCreated>星期二, 26 四月 2022 00:12:04 CST</dateCreated>
  203. </head>
  204. <body>
  205. <outline text="My Feeds" rssowl:isSet="true" rssowl:id="7">
  206. <outline text="Some Category" rssowl:isSet="false" rssowl:id="55">
  207. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  208. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  209. </outline>
  210. <outline text="Another Category" rssowl:isSet="false" rssowl:id="87">
  211. <outline type="rss" title="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"></outline>
  212. </outline>
  213. </outline>
  214. </body>
  215. </opml>
  216. `
  217. var expected []subcription
  218. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"})
  219. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"})
  220. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"})
  221. subscriptions, err := parse(bytes.NewBufferString(data))
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. if len(subscriptions) != 3 {
  226. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  227. }
  228. for i := range len(subscriptions) {
  229. if subscriptions[i] != expected[i] {
  230. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  231. }
  232. }
  233. }
  234. func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) {
  235. data := `<?xml version="1.0"?>
  236. <opml version="1.0">
  237. <head>
  238. <title>mySubscriptions.opml</title>
  239. </head>
  240. <body>
  241. <outline title="Feed 1">
  242. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/a&b" htmlUrl="http://example.org/c&d"></outline>
  243. </outline>
  244. </body>
  245. </opml>
  246. `
  247. var expected []subcription
  248. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"})
  249. subscriptions, err := parse(bytes.NewBufferString(data))
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. if len(subscriptions) != 1 {
  254. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  255. }
  256. for i := range len(subscriptions) {
  257. if subscriptions[i] != expected[i] {
  258. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  259. }
  260. }
  261. }
  262. func TestParseInvalidXML(t *testing.T) {
  263. data := `garbage`
  264. _, err := parse(bytes.NewBufferString(data))
  265. if err == nil {
  266. t.Error("Parse should generate an error")
  267. }
  268. }
  269. func TestParseOpmlWithMinifluxSettings(t *testing.T) {
  270. data := `<?xml version="1.0" encoding="utf-8"?>
  271. <opml version="2.0" xmlns:miniflux="https://miniflux.app/opml">
  272. <head><title>Miniflux</title></head>
  273. <body>
  274. <outline text="My Category">
  275. <outline type="rss"
  276. text="Feed 1"
  277. title="Feed 1"
  278. xmlUrl="http://example.org/feed1/"
  279. htmlUrl="http://example.org/1"
  280. miniflux:scraperRules="article.content"
  281. miniflux:rewriteRules="replace(&quot;foo&quot;|&quot;bar&quot;)"
  282. miniflux:urlRewriteRules="rewrite(&quot;^https://old&quot;|&quot;https://new&quot;)"
  283. miniflux:blocklistRules="sponsored"
  284. miniflux:keeplistRules="important"
  285. miniflux:blockFilterEntryRules="EntryTitle=~&quot;ad&quot;"
  286. miniflux:keepFilterEntryRules="EntryTitle=~&quot;news&quot;"
  287. miniflux:userAgent="CustomAgent/1.0"
  288. miniflux:proxyUrl="http://proxy.example.org"
  289. miniflux:crawler="true"
  290. miniflux:ignoreHTTPCache="true"
  291. miniflux:fetchViaProxy="true"
  292. miniflux:disabled="true"
  293. miniflux:noMediaPlayer="true"
  294. miniflux:hideGlobally="true"
  295. miniflux:allowSelfSignedCertificates="true"
  296. miniflux:disableHTTP2="true"
  297. miniflux:ignoreEntryUpdates="true"
  298. />
  299. </outline>
  300. </body>
  301. </opml>
  302. `
  303. expected := subcription{
  304. Title: "Feed 1",
  305. FeedURL: "http://example.org/feed1/",
  306. SiteURL: "http://example.org/1",
  307. CategoryName: "My Category",
  308. ScraperRules: "article.content",
  309. RewriteRules: `replace("foo"|"bar")`,
  310. UrlRewriteRules: `rewrite("^https://old"|"https://new")`,
  311. BlocklistRules: "sponsored",
  312. KeeplistRules: "important",
  313. BlockFilterEntryRules: `EntryTitle=~"ad"`,
  314. KeepFilterEntryRules: `EntryTitle=~"news"`,
  315. UserAgent: "CustomAgent/1.0",
  316. Crawler: true,
  317. IgnoreHTTPCache: true,
  318. FetchViaProxy: true,
  319. Disabled: true,
  320. NoMediaPlayer: true,
  321. HideGlobally: true,
  322. AllowSelfSignedCertificates: true,
  323. DisableHTTP2: true,
  324. IgnoreEntryUpdates: true,
  325. }
  326. subscriptions, err := parse(bytes.NewBufferString(data))
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. if len(subscriptions) != 1 {
  331. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  332. }
  333. if subscriptions[0] != expected {
  334. t.Errorf("Subscription is different:\ngot: %+v\nwant: %+v", subscriptions[0], expected)
  335. }
  336. }
  337. func TestParseInvalidDocument(t *testing.T) {
  338. tests := []struct {
  339. name string
  340. data string
  341. wantMessage string
  342. }{
  343. {name: "empty document", data: "", wantMessage: "opml: invalid document: no opml element found"},
  344. {name: "plain text", data: "not xml", wantMessage: "opml: invalid document: no opml element found"},
  345. {name: "malformed XML", data: `<opml version="2.0"><body><outline xmlUrl="https://example.org/feed.xml"`},
  346. {name: "wrong root element", data: `<rss version="2.0"><channel></channel></rss>`},
  347. {name: "invalid miniflux attribute", data: `<opml version="2.0" xmlns:miniflux="https://miniflux.app/opml"><body><outline xmlUrl="https://example.org/feed.xml" miniflux:crawler="maybe"/></body></opml>`},
  348. }
  349. for _, tc := range tests {
  350. t.Run(tc.name, func(t *testing.T) {
  351. _, err := parse(bytes.NewBufferString(tc.data))
  352. if !errors.Is(err, ErrInvalidDocument) {
  353. t.Fatalf("expected ErrInvalidDocument, got %v", err)
  354. }
  355. if tc.wantMessage != "" && err.Error() != tc.wantMessage {
  356. t.Fatalf("expected error message %q, got %q", tc.wantMessage, err.Error())
  357. }
  358. })
  359. }
  360. }