-
Notifications
You must be signed in to change notification settings - Fork 11
/
feed_manager_test.go
149 lines (116 loc) · 2.99 KB
/
feed_manager_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
package readeef
/*
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/urandom/readeef"
)
func TestFeedManager(t *testing.T) {
cleanDB(t)
var ts *httptest.Server
done := make(chan bool)
rec := updateReceiver{updateFeed: make(chan Feed)}
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() { done <- true }()
if r.RequestURI == "/link" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Replace(atomXml, "{{ .FeedLink }}", ts.URL+"/link", -1)))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
conf, err := ReadConfig()
if err != nil {
t.Fatal(err)
}
conf.Updater.Converted.Interval = 100 * time.Millisecond
fm := NewFeedManager(db, conf, readeef.NewStandardLogger(os.Stderr, "", 0), &UpdateFeedReceiverManager{})
fm.AddUpdateReceiver(rec)
fm.Start()
f := Feed{Link: ts.URL + "/link"}
f, _, err = db.UpdateFeed(f)
if err != nil {
t.Fatal(err)
}
fm.AddFeed(f)
<-done // First update request
f2 := <-rec.updateFeed // Feed gets updated
f, err = db.GetFeed(f.Id)
if err != nil {
t.Fatal(err)
}
if f.SubscribeError != "" {
t.Fatal(f.SubscribeError)
}
expectedStr := "Example Feed"
if f.Title != expectedStr {
t.Fatalf("Expected feed title to be '%s', got '%s'\n", expectedStr, f.Title)
}
if f.Link != f2.Link || f.Title != f2.Title || f.Description != f2.Description {
t.Fatal("")
}
<-done // Second update request
fm.RemoveFeed(f)
time.Sleep(150 * time.Millisecond)
select {
case <-done:
t.Fatalf("Did not expect another request\n")
default:
}
}
func TestFeedManagerDetection(t *testing.T) {
var ts *httptest.Server
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/link" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Replace(atomXml, "{{ .FeedLink }}", ts.URL+"/link", -1)))
} else if r.RequestURI == "/html" {
w.Write([]byte(`
<html>
<head>
<link type="text/css" href="/foo.css">
<link rel="alternative" type="application/rss+xml" href="/link"/>
</head>
<body><main></main></body>
</html>
`))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
_, err := discoverParserFeeds(ts.URL)
if err == nil {
t.Fatalf("Expected an ErrNoFeed error, got nothing\n")
} else if err != ErrNoFeed {
t.Fatalf("Expected an ErrNoFeed error, got %v\n", err)
}
pf, err := discoverParserFeeds(ts.URL + "/link")
if err != nil {
t.Fatal(err)
}
expectedStr := ts.URL + "/link"
if pf[0].Link != expectedStr {
t.Fatalf("Expected '%s' for a url, got '%s'\n", expectedStr, pf[0].Link)
}
pf, err = discoverParserFeeds(ts.URL + "/html")
if err != nil {
t.Fatal(err)
}
expectedStr = ts.URL + "/link"
if pf[0].Link != expectedStr {
t.Fatalf("Expected '%s' for a url, got '%s'\n", expectedStr, pf[0].Link)
}
}
type updateReceiver struct {
updateFeed chan Feed
}
func (r updateReceiver) UpdateFeedChannel() chan<- Feed {
return r.updateFeed
}
*/