aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/opml/parser_test.go
blob: f6ce6346761463cc2efdc65005cfa35971bb5b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package opml // import "miniflux.app/reader/opml"

import (
	"bytes"
	"testing"
)

func TestParseOpmlWithoutCategories(t *testing.T) {
	data := `<?xml version="1.0" encoding="ISO-8859-1"?>
	<opml version="2.0">
		<head>
			<title>mySubscriptions.opml</title>
		</head>
		<body>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
			<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"/>
		</body>
	</opml>
	`

	var expected SubcriptionList
	expected = append(expected, &Subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/"})

	subscriptions, err := Parse(bytes.NewBufferString(data))
	if err != nil {
		t.Error(err)
	}

	if len(subscriptions) != 13 {
		t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
	}

	if !subscriptions[0].Equals(expected[0]) {
		t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[0], expected[0])
	}
}

func TestParseOpmlWithCategories(t *testing.T) {
	data := `<?xml version="1.0" encoding="utf-8"?>
	<opml version="2.0">
		<head>
			<title>mySubscriptions.opml</title>
		</head>
		<body>
			<outline text="My Category 1">
				<outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
				<outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
			</outline>
			<outline text="My Category 2">
			<outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
		</outline>
		</body>
	</opml>
	`

	var expected SubcriptionList
	expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
	expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
	expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})

	subscriptions, err := Parse(bytes.NewBufferString(data))
	if err != nil {
		t.Error(err)
	}

	if len(subscriptions) != 3 {
		t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
	}

	for i := 0; i < len(subscriptions); i++ {
		if !subscriptions[i].Equals(expected[i]) {
			t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
		}
	}
}

func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
	data := `<?xml version="1.0" encoding="ISO-8859-1"?>
	<opml version="2.0">
	<head>
	<title>mySubscriptions.opml</title>
	</head>
	<body>
		<outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
		<outline xmlUrl="http://example.org/feed2/"/>
	</body>
	</opml>
	`

	var expected SubcriptionList
	expected = append(expected, &Subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
	expected = append(expected, &Subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})

	subscriptions, err := Parse(bytes.NewBufferString(data))
	if err != nil {
		t.Error(err)
	}

	if len(subscriptions) != 2 {
		t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
	}

	for i := 0; i < len(subscriptions); i++ {
		if !subscriptions[i].Equals(expected[i]) {
			t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
		}
	}
}

func TestParseOpmlVersion1(t *testing.T) {
	data := `<?xml version="1.0"?>
	<opml version="1.0">
		<head>
			<title>mySubscriptions.opml</title>
			<dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
		</head>
		<body>
			<outline title="Feed 1">
				<outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
			</outline>
			<outline title="Feed 2">
				<outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
			</outline>
		</body>
	</opml>
	`

	var expected SubcriptionList
	expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
	expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})

	subscriptions, err := Parse(bytes.NewBufferString(data))
	if err != nil {
		t.Error(err)
	}

	if len(subscriptions) != 2 {
		t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
	}

	for i := 0; i < len(subscriptions); i++ {
		if !subscriptions[i].Equals(expected[i]) {
			t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
		}
	}
}

func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) {
	data := `<?xml version="1.0"?>
	<opml version="1.0">
		<head>
			<title>mySubscriptions.opml</title>
			<dateCreated>Wed, 13 Mar 2019 11:51:41 GMT</dateCreated>
		</head>
		<body>
			<outline type="rss" title="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"></outline>
			<outline type="rss" title="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"></outline>
		</body>
	</opml>
	`

	var expected SubcriptionList
	expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
	expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""})

	subscriptions, err := Parse(bytes.NewBufferString(data))
	if err != nil {
		t.Error(err)
	}

	if len(subscriptions) != 2 {
		t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
	}

	for i := 0; i < len(subscriptions); i++ {
		if !subscriptions[i].Equals(expected[i]) {
			t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
		}
	}
}
func TestParseInvalidXML(t *testing.T) {
	data := `garbage`
	_, err := Parse(bytes.NewBufferString(data))
	if err == nil {
		t.Error("Parse should generate an error")
	}
}