/
mockflake.go
62 lines (51 loc) · 1.32 KB
/
mockflake.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
package snowflake
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
)
type testRoundTripper struct {
resources Resources
recorder *httptest.ResponseRecorder
}
func MockRun(r Resources, options *GlobalOptions) http.Client {
roundTripper := testRoundTripper{
resources: r,
recorder: httptest.NewRecorder(),
}
testClient := http.Client{
Transport: roundTripper,
}
return testClient
}
func (trt testRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
w := trt.recorder
for _, resource := range trt.resources {
fmt.Printf("resource path: %q\n", resource.Path)
fmt.Printf("request path: %q\n", request.URL.Path)
if resource.Path == request.URL.Path {
if request.Method == "GET" {
resource.Get.Handle(0, w, request)
return respFromRecorder(w), nil
}
if request.Method == "POST" {
resource.Post.Handle(0, w, request)
return respFromRecorder(w), nil
}
}
}
return nil, errors.New("path not found")
}
// respFromRecorder builds an http response from a httptest recorder
func respFromRecorder(w *httptest.ResponseRecorder) *http.Response {
resp := http.Response{}
resp.StatusCode = w.Code
resp.Header = w.Header()
// TODO: fill in the rest of response
b := w.Body.Bytes()
resp.Body = ioutil.NopCloser(bytes.NewReader(b))
return &resp
}