parser_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // equals compare two subscriptions.
  9. func (s subcription) equals(subscription subcription) bool {
  10. return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL &&
  11. s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName &&
  12. s.Description == subscription.Description
  13. }
  14. func TestParseOpmlWithoutCategories(t *testing.T) {
  15. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  16. <opml version="2.0">
  17. <head>
  18. <title>mySubscriptions.opml</title>
  19. </head>
  20. <body>
  21. <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"/>
  22. <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"/>
  23. <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"/>
  24. <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"/>
  25. <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"/>
  26. <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"/>
  27. <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"/>
  28. <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"/>
  29. <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"/>
  30. <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"/>
  31. <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"/>
  32. <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"/>
  33. <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"/>
  34. </body>
  35. </opml>
  36. `
  37. var expected []subcription
  38. 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."})
  39. subscriptions, err := parse(bytes.NewBufferString(data))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if len(subscriptions) != 13 {
  44. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
  45. }
  46. if !subscriptions[0].equals(expected[0]) {
  47. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[0], expected[0])
  48. }
  49. }
  50. func TestParseOpmlWithCategories(t *testing.T) {
  51. data := `<?xml version="1.0" encoding="utf-8"?>
  52. <opml version="2.0">
  53. <head>
  54. <title>mySubscriptions.opml</title>
  55. </head>
  56. <body>
  57. <outline text="My Category 1">
  58. <outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  59. <outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
  60. </outline>
  61. <outline text="My Category 2">
  62. <outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
  63. </outline>
  64. </body>
  65. </opml>
  66. `
  67. var expected []subcription
  68. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
  69. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
  70. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})
  71. subscriptions, err := parse(bytes.NewBufferString(data))
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if len(subscriptions) != 3 {
  76. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  77. }
  78. for i := range len(subscriptions) {
  79. if !subscriptions[i].equals(expected[i]) {
  80. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  81. }
  82. }
  83. }
  84. func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
  85. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  86. <opml version="2.0">
  87. <head>
  88. <title>mySubscriptions.opml</title>
  89. </head>
  90. <body>
  91. <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  92. <outline xmlUrl="http://example.org/feed2/"/>
  93. </body>
  94. </opml>
  95. `
  96. var expected []subcription
  97. expected = append(expected, subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  98. expected = append(expected, subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
  99. subscriptions, err := parse(bytes.NewBufferString(data))
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if len(subscriptions) != 2 {
  104. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  105. }
  106. for i := range len(subscriptions) {
  107. if !subscriptions[i].equals(expected[i]) {
  108. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  109. }
  110. }
  111. }
  112. func TestParseOpmlVersion1(t *testing.T) {
  113. data := `<?xml version="1.0"?>
  114. <opml version="1.0">
  115. <head>
  116. <title>mySubscriptions.opml</title>
  117. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  118. </head>
  119. <body>
  120. <outline title="Category 1">
  121. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  122. </outline>
  123. <outline title="Category 2">
  124. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  125. </outline>
  126. </body>
  127. </opml>
  128. `
  129. var expected []subcription
  130. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  131. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"})
  132. subscriptions, err := parse(bytes.NewBufferString(data))
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if len(subscriptions) != 2 {
  137. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  138. }
  139. for i := range len(subscriptions) {
  140. if !subscriptions[i].equals(expected[i]) {
  141. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  142. }
  143. }
  144. }
  145. func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) {
  146. data := `<?xml version="1.0"?>
  147. <opml version="1.0">
  148. <head>
  149. <title>mySubscriptions.opml</title>
  150. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  151. </head>
  152. <body>
  153. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  154. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  155. </body>
  156. </opml>
  157. `
  158. var expected []subcription
  159. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  160. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})
  161. subscriptions, err := parse(bytes.NewBufferString(data))
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if len(subscriptions) != 2 {
  166. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  167. }
  168. for i := range len(subscriptions) {
  169. if !subscriptions[i].equals(expected[i]) {
  170. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  171. }
  172. }
  173. }
  174. func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) {
  175. data := `<?xml version="1.0"?>
  176. <opml xmlns:rssowl="http://www.rssowl.org" version="1.1">
  177. <head>
  178. <title>RSSOwl Subscriptions</title>
  179. <dateCreated>星期二, 26 四月 2022 00:12:04 CST</dateCreated>
  180. </head>
  181. <body>
  182. <outline text="My Feeds" rssowl:isSet="true" rssowl:id="7">
  183. <outline text="Some Category" rssowl:isSet="false" rssowl:id="55">
  184. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  185. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  186. </outline>
  187. <outline text="Another Category" rssowl:isSet="false" rssowl:id="87">
  188. <outline type="rss" title="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"></outline>
  189. </outline>
  190. </outline>
  191. </body>
  192. </opml>
  193. `
  194. var expected []subcription
  195. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"})
  196. expected = append(expected, subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"})
  197. expected = append(expected, subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"})
  198. subscriptions, err := parse(bytes.NewBufferString(data))
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. if len(subscriptions) != 3 {
  203. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  204. }
  205. for i := range len(subscriptions) {
  206. if !subscriptions[i].equals(expected[i]) {
  207. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  208. }
  209. }
  210. }
  211. func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) {
  212. data := `<?xml version="1.0"?>
  213. <opml version="1.0">
  214. <head>
  215. <title>mySubscriptions.opml</title>
  216. </head>
  217. <body>
  218. <outline title="Feed 1">
  219. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/a&b" htmlUrl="http://example.org/c&d"></outline>
  220. </outline>
  221. </body>
  222. </opml>
  223. `
  224. var expected []subcription
  225. expected = append(expected, subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"})
  226. subscriptions, err := parse(bytes.NewBufferString(data))
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. if len(subscriptions) != 1 {
  231. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  232. }
  233. for i := range len(subscriptions) {
  234. if !subscriptions[i].equals(expected[i]) {
  235. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  236. }
  237. }
  238. }
  239. func TestParseInvalidXML(t *testing.T) {
  240. data := `garbage`
  241. _, err := parse(bytes.NewBufferString(data))
  242. if err == nil {
  243. t.Error("Parse should generate an error")
  244. }
  245. }