forked from richyen/walker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmocks.go
More file actions
310 lines (262 loc) · 7.93 KB
/
mocks.go
File metadata and controls
310 lines (262 loc) · 7.93 KB
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
package walker
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"github.com/stretchr/testify/mock"
)
// MockDatastore implements walker's Datastore interface for testing.
type MockDatastore struct {
mock.Mock
}
func (ds *MockDatastore) StoreParsedURL(u *URL, fr *FetchResults) {
ds.Mock.Called(u, fr)
}
func (ds *MockDatastore) StoreURLFetchResults(fr *FetchResults) {
ds.Mock.Called(fr)
}
// ClaimNewHost implements walker.Datastore interface
func (ds *MockDatastore) ClaimNewHost() string {
args := ds.Mock.Called()
return args.String(0)
}
// UnclaimHost implements walker.Datastore interface
func (ds *MockDatastore) UnclaimHost(host string) {
ds.Mock.Called(host)
}
// UnclaimAll implements method on cassandra.Datastore
func (ds *MockDatastore) UnclaimAll() error {
args := ds.Mock.Called()
return args.Error(0)
}
func (ds *MockDatastore) LinksForHost(domain string) <-chan *URL {
args := ds.Mock.Called(domain)
urls := args.Get(0).([]*URL)
ch := make(chan *URL, len(urls))
for _, u := range urls {
ch <- u
}
close(ch)
return ch
}
// KeepAlive implements walker.Datastore interface
func (ds *MockDatastore) KeepAlive() error {
ds.Mock.Called()
return nil
}
func (ds *MockDatastore) Close() {
ds.Mock.Called()
}
// MockHandler implements the walker.Handler interface
type MockHandler struct {
mock.Mock
}
func (h *MockHandler) HandleResponse(fr *FetchResults) {
// Copy response body so that the fetcher code can reuse readBuffer
var buffer bytes.Buffer
_, err := buffer.ReadFrom(fr.Response.Body)
if err != nil {
panic(err)
}
fr.Response.Body = ioutil.NopCloser(bytes.NewReader(buffer.Bytes()))
h.Mock.Called(fr)
}
// MockDispatcher implements the walker.Dispatcher interface
type MockDispatcher struct {
mock.Mock
}
// StartDispatcher implements the walker.Dispatcher interface
func (d *MockDispatcher) StartDispatcher() error {
args := d.Mock.Called()
return args.Error(0)
}
// StopDispatcher implements the walker.Dispatcher interface
func (d *MockDispatcher) StopDispatcher() error {
args := d.Mock.Called()
return args.Error(0)
}
// MockResponse is the source object used to build fake responses in
// MockHTTPHandler.
type MockResponse struct {
// Status defaults to 200
Status int
// Status defaults to "GET"
Method string
// Body defaults to nil (no response body)
Body string
// Headers of response
Headers http.Header
//ContentType defaults to "text/html"
ContentType string
// How long is the content
ContentLength int
}
// MockHTTPHandler implements http.Handler to serve mock requests.
//
// It is not a mere mock.Mock object because using `.Return()` to return
// *http.Response objects is hard to do, and this provides conveniences in our
// tests.
//
// It should be instantiated with `NewMockRemoteServer()`
type MockHTTPHandler struct {
// returns keeps track of mock calls and what to respond with. The top
// level map is by method, i.e. returns["GET"]["http://test.com/"] => an
// expected response
returns map[string]map[string]*MockResponse
// headers stores the headers sent to the Mock server indexed (as for
// returns) by the pair (method, url)
headers map[string]map[string][]http.Header
}
// NewMockHTTPHandler creates a new MockHTTPHandler
func NewMockHTTPHandler() *MockHTTPHandler {
s := new(MockHTTPHandler)
s.returns = map[string]map[string]*MockResponse{
"DELETE": map[string]*MockResponse{},
"GET": map[string]*MockResponse{},
"HEAD": map[string]*MockResponse{},
"OPTIONS": map[string]*MockResponse{},
"POST": map[string]*MockResponse{},
"PUT": map[string]*MockResponse{},
"TRACE": map[string]*MockResponse{},
}
s.headers = map[string]map[string][]http.Header{
"DELETE": map[string][]http.Header{},
"GET": map[string][]http.Header{},
"HEAD": map[string][]http.Header{},
"OPTIONS": map[string][]http.Header{},
"POST": map[string][]http.Header{},
"PUT": map[string][]http.Header{},
"TRACE": map[string][]http.Header{},
}
return s
}
// SetResponse sets a mock response for the server to return when it sees an
// incoming request matching the given link. The link should have a scheme and
// host (ex. "http://test.com/stuff"). Empty fields on MockResponse will be
// filled in with default values (see MockResponse)
func (s *MockHTTPHandler) SetResponse(link string, r *MockResponse) {
if r.Method == "" {
r.Method = "GET"
}
m := s.returns[r.Method]
m[link] = r
}
// storeHeader stores header information
func (s *MockHTTPHandler) storeHeader(method string, link string, inHeaders http.Header) error {
// first copy the input headers
headers := http.Header{}
for key, list := range inHeaders {
var nlist []string
nlist = append(nlist, list...)
headers[key] = nlist
}
// now put them in the right place
m, mok := s.headers[method]
if !mok {
return fmt.Errorf("Failed to find method %v in headers", method)
}
m[link] = append(m[link], headers)
return nil
}
// ServeHTTP implements http.Handler interface
func (s *MockHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.TLS == nil {
r.URL.Scheme = "http"
} else {
r.URL.Scheme = "https"
}
r.URL.Host = r.Host
m, ok := s.returns[r.Method]
if !ok {
panic(fmt.Sprintf("Got an http method we didn't expect: %v", r.Method))
}
link := r.URL.String()
s.storeHeader(r.Method, link, r.Header)
res, ok := m[link]
if !ok {
// No particular response requested, just return 200 OK return
return
}
if res.Status == 0 {
res.Status = 200
}
if res.ContentType == "" {
res.ContentType = "text/html"
}
w.Header().Set("Content-Type", res.ContentType)
if res.ContentLength != 0 {
w.Header().Set("Content-Length", fmt.Sprintf("%d", res.ContentLength))
}
w.WriteHeader(res.Status)
_, err := w.Write([]byte(res.Body))
if err != nil {
panic(fmt.Sprintf("Failed to write response for page %v, err: %v", r.URL, err))
}
}
// MockRemoteServer wraps MockHTTPHandler to start a fake server for the user.
// Use `NewMockRemoteServer()`
type MockRemoteServer struct {
*MockHTTPHandler
listener net.Listener
}
// NewMockRemoteServer starts a server listening on port 80. It wraps
// MockHTTPHandler so mock return values can be set. Stop should be called at
// the end of the test to stop the server.
func NewMockRemoteServer() (*MockRemoteServer, error) {
rs := new(MockRemoteServer)
rs.MockHTTPHandler = NewMockHTTPHandler()
var err error
rs.listener, err = net.Listen("tcp", ":80")
if err != nil {
return nil, fmt.Errorf("Failed to listen on port 80, you probably do "+
"not have sufficient privileges to run this test (source error: %v", err)
}
go http.Serve(rs.listener, rs)
return rs, nil
}
// Headers allows user to inspect the headers included in the request object
// sent to MockRemoteServer. The triple (method, url, depth) selects which
// header to return. Here:
// (a) method is the http method (GET, POST, etc.)
// (b) url is the full url of the page that received the request.
// (c) depth is an integer specifying which (of possibly many) headers for the
// given (method, url) pair to return. Use depth=-1 to get the latest
// header.
func (rs *MockRemoteServer) Headers(method string, url string, depth int) (http.Header, error) {
m, mok := rs.MockHTTPHandler.headers[method]
if !mok {
return nil, fmt.Errorf("Failed to find method %q", method)
}
head, headok := m[url]
if !headok {
return nil, fmt.Errorf("Failed to find link %q", url)
}
if depth >= len(head) {
return nil, fmt.Errorf("Depth (%d) was >= length of headers %d", depth, len(head))
}
if depth < 0 {
return head[len(head)-1], nil
}
return head[depth], nil
}
// Requested returns true if the url was requested, and false otherwise.
func (rs *MockRemoteServer) Requested(method string, url string) bool {
m, mok := rs.MockHTTPHandler.headers[method]
if !mok {
return false
}
head, headok := m[url]
if !headok {
return false
}
if len(head) == 0 {
return false
}
return true
}
// Stop will stop the faux-server.
func (rs *MockRemoteServer) Stop() {
rs.listener.Close()
}