parser_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 SubcriptionList
  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].Equals(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 SubcriptionList
  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].Equals(expected[i]) {
  74. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  75. }
  76. }
  77. }
  78. func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
  79. data := `<?xml version="1.0" encoding="ISO-8859-1"?>
  80. <opml version="2.0">
  81. <head>
  82. <title>mySubscriptions.opml</title>
  83. </head>
  84. <body>
  85. <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
  86. <outline xmlUrl="http://example.org/feed2/"/>
  87. </body>
  88. </opml>
  89. `
  90. var expected SubcriptionList
  91. expected = append(expected, &Subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  92. expected = append(expected, &Subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
  93. subscriptions, err := Parse(bytes.NewBufferString(data))
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if len(subscriptions) != 2 {
  98. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  99. }
  100. for i := range len(subscriptions) {
  101. if !subscriptions[i].Equals(expected[i]) {
  102. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  103. }
  104. }
  105. }
  106. func TestParseOpmlVersion1(t *testing.T) {
  107. data := `<?xml version="1.0"?>
  108. <opml version="1.0">
  109. <head>
  110. <title>mySubscriptions.opml</title>
  111. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  112. </head>
  113. <body>
  114. <outline title="Category 1">
  115. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  116. </outline>
  117. <outline title="Category 2">
  118. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  119. </outline>
  120. </body>
  121. </opml>
  122. `
  123. var expected SubcriptionList
  124. expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
  125. expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"})
  126. subscriptions, err := Parse(bytes.NewBufferString(data))
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if len(subscriptions) != 2 {
  131. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
  132. }
  133. for i := range len(subscriptions) {
  134. if !subscriptions[i].Equals(expected[i]) {
  135. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  136. }
  137. }
  138. }
  139. func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) {
  140. data := `<?xml version="1.0"?>
  141. <opml version="1.0">
  142. <head>
  143. <title>mySubscriptions.opml</title>
  144. <dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
  145. </head>
  146. <body>
  147. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  148. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  149. </body>
  150. </opml>
  151. `
  152. var expected SubcriptionList
  153. expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
  154. expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})
  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].Equals(expected[i]) {
  164. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  165. }
  166. }
  167. }
  168. func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) {
  169. data := `<?xml version="1.0"?>
  170. <opml xmlns:rssowl="http://www.rssowl.org" version="1.1">
  171. <head>
  172. <title>RSSOwl Subscriptions</title>
  173. <dateCreated>星期二, 26 四月 2022 00:12:04 CST</dateCreated>
  174. </head>
  175. <body>
  176. <outline text="My Feeds" rssowl:isSet="true" rssowl:id="7">
  177. <outline text="Some Category" rssowl:isSet="false" rssowl:id="55">
  178. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
  179. <outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
  180. </outline>
  181. <outline text="Another Category" rssowl:isSet="false" rssowl:id="87">
  182. <outline type="rss" title="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"></outline>
  183. </outline>
  184. </outline>
  185. </body>
  186. </opml>
  187. `
  188. var expected SubcriptionList
  189. expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"})
  190. expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"})
  191. expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"})
  192. subscriptions, err := Parse(bytes.NewBufferString(data))
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. if len(subscriptions) != 3 {
  197. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
  198. }
  199. for i := range len(subscriptions) {
  200. if !subscriptions[i].Equals(expected[i]) {
  201. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  202. }
  203. }
  204. }
  205. func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) {
  206. data := `<?xml version="1.0"?>
  207. <opml version="1.0">
  208. <head>
  209. <title>mySubscriptions.opml</title>
  210. </head>
  211. <body>
  212. <outline title="Feed 1">
  213. <outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/a&b" htmlUrl="http://example.org/c&d"></outline>
  214. </outline>
  215. </body>
  216. </opml>
  217. `
  218. var expected SubcriptionList
  219. expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"})
  220. subscriptions, err := Parse(bytes.NewBufferString(data))
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. if len(subscriptions) != 1 {
  225. t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1)
  226. }
  227. for i := range len(subscriptions) {
  228. if !subscriptions[i].Equals(expected[i]) {
  229. t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
  230. }
  231. }
  232. }
  233. func TestParseInvalidXML(t *testing.T) {
  234. data := `garbage`
  235. _, err := Parse(bytes.NewBufferString(data))
  236. if err == nil {
  237. t.Error("Parse should generate an error")
  238. }
  239. }