parser_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. "testing"
  7. )
  8. func TestParseOpmlWithoutCategories(t *testing.T) {
  9. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  10. <opml version="2.0">
  11. <head>
  12. <title>mySubscriptions.opml</title>
  13. </head>
  14. <body>
  15. <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"/>
  16. <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"/>
  17. <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"/>
  18. <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"/>
  19. <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"/>
  20. <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"/>
  21. <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"/>
  22. <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"/>
  23. <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"/>
  24. <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"/>
  25. <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"/>
  26. <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"/>
  27. <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"/>
  28. </body>
  29. </opml>
  30. `
  31. var expected []subcription
  32. 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."})
  33. subscriptions, err := parse(bytes.NewBufferString(data))
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if len(subscriptions) != 13 {
  38. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
  39. }
  40. if subscriptions[0] != expected[0] {
  41. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[0], expected[0])
  42. }
  43. }
  44. func TestParseOpmlWithCategories(t *testing.T) {
  45. data := `<?xml version="1.0" encoding="utf-8"?>
  46. <opml version="2.0">
  47. <head>
  48. <title>mySubscriptions.opml</title>
  49. </head>
  50. <body>
  51. <outline text="My Category 1">
  52. <outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  53. <outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
  54. </outline>
  55. <outline text="My Category 2">
  56. <outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
  57. </outline>
  58. </body>
  59. </opml>
  60. `
  61. var expected []subcription
  62. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
  63. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
  64. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})
  65. subscriptions, err := parse(bytes.NewBufferString(data))
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if len(subscriptions) != 3 {
  70. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  71. }
  72. for i := range len(subscriptions) {
  73. if subscriptions[i] != expected[i] {
  74. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  75. }
  76. }
  77. }
  78. func TestParseOpmlTrimsWhitespace(t *testing.T) {
  79. data := `<?xml version="1.0" encoding="utf-8"?>
  80. <opml version="2.0">
  81. <body>
  82. <outline text=" My Category ">
  83. <outline title=" Feed 1 " xmlUrl=" http://example.org/feed1/ " htmlUrl=" http://example.org/1 "/>
  84. <outline title=" " xmlUrl="&#10;http://example.org/feed2/&#9;"/>
  85. </outline>
  86. </body>
  87. </opml>
  88. `
  89. expected := []subcription{
  90. {Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category"},
  91. {Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: "My Category"},
  92. }
  93. subscriptions, err := parse(bytes.NewBufferString(data))
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if len(subscriptions) != len(expected) {
  98. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), len(expected))
  99. }
  100. for i := range len(subscriptions) {
  101. if subscriptions[i] != expected[i] {
  102. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  103. }
  104. }
  105. }
  106. func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
  107. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  108. <opml version="2.0">
  109. <head>
  110. <title>mySubscriptions.opml</title>
  111. </head>
  112. <body>
  113. <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  114. <outline xmlUrl="http://example.org/feed2/"/>
  115. </body>
  116. </opml>
  117. `
  118. var expected []subcription
  119. expected = append(expected, subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  120. expected = append(expected, subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
  121. subscriptions, err := parse(bytes.NewBufferString(data))
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if len(subscriptions) != 2 {
  126. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  127. }
  128. for i := range len(subscriptions) {
  129. if subscriptions[i] != expected[i] {
  130. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  131. }
  132. }
  133. }
  134. func TestParseOpmlVersion1(t *testing.T) {
  135. data := `<?xml version="1.0"?>
  136. <opml version="1.0">
  137. <head>
  138. <title>mySubscriptions.opml</title>
  139. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  140. </head>
  141. <body>
  142. <outline title="Category 1">
  143. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  144. </outline>
  145. <outline title="Category 2">
  146. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  147. </outline>
  148. </body>
  149. </opml>
  150. `
  151. var expected []subcription
  152. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  153. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"})
  154. subscriptions, err := parse(bytes.NewBufferString(data))
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. if len(subscriptions) != 2 {
  159. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  160. }
  161. for i := range len(subscriptions) {
  162. if subscriptions[i] != expected[i] {
  163. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  164. }
  165. }
  166. }
  167. func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) {
  168. data := `<?xml version="1.0"?>
  169. <opml version="1.0">
  170. <head>
  171. <title>mySubscriptions.opml</title>
  172. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  173. </head>
  174. <body>
  175. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  176. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  177. </body>
  178. </opml>
  179. `
  180. var expected []subcription
  181. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  182. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})
  183. subscriptions, err := parse(bytes.NewBufferString(data))
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if len(subscriptions) != 2 {
  188. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  189. }
  190. for i := range len(subscriptions) {
  191. if subscriptions[i] != expected[i] {
  192. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  193. }
  194. }
  195. }
  196. func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) {
  197. data := `<?xml version="1.0"?>
  198. <opml xmlns:rssowl="http://www.rssowl.org" version="1.1">
  199. <head>
  200. <title>RSSOwl Subscriptions</title>
  201. <dateCreated>星期二, 26 四月 2022 00:12:04 CST</dateCreated>
  202. </head>
  203. <body>
  204. <outline text="My Feeds" rssowl:isSet="true" rssowl:id="7">
  205. <outline text="Some Category" rssowl:isSet="false" rssowl:id="55">
  206. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  207. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  208. </outline>
  209. <outline text="Another Category" rssowl:isSet="false" rssowl:id="87">
  210. <outline type="rss" title="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"></outline>
  211. </outline>
  212. </outline>
  213. </body>
  214. </opml>
  215. `
  216. var expected []subcription
  217. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"})
  218. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"})
  219. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"})
  220. subscriptions, err := parse(bytes.NewBufferString(data))
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. if len(subscriptions) != 3 {
  225. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  226. }
  227. for i := range len(subscriptions) {
  228. if subscriptions[i] != expected[i] {
  229. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  230. }
  231. }
  232. }
  233. func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) {
  234. data := `<?xml version="1.0"?>
  235. <opml version="1.0">
  236. <head>
  237. <title>mySubscriptions.opml</title>
  238. </head>
  239. <body>
  240. <outline title="Feed 1">
  241. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/a&b" htmlUrl="http://example.org/c&d"></outline>
  242. </outline>
  243. </body>
  244. </opml>
  245. `
  246. var expected []subcription
  247. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"})
  248. subscriptions, err := parse(bytes.NewBufferString(data))
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. if len(subscriptions) != 1 {
  253. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  254. }
  255. for i := range len(subscriptions) {
  256. if subscriptions[i] != expected[i] {
  257. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  258. }
  259. }
  260. }
  261. func TestParseInvalidXML(t *testing.T) {
  262. data := `garbage`
  263. _, err := parse(bytes.NewBufferString(data))
  264. if err == nil {
  265. t.Error("Parse should generate an error")
  266. }
  267. }
  268. func TestParseOpmlWithMinifluxSettings(t *testing.T) {
  269. data := `<?xml version="1.0" encoding="utf-8"?>
  270. <opml version="2.0" xmlns:miniflux="https://miniflux.app/opml">
  271. <head><title>Miniflux</title></head>
  272. <body>
  273. <outline text="My Category">
  274. <outline type="rss"
  275. text="Feed 1"
  276. title="Feed 1"
  277. xmlUrl="http://example.org/feed1/"
  278. htmlUrl="http://example.org/1"
  279. miniflux:scraperRules="article.content"
  280. miniflux:rewriteRules="replace(&quot;foo&quot;|&quot;bar&quot;)"
  281. miniflux:urlRewriteRules="rewrite(&quot;^https://old&quot;|&quot;https://new&quot;)"
  282. miniflux:blocklistRules="sponsored"
  283. miniflux:keeplistRules="important"
  284. miniflux:blockFilterEntryRules="EntryTitle=~&quot;ad&quot;"
  285. miniflux:keepFilterEntryRules="EntryTitle=~&quot;news&quot;"
  286. miniflux:userAgent="CustomAgent/1.0"
  287. miniflux:proxyUrl="http://proxy.example.org"
  288. miniflux:crawler="true"
  289. miniflux:ignoreHTTPCache="true"
  290. miniflux:fetchViaProxy="true"
  291. miniflux:disabled="true"
  292. miniflux:noMediaPlayer="true"
  293. miniflux:hideGlobally="true"
  294. miniflux:allowSelfSignedCertificates="true"
  295. miniflux:disableHTTP2="true"
  296. miniflux:ignoreEntryUpdates="true"
  297. />
  298. </outline>
  299. </body>
  300. </opml>
  301. `
  302. expected := subcription{
  303. Title: "Feed 1",
  304. FeedURL: "http://example.org/feed1/",
  305. SiteURL: "http://example.org/1",
  306. CategoryName: "My Category",
  307. ScraperRules: "article.content",
  308. RewriteRules: `replace("foo"|"bar")`,
  309. UrlRewriteRules: `rewrite("^https://old"|"https://new")`,
  310. BlocklistRules: "sponsored",
  311. KeeplistRules: "important",
  312. BlockFilterEntryRules: `EntryTitle=~"ad"`,
  313. KeepFilterEntryRules: `EntryTitle=~"news"`,
  314. UserAgent: "CustomAgent/1.0",
  315. Crawler: true,
  316. IgnoreHTTPCache: true,
  317. FetchViaProxy: true,
  318. Disabled: true,
  319. NoMediaPlayer: true,
  320. HideGlobally: true,
  321. AllowSelfSignedCertificates: true,
  322. DisableHTTP2: true,
  323. IgnoreEntryUpdates: true,
  324. }
  325. subscriptions, err := parse(bytes.NewBufferString(data))
  326. if err != nil {
  327. t.Fatal(err)
  328. }
  329. if len(subscriptions) != 1 {
  330. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  331. }
  332. if subscriptions[0] != expected {
  333. t.Errorf("Subscription is different:\ngot: %+v\nwant: %+v", subscriptions[0], expected)
  334. }
  335. }