-
Notifications
You must be signed in to change notification settings - Fork 11
/
hubbub_test.go
212 lines (175 loc) · 5.16 KB
/
hubbub_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
package readeef
/*
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/urandom/webfw"
"github.com/urandom/webfw/context"
)
func TestHubbub(t *testing.T) {
cleanDB(t)
var ts *httptest.Server
var callbackURL string
done := make(chan bool)
webfwConfig, err := webfw.ReadConfig()
if err != nil {
t.Fatal(err)
}
dispatcher := webfw.NewDispatcher("/", webfwConfig)
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.RequestURI, "/callback/v0/") {
r.RequestURI = strings.Replace(r.RequestURI, "/callback/v0", "", -1)
}
defer func() { done <- true }()
dispatcher.ServeHTTP(w, r)
}))
defer ts.Close()
var conf Config
addFeed := make(chan Feed)
removeFeed := make(chan Feed)
go func() {
for {
select {
case <-addFeed:
case <-removeFeed:
}
}
}()
h := NewHubbub(db, conf, webfw.NewStandardLogger(os.Stderr, "", 0), "/callback/", addFeed, removeFeed, &UpdateFeedReceiverManager{})
f := Feed{Link: ts.URL + "/link"}
f, _, err = db.UpdateFeed(f)
if err != nil {
t.Fatal(err)
}
err = h.Subscribe(f)
if err != ErrNotConfigured {
t.Fatalf("Exepcted a ErrNotConfigured error, got: '%v'\n", err)
}
callbackURL = fmt.Sprintf("%s/callback/v0/hubbub/%d", ts.URL, f.Id)
conf.Hubbub.CallbackURL = ts.URL
conf.Hubbub.RelativePath = "/hubbub"
conf.Timeout.Converted.Connect = time.Second
conf.Timeout.Converted.ReadWrite = time.Second
h = NewHubbub(db, conf, webfw.NewStandardLogger(os.Stderr, "", 0), "/callback/", addFeed, removeFeed, &UpdateFeedReceiverManager{})
dispatcher.Handle(hublink_con{webfw.NewBasePatternController("/hublink", webfw.MethodAll, ""), callbackURL, ts.URL + "/link", done})
hc := NewHubbubController(h)
dispatcher.Handle(hc)
dispatcher.Initialize()
err = h.Subscribe(f)
if err != ErrNoFeedHubLink {
t.Fatalf("Exepcted a ErrNoFeedHubLink error, got: '%v'\n", err)
}
f.HubLink = ts.URL + "/hublink"
f, _, err = db.UpdateFeed(f)
if err != nil {
t.Fatalf("Got an error during feed db update: '%v'\n", err)
}
err = h.Subscribe(f)
if err != nil {
t.Fatalf("Got an error during subscription: '%v'\n", err)
}
<-done // hublink request
<-done // callback request
if s, err := db.GetHubbubSubscription(f.Id); err != nil {
t.Fatal(err)
} else {
if s.SubscriptionFailure {
t.Fatal("Expected successfull subscription\n")
}
}
<-done // subscription challenge
go func() {
buf := bytes.NewBufferString(strings.Replace(atomXml, "{{ .FeedLink }}", f.Link, -1))
resp, err := http.Post(callbackURL, "text/xml", buf)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}()
<-done // subscription feed update
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)
}
subs, err := db.GetHubbubSubscriptions()
if err != nil {
t.Fatal(err)
}
expectedInt := 1
if len(subs) != expectedInt {
t.Fatalf("Expected %d subscriptions, got %d\n", expectedInt, len(subs))
}
}
type hublink_con struct {
webfw.BasePatternController
callbackURL string
feedLink string
done chan bool
}
func (con hublink_con) Handler(c context.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
panic(err)
}
expectedStr := con.callbackURL
if r.Form.Get("hub.callback") != expectedStr {
panic(fmt.Sprintf("Expected hub.callback '%s', got '%s'\n", r.Form.Get("hub.callback"), expectedStr))
}
expectedStr = con.feedLink
if r.Form.Get("hub.topic") != expectedStr {
panic(fmt.Sprintf("Expected hub.topic '%s', got '%s'\n", r.Form.Get("hub.topic"), expectedStr))
}
expectedStr = "subscribe"
if r.Form.Get("hub.mode") != expectedStr {
panic(fmt.Sprintf("Expected hub.mode '%s', got '%s'\n", r.Form.Get("hub.mode"), expectedStr))
}
w.WriteHeader(http.StatusAccepted)
go func() {
res, err := http.Get(con.callbackURL + "?hub.mode=subscribe&hub.challenge=secret")
defer res.Body.Close()
defer func() { con.done <- true }()
if err != nil {
panic(err)
}
challenge, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
challengeStr := string(challenge[:])
expectedStr = "secret"
if challengeStr != expectedStr {
panic(fmt.Sprintf("Expected challenge '%s', got '%s'\n", challengeStr, expectedStr))
}
}()
})
}
var atomXml = `` +
`<feed xmlns="http://www.w3.org/2005/Atom" updated="2003-12-13T18:30:02Z">` +
`<title>Example Feed</title>` +
`<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>` +
`<link href="{{ .FeedLink }}"></link>` +
`<author><name>John Doe</name><uri></uri><email></email></author>` +
`<entry>` +
`<title>Atom-Powered Robots Run Amok</title>` +
`<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>` +
`<link href="http://example.org/2003/12/13/atom03"></link>` +
`<updated>2003-12-13T18:30:02Z</updated>` +
`<author><name></name><uri></uri><email></email></author>` +
`<summary>Some text.</summary>` +
`</entry>` +
`</feed>`
*/