parser_test.go 13 KB

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