/
http_client.go
64 lines (51 loc) · 1.11 KB
/
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
package httptest
import (
"io/ioutil"
"net/http"
"strings"
"sync"
)
type FakeClient struct {
requests []http.Request
responseCode int
responseBody string
Err error
mu sync.Mutex
}
func (fc *FakeClient) Do(req *http.Request) (*http.Response, error) {
fc.mu.Lock()
defer fc.mu.Unlock()
fc.requests = append(fc.requests, *req)
r := http.Response{
StatusCode: fc.responseCode,
Body: ioutil.NopCloser(strings.NewReader(fc.responseBody)),
}
return &r, fc.Err
}
func (fc *FakeClient) SetResponse(s string) {
fc.responseCode = http.StatusOK
fc.responseBody = s
}
func (fc *FakeClient) ClearRequests() {
fc.mu.Lock()
defer fc.mu.Unlock()
fc.requests = nil
}
func (fc *FakeClient) Requests() []http.Request {
fc.mu.Lock()
defer fc.mu.Unlock()
ret := append([]http.Request{}, fc.requests...)
return ret
}
func NewFakeClient() *FakeClient {
return &FakeClient{
responseCode: http.StatusInternalServerError,
responseBody: "FakeClient response uninitialized",
}
}
func NewFakeClientEmptyJSON() *FakeClient {
return &FakeClient{
responseCode: http.StatusOK,
responseBody: "{}",
}
}