/
fake.go
73 lines (61 loc) · 1.84 KB
/
fake.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
package httputil
import (
"bytes"
"io/ioutil"
"net/http"
)
// FakeTransport represents a fake http.Transport that returns prerecorded responses.
type FakeTransport struct {
responses map[string]*responseCollection
}
// NewFakeTransport creates a new FakeTransport instance without any responses.
func NewFakeTransport() *FakeTransport {
return &FakeTransport{
responses: make(map[string]*responseCollection),
}
}
// AddResponse stores a fake HTTP response for the given URL.
func (ft *FakeTransport) AddResponse(url string, status int, body string, headers map[string]string) {
if _, ok := ft.responses[url]; !ok {
ft.responses[url] = &responseCollection{}
}
ft.responses[url].Add(createResponse(status, body, headers))
}
// RoundTrip returns a prerecorded response to the given request, if one exists. Otherwise its response indicates 404 - not found.
func (ft *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if responses, ok := ft.responses[req.URL.String()]; ok {
return responses.Next(), nil
}
return notFound(), nil
}
type responseCollection struct {
all []*http.Response
next int
}
func (rc *responseCollection) Add(resp *http.Response) {
rc.all = append(rc.all, resp)
}
func (rc *responseCollection) Next() *http.Response {
if rc.next >= len(rc.all) {
return notFound()
}
rc.next++
return rc.all[rc.next-1]
}
func createResponse(status int, body string, headers map[string]string) *http.Response {
return &http.Response{
StatusCode: status,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
Header: transformHeaders(headers),
}
}
func transformHeaders(original map[string]string) http.Header {
result := make(map[string][]string)
for k, v := range original {
result[k] = []string{v}
}
return result
}
func notFound() *http.Response {
return createResponse(http.StatusNotFound, "", nil)
}