-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockhttp.go
102 lines (83 loc) · 2.4 KB
/
mockhttp.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package mockhttp
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/nicklarsennz/mockhttp/responders"
"github.com/pkg/errors"
)
func NewClient(definitionsFilePath string) (*http.Client, error) {
config, err := responders.ParseConfig(definitionsFilePath)
if err != nil {
return nil, errors.Wrap(err, "parser error")
}
client := makeClient(config)
return client, nil
}
func makeClient(config *responders.ResponderConfig) *http.Client {
return &http.Client{
Transport: &mockTransport{
ResponderConfig: config,
},
}
}
type mockTransport struct {
*responders.ResponderConfig
}
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return MatchResponse(req, t.ResponderConfig), nil
}
// Todo: move this to the responder package
// I think we need more clever matching, best match, not just first match
func MatchResponse(req *http.Request, config *responders.ResponderConfig) *http.Response {
trimmedRequestBody := strings.Trim(bodyString(req), " \r\n")
var fullPath = req.URL.Path
if req.URL.RawQuery != "" {
fullPath = fullPath + "?" + req.URL.RawQuery
}
fmt.Println(req.Method + " " + fullPath)
// Loop through responders which match the method and path
for _, responder := range config.Responders {
// Skip if no match
if responder.When.Http.Method != req.Method {
continue
}
if responder.When.Http.Path != fullPath {
continue
}
if !responder.When.Headers.AppearIn(req.Header) {
continue
}
trimmedResponderBody := strings.Trim(responder.When.Body, " \r\n")
if trimmedResponderBody != trimmedRequestBody {
continue
}
var headers = make(http.Header)
for k, v := range responder.Then.Headers {
headers.Add(k, v)
}
return &http.Response{
Status: fmt.Sprintf("%d %s", responder.Then.Http.Status, responder.Then.Http.Message),
StatusCode: responder.Then.Http.Status,
Header: headers,
Body: ioutil.NopCloser(bytes.NewBufferString(responder.Then.Body)),
}
}
// Otherwise 404
return &http.Response{
Status: "404 Not Found",
Header: http.Header{"X-NOOP": []string{"mockhttp failed to match"}},
Body: ioutil.NopCloser(bytes.NewBufferString("Error: mockhttp could not find the responder for the given conditions")),
}
}
func bodyString(r *http.Request) string {
if r.Body == nil {
return ""
}
if body, err := ioutil.ReadAll(r.Body); err == nil {
return string(body)
}
return ""
}