-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed_editor_test.go
272 lines (227 loc) · 9.44 KB
/
feed_editor_test.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"strings"
"testing"
"github.com/beevik/etree"
"github.com/stretchr/testify/assert"
)
func TestRSSFeedEditor(t *testing.T) {
rssfeed := `
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>ぽ靴な缶</title>
<link>https://blog.pokutuna.com/</link>
<description></description>
<lastBuildDate>Fri, 11 Mar 2022 11:00:00 +0900</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Hatena::Blog</generator>
<item>
<title>Title 1</title>
<link>https://blog.pokutuna.com/entry/1</link>
<description>Hello This is a <a href="https://example.test">link</a></description>
<pubDate>Fri, 11 Mar 2022 11:00:00 +0900</pubDate>
<guid isPermalink="false">hatenablog://entry/13574176438071665569</guid>
</item>
<item>
<title>Title 2</title>
<link>https://blog.pokutuna.com/entry/2</link>
<description><![CDATA[Hello This is a <a href="https://example.test">link</a>]]></description>
<pubDate>Thu, 02 Dec 2021 22:00:00 +0900</pubDate>
<guid isPermalink="false">hatenablog://entry/13574176438038846201</guid>
</item>
<item>
<title>Title 3</title>
<link>https://blog.pokutuna.com/entry/3</link>
<content:encoded><![CDATA[Hello This is a <a href="https://example.test">link</a>]]></content:encoded>
<pubDate>Thu, 03 Dec 2021 03:00:00 +0900</pubDate>
<guid isPermalink="false">hatenablog://entry/3</guid>
</item>
</channel>
</rss>
`
editor := RSSFeedEditor{}
var doc *etree.Document
reload := func() {
doc = etree.NewDocument()
_, err := doc.ReadFrom(strings.NewReader(rssfeed))
assert.NoError(t, err)
}
t.Run("UpdateFeedTitle", func(t *testing.T) {
reload()
editor.UpdateFeedTitle(doc)
title := doc.FindElement("/rss/channel/title").Text()
assert.Equal(t, "ぽ靴な缶 (with feed-pruning-proxy)", title)
})
t.Run("RemoveEntryContent", func(t *testing.T) {
t.Run("description", func(t *testing.T) {
reload()
item := doc.FindElement("/rss/channel/item")
assert.NotNil(t, item.SelectElement("description"))
editor.RemoveEntryContent(doc)
assert.Nil(t, item.SelectElement("description"))
})
t.Run("content:encoded", func(t *testing.T) {
reload()
item := doc.FindElements("/rss/channel/item")[2]
assert.NotNil(t, item.SelectElement("content:encoded"))
editor.RemoveEntryContent(doc)
assert.Nil(t, item.SelectElement("content:encoded"))
})
})
t.Run("DietEntryContent", func(t *testing.T) {
reload()
items := doc.FindElements("/rss/channel/item")
item1 := items[0] // description with Escaped
item2 := items[1] // description with CDATA
item3 := items[2] // content:encoded with CDATA
editor.DietEntryContent(doc)
assert.Equal(t, "Hello This is a link", item1.SelectElement("description").Text())
assert.Equal(t, "Hello This is a link", item2.SelectElement("description").Text())
assert.Equal(t, "Hello This is a link", item3.SelectElement("content:encoded").Text())
})
t.Run("TapRedirector", func(t *testing.T) {
})
}
func TestAtomFeedEditor(t *testing.T) {
var atomfeed = `
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
<title>ぽ靴な缶</title>
<link href="https://blog.pokutuna.com/"/>
<updated>2022-03-11T11:00:00+09:00</updated>
<author>
<name>pokutuna</name>
</author>
<generator uri="https://blog.hatena.ne.jp/" version="7f2239af4151fedf9100f66511debb">Hatena::Blog</generator>
<id>hatenablog://blog/12704591929886254776</id>
<entry>
<title>Title 1</title>
<link href="https://blog.pokutuna.com/entry/1"/>
<id>hatenablog://entry/13574176438071665569</id>
<published>2022-03-11T11:00:00+09:00</published>
<updated>2022-03-11T11:00:03+09:00</updated>
<summary type="html">Hello This is a <a href="https://example.test">link</a> in summary</summary>
<content type="html">Hello This is a <a href="https://example.test">link</a> in content</content>
<author>
<name>pokutuna</name>
</author>
</entry>
<entry>
<title>Title 2</title>
<link href="https://blog.pokutuna.com/entry/2"/>
<id>hatenablog://entry/13574176438038846201</id>
<published>2021-12-02T22:00:00+09:00</published>
<updated>2021-12-10T14:10:08+09:00</updated>
<summary type="html"><![CDATA[Hello This is a <a href="https://example.test">link</a> in summary]]></summary>
<content type="html"><![CDATA[Hello This is a <a href="https://example.test">link</a> in content]]></content>
<author>
<name>pokutuna</name>
</author>
</entry>
</feed>
`
editor := AtomFeedEditor{}
var doc *etree.Document
reload := func() {
doc = etree.NewDocument()
_, err := doc.ReadFrom(strings.NewReader(atomfeed))
assert.NoError(t, err)
}
t.Run("UpdateFeedTitle", func(t *testing.T) {
reload()
editor.UpdateFeedTitle(doc)
title := doc.FindElement("/feed/title").Text()
assert.Equal(t, "ぽ靴な缶 (with feed-pruning-proxy)", title)
})
t.Run("RemoveEntryContent", func(t *testing.T) {
reload()
item := doc.FindElement("/feed/entry")
assert.NotNil(t, item.SelectElement("summary"))
assert.NotNil(t, item.SelectElement("content"))
editor.RemoveEntryContent(doc)
assert.Nil(t, item.SelectElement("summary"))
assert.Nil(t, item.SelectElement("content"))
})
t.Run("DietEntryContent", func(t *testing.T) {
reload()
items := doc.FindElements("/feed/entry")
item1 := items[0] // Escaped
item2 := items[1] // CDATA
editor.DietEntryContent(doc)
assert.Equal(t, "Hello This is a link in summary", item1.SelectElement("summary").Text())
assert.Equal(t, "Hello This is a link in content", item1.SelectElement("content").Text())
assert.Equal(t, "Hello This is a link in summary", item2.SelectElement("summary").Text())
assert.Equal(t, "Hello This is a link in content", item2.SelectElement("content").Text())
})
t.Run("TapRedirector", func(t *testing.T) {
})
}
func TestRDFFeedEditor(t *testing.T) {
var rdffeed = `
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:hatena="http://www.hatena.ne.jp/info/xmlns#" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/">
<channel rdf:about="https://blog.pokutuna.com/">
<title>ぽ靴な缶</title>
<link>https://blog.pokutuna.com/</link>
<description>pokutuna</description>
<items>
<rdf:Seq>
<rdf:li rdf:resource="https://blog.pokutuna.com/entry/1"/>
<rdf:li rdf:resource="https://blog.pokutuna.com/entry/2"/>
<rdf:li rdf:resource="https://blog.pokutuna.com/entry/3"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="https://blog.pokutuna.com/entry/1">
<title>Title 1</title>
<link>https://blog.pokutuna.com/entry/1</link>
<description>description</description>
<dc:creator>pokutuna</dc:creator>
<dc:date>2022-01-01T00:00:00Z</dc:date>
<content:encoded>Hello This is a <a href="https://example.test">link</a></content:encoded>
<dc:subject>HTML Encoded</dc:subject>
</item>
<item rdf:about="https://blog.pokutuna.com/entry/2">
<title>Title 2</title>
<link>https://blog.pokutuna.com/entry/2</link>
<description>description</description>
<dc:creator>pokutuna</dc:creator>
<dc:date>2022-02-02T00:00:00Z</dc:date>
<content:encoded><![CDATA[Hello This is a <a href="https://example.test">link</a>]]></content:encoded>
<dc:subject>CDATA</dc:subject>
</item>
</rdf:RDF>
`
editor := RDFFeedEditor{}
var doc *etree.Document
reload := func() {
doc = etree.NewDocument()
_, err := doc.ReadFrom(strings.NewReader(rdffeed))
assert.NoError(t, err)
}
t.Run("UpdateFeedTitle", func(t *testing.T) {
reload()
editor.UpdateFeedTitle(doc)
title := doc.FindElement("/rdf:RDF/channel[@rdf:about]/title").Text()
assert.Equal(t, "ぽ靴な缶 (with feed-pruning-proxy)", title)
})
t.Run("RemoveEntryContent", func(t *testing.T) {
reload()
item := doc.FindElement("/rdf:RDF/item[@rdf:about]")
assert.NotNil(t, item.SelectElement("description"))
assert.NotNil(t, item.SelectElement("content:encoded"))
editor.RemoveEntryContent(doc)
assert.Nil(t, item.SelectElement("description"))
assert.Nil(t, item.SelectElement("content:encoded"))
})
t.Run("DietEntryContent", func(t *testing.T) {
reload()
items := doc.FindElements("/rdf:RDF/item[@rdf:about]")
item1 := items[0] // escaped
item2 := items[1] // CDATA
editor.DietEntryContent(doc)
assert.Equal(t, "Hello This is a link", item1.SelectElement("content:encoded").Text())
assert.Equal(t, "Hello This is a link", item2.SelectElement("content:encoded").Text())
})
t.Run("TapRedirector", func(t *testing.T) {
})
}