forked from mawenbao/gofeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofeed_test.go
349 lines (296 loc) · 10.1 KB
/
gofeed_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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"bytes"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"testing"
"time"
)
func init() {
// print detailed debug infomation
*gDebug = true
//*gVerbose = true
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
func TestExtractCacheLifetime(t *testing.T) {
sampleCacheTimeStr := []string{
"2d",
"100h",
"2D5h3M4s",
"",
"2d5d3s",
"2d3M4S10s",
"-1d",
"3d1sabcde",
"05m",
"abc",
}
expectCacheTime := []time.Duration{
2 * 24 * time.Hour,
100 * time.Hour,
(2*24+5)*time.Hour + 3*time.Minute + 4*time.Second,
time.Duration(-1),
time.Duration(-2),
time.Duration(-2),
time.Duration(-2),
time.Duration(-2),
time.Duration(-2),
time.Duration(-2),
}
for i, _ := range sampleCacheTimeStr {
actualCacheLife := ExtractCacheLifetime(sampleCacheTimeStr[i])
if expectCacheTime[i] != actualCacheLife {
t.Fatalf("extract cache lifetime failed, expect %d from %s, got %d", expectCacheTime[i], sampleCacheTimeStr[i], actualCacheLife)
}
}
}
func TestParseJsonConfig(t *testing.T) {
config_file := "example_config2.json"
feedTargets := ParseJsonConfig(config_file)
feedTar := feedTargets[0]
feedURL := "http://blog.atime.me"
if feedURL != feedTar.URLs[0].String() {
t.Fatalf("%s: failed to parse url, expected %s, got %s", config_file, feedURL, feedTar.URLs[0].String())
}
feedIndexPattern := `<div class="niu2-index-article-title"><span><a href="(?P<link>(?s).+?)">(?P<title>(?s).+?)</a></span></div>`
if feedIndexPattern != feedTar.IndexRegs[0].String() {
t.Fatalf("%s: failed to parse index pattern, expected %s, got %s", config_file, feedIndexPattern, feedTar.IndexRegs[0].String())
}
feedContentPattern := `<div class="clearfix visible-xs niu2-clearfix"></div>(?P<description>(?s).*?)<div id="content-comments">`
if feedContentPattern != feedTar.ContentRegs[0].String() {
t.Fatalf("%s: failed to parse content pattern, expected %s, got %s", config_file, feedContentPattern, feedTar.ContentRegs[0].String())
}
feedPath, _ := filepath.Abs(`blog.atime.me.xml`)
if feedPath != feedTar.FeedPath {
t.Fatalf("%s: failed to parse path, expected %s, got %s", config_file, feedPath, feedTar.FeedPath)
}
}
/*
func TestRequestHtml(t *testing.T) {
url := "http://blog.atime.me/agreement.html"
cache := HtmlCache { URL: url }
err := requestHtml(&cache)
if nil != err {
t.Fatalf("failed to crawl %s: %s", url, err)
}
testFile := "test_data/test_crawl.html"
expectData, err := ioutil.ReadFile(testFile)
if nil != err {
t.Fatalf("failed to read %s: %s", testFile, err)
}
if 0 != bytes.Compare(expectData, cache.Html) {
t.Fatalf("html data crawled from %s not equal to %s", url, testFile)
}
}
*/
func TestFetchHtml(t *testing.T) {
feedTargets := ParseJsonConfig("example_config2.json")
cacheDB := feedTargets[0].CacheDB
os.Remove(cacheDB)
err := CreateDBScheme(cacheDB)
if nil != err {
t.Fatalf("failed to create db at %s: %s", cacheDB, err)
}
defer os.Remove(cacheDB)
// new cache
url, _ := url.Parse("http://blog.atime.me/agreement.html")
cache, err := FetchHtml(url, cacheDB, time.Duration(-1))
if nil != err {
t.Fatalf("failed to fetch html %s", err)
}
if url != cache.URL {
t.Fatalf("wrong html cache, url not match")
}
cache2, err := GetHtmlCacheByURL(cacheDB, url.String())
if nil != err {
t.Fatalf("html cache not saved for url %s", url)
}
if cache.URL.String() != cache2.URL.String() ||
!cache.LastModified.Equal(*cache2.LastModified) ||
0 != bytes.Compare(cache.Html, cache2.Html) {
t.Logf("url %t, %s vs %s", cache.URL.String() == cache2.URL.String(), cache.URL.String(), cache2.URL.String())
t.Logf("lastmod %t, %s vs %s", cache.LastModified.Equal(*cache2.LastModified), cache.LastModified.String(), cache2.LastModified.String())
t.Logf("html length %t, %d vs %d", len(cache.Html) == len(cache2.Html), len(cache.Html), len(cache2.Html))
t.Fatalf("html cache not match")
}
// use old cache
cache4, err := FetchHtml(url, cacheDB, time.Duration(-1))
if nil != err || CACHE_NOT_MODIFIED != cache4.Status {
t.Fatalf("failed to reuse html cache for %s: %s", url, err)
}
}
func TestCheckPatterns(t *testing.T) {
invalidTargets := [...]TargetConfig{
TargetConfig{IndexPatterns: []string{""}, ContentPatterns: []string{""}},
TargetConfig{IndexPatterns: []string{"abc"}, ContentPatterns: []string{""}},
TargetConfig{IndexPatterns: []string{"abc"}, ContentPatterns: []string{"cde"}},
TargetConfig{IndexPatterns: []string{"abc{link}"}, ContentPatterns: []string{"cde"}},
TargetConfig{IndexPatterns: []string{"{title} {link}abc{title}"}, ContentPatterns: []string{"{description}"}},
TargetConfig{IndexPatterns: []string{"{*}abc{title}"}, ContentPatterns: []string{"{title}"}},
TargetConfig{IndexPatterns: []string{"{link}abc{title}"}, ContentPatterns: []string{"{title}{description}"}},
TargetConfig{IndexPatterns: []string{"{link}abc{title}"}, ContentPatterns: []string{"{link}{*}{description}"}},
}
validTargets := [...]TargetConfig{
TargetConfig{IndexPatterns: []string{"{link}abc{title}"}, ContentPatterns: []string{"{*}{description}"}},
TargetConfig{IndexPatterns: []string{"{link}abc{*}cde{title}"}, ContentPatterns: []string{"{description}"}},
}
for _, tar := range invalidTargets {
if CheckPatterns(&tar) {
t.Fatal("check patterns failed")
}
}
for _, tar := range validTargets {
if !CheckPatterns(&tar) {
t.Fatal("check pattern failed")
}
}
}
func TestExtractHtmlTitle(t *testing.T) {
blogURL, _ := url.Parse("http://blog.atime.me")
cache := HtmlCache{URL: blogURL}
resp, err := SendHttpRequest(&cache)
if nil != err {
t.Fatalf("failed to send http request to %s", cache.URL.String())
}
if err = ParseHttpResponse(resp, &cache); nil != err {
t.Fatalf("failed to parse http response for %s", cache.URL.String())
}
expectedTitle := "MWB日常笔记"
realTitle := ExtractHtmlTitle(cache.Html)
if expectedTitle != realTitle {
t.Fatalf("title mismatch, expected %s, got %s", expectedTitle, realTitle)
}
}
func TestMinifyHtml(t *testing.T) {
rawHtml := `<html>
<head> </head>
<body> Hello world
</body>
</html>`
expectedHtml := "<html><head></head><body>Hello world</body></html>"
if expectedHtml != string(MinifyHtml([]byte(rawHtml))) {
t.Fatal("failed to minify html")
}
}
/*
func TestFilterHtmlWithoutPattern(t *testing.T) {
feedTargets := ParseJsonConfig("example_config2.json")
cache := HtmlCache { URL: tar.URL }
err = RequestHtml(&cache)
if nil != err {
t.Fatal("failed to download web page")
}
htmlData := MinifyHtml(cache.Html)
if !FilterHtmlWithoutPattern(htmlData, tar.IndexPattern) {
t.Fatalf("filter without index pattern failed for target %s", tar.URL)
}
}
*/
func TestDB(t *testing.T) {
feedTargets := ParseJsonConfig("example_config2.json")
cacheDB := feedTargets[0].CacheDB
os.Remove(cacheDB)
err := CreateDBScheme(cacheDB)
if nil != err {
t.Fatalf("failed to create db %s: %s", cacheDB, err)
}
defer os.Remove(cacheDB)
url1, _ := url.Parse("http://blog.atime.me")
url2, _ := url.Parse("http://atime.me")
dateNow := time.Now()
cache := []*HtmlCache{
&HtmlCache{URL: url1, Date: &dateNow, LastModified: &dateNow, Html: []byte("hello world")},
&HtmlCache{URL: url2, Date: &dateNow, LastModified: &dateNow, Html: []byte("hello world")},
}
err = PutHtmlCache(cacheDB, cache)
if nil != err {
t.Fatalf("failed to insert records to db %s: %s", cacheDB, err)
}
cache2, err := GetHtmlCacheByURL(cacheDB, "no.cache")
if nil == err {
t.Fatalf("should not get html cache from an non-exist url")
}
cache2, err = GetHtmlCacheByURL(cacheDB, cache[0].URL.String())
if nil != err {
t.Fatalf("failed to get html cache for url %s: %s", cache[0].URL.String(), err)
}
if cache2.URL.String() != cache[0].URL.String() ||
cache2.LastModified.Format(http.TimeFormat) != cache[0].LastModified.Format(http.TimeFormat) ||
0 != bytes.Compare(cache2.Html, cache[0].Html) {
t.Fatalf("got wrong html cache")
}
// update db
cache2.CacheControl = "ok, I know this is not true"
err = UpdateHtmlCache(cacheDB, []*HtmlCache{cache2})
if nil != err {
t.Fatalf("failed to update db: %s", err)
}
cache3, err := GetHtmlCacheByURL(cacheDB, cache2.URL.String())
if cache2.CacheControl != cache3.CacheControl {
t.Fatalf("updated CacheControl does match, %s vs %s", cache2.CacheControl, cache3.CacheControl)
}
// test remove
err = DelHtmlCacheByURL(cacheDB, cache2.URL.String())
if nil != err {
t.Fatal("failed to remove record from db")
}
_, err = GetHtmlCacheByURL(cacheDB, cache2.URL.String())
if nil == err {
t.Fatal("failed to remove record from db, record still exists")
}
}
func TestGenerateRss2Feed(t *testing.T) {
feedTargets := ParseJsonConfig("example_config2.json")
cacheDB := feedTargets[0].CacheDB
os.Remove(cacheDB)
err := CreateDBScheme(cacheDB)
if nil != err {
t.Fatalf("failed to create db at %s: %s", cacheDB, err)
}
defer os.Remove(cacheDB)
feedTar := feedTargets[0]
feed, ok := ParseIndexHtml(feedTar)
if !ok {
t.Fatalf("failed to parse index html for feed target %s", feedTar.FeedPath)
}
if !ParseContentHtml(feedTar, feed) {
t.Fatalf("failed to parse content html for feed target %s", feedTar.FeedPath)
}
_, err = GenerateRss2Feed(feed)
if nil != err {
t.Fatalf("failed to generate rss")
}
}
func TestParseIndexAndContentHtml(t *testing.T) {
feedTargets := ParseJsonConfig("example_config2.json")
cacheDB := feedTargets[0].CacheDB
os.Remove(cacheDB)
err := CreateDBScheme(cacheDB)
if nil != err {
t.Fatalf("failed to create db at %s: %s", cacheDB, err)
}
defer os.Remove(cacheDB)
for _, tar := range feedTargets {
feed, ok := ParseIndexHtml(tar)
if !ok {
t.Fatalf("failed to parse index html for feed target %s", tar.FeedPath)
}
if !ParseContentHtml(tar, feed) {
t.Fatalf("failed to parse content html for feed target %s", tar.FeedPath)
}
for _, entry := range feed.Entries {
println("title", entry.Title)
println("link", entry.Link)
println("content length", len(entry.Content))
var entryDesc []rune = []rune(string(entry.Content))
if len(entryDesc) > 100 {
println("content summary", string(entryDesc)[:100])
} else {
println("content", string(entryDesc))
}
}
}
}