-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaper_test.go
53 lines (45 loc) · 1.18 KB
/
reaper_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// MockClient is a mock HTTP client for testing
type MockClient struct {
Response []byte
Code int
HeaderMap map[string]string
Method string
}
// NewMockClient creates a new mock client that will return the response b and the code c
func NewMockClient(b []byte, c int) MockClient {
return MockClient{
Response: b,
Code: c,
}
}
// Do implements the HTTPClient interface for MockClient
func (m MockClient) Do(req *http.Request) (*http.Response, error) {
var res http.Response
if len(m.HeaderMap) > 0 {
for k, value := range m.HeaderMap {
fmt.Printf("Checking key: %s for values %+v against headers %+v", k, value, req.Header)
if value != req.Header.Get(k) {
res.Body = ioutil.NopCloser(bytes.NewReader([]byte("Bad header values")))
res.StatusCode = http.StatusBadRequest
return &res, nil
}
}
}
if m.Method != "" {
if req.Method != m.Method {
res.Body = ioutil.NopCloser(bytes.NewReader([]byte("Bad method")))
res.StatusCode = http.StatusBadRequest
return &res, nil
}
}
res.Body = ioutil.NopCloser(bytes.NewReader(m.Response))
res.StatusCode = m.Code
return &res, nil
}