-
Notifications
You must be signed in to change notification settings - Fork 23
/
fake_http_client.go
75 lines (60 loc) · 1.42 KB
/
fake_http_client.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
package fakes
import (
"net/http"
boshhttp "github.com/cloudfoundry/bosh-utils/http"
)
type doInput struct {
req *http.Request
}
type doOutput struct {
resp *http.Response
err error
}
type FakeClient struct {
StatusCode int
CallCount int
Error error
returnNilResponse bool
RequestBodies []string
Requests []*http.Request
responseMessage string
doBehavior []doOutput
}
func NewFakeClient() (fakeClient *FakeClient) {
fakeClient = &FakeClient{}
return
}
func (c *FakeClient) SetMessage(message string) {
c.responseMessage = message
}
func (c *FakeClient) SetNilResponse() {
c.returnNilResponse = true
}
func (c *FakeClient) Do(req *http.Request) (*http.Response, error) {
c.CallCount++
if req.Body != nil {
content, err := boshhttp.ReadAndClose(req.Body)
if err != nil {
return nil, err
}
c.RequestBodies = append(c.RequestBodies, string(content))
}
c.Requests = append(c.Requests, req)
if len(c.doBehavior) > 0 {
output := c.doBehavior[0]
c.doBehavior = c.doBehavior[1:]
return output.resp, output.err
}
var resp *http.Response
if !c.returnNilResponse {
resp = &http.Response{
Body: boshhttp.NewStringReadCloser(c.responseMessage),
StatusCode: c.StatusCode,
}
}
err := c.Error
return resp, err
}
func (c *FakeClient) AddDoBehavior(resp *http.Response, err error) {
c.doBehavior = append(c.doBehavior, doOutput{resp: resp, err: err})
}