-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_expectation.go
101 lines (86 loc) · 2.99 KB
/
response_expectation.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
package httpmockserver
import (
"encoding/json"
)
type MockResponse struct {
Code int
Headers map[string]string
Body []byte
}
// ResponseExpectation is a builder for a MockResponse
// you may set Headers, Body, and Code on the response
// this response is returned to the caller when the corresponding request is matched
type ResponseExpectation interface {
ContentType(contentType string) ResponseExpectation
Header(key, value string) ResponseExpectation
Headers(headers map[string]string) ResponseExpectation
StringBody(body string) ResponseExpectation
JsonBody(object interface{}) ResponseExpectation
Body(data []byte) ResponseExpectation
}
type responseExpectation struct {
resp *MockResponse
t T
}
// ContentType sets the content type header on the response
func (exp *responseExpectation) ContentType(contentType string) ResponseExpectation {
exp.resp.Headers["Content-Type"] = contentType
return exp
}
// Header sets a header on the response
func (exp *responseExpectation) Header(key, value string) ResponseExpectation {
exp.resp.Headers[key] = value
return exp
}
// Headers sets multiple headers on the response
func (exp *responseExpectation) Headers(headers map[string]string) ResponseExpectation {
for key, value := range headers {
exp.resp.Headers[key] = value
}
return exp
}
// StringBody sets the body of the response to the given string (e.g. "Hello World" or `{"foo":"bar"}`)
func (exp *responseExpectation) StringBody(body string) ResponseExpectation {
return exp.Body([]byte(body))
}
// JsonBody sets the body of the response to the given object (e.g. `{"foo":"bar"}` or map[string]string{"foo":"bar"})
// you may provide a go object or a valid json string
// automatically sets the content type to application/json if ContentType is not set yet
func (exp *responseExpectation) JsonBody(object interface{}) ResponseExpectation {
exp.t.Helper()
// check if ContentType is set, if not set it to application/json
if _, ok := exp.resp.Headers["Content-Type"]; !ok {
exp.resp.Headers["Content-Type"] = "application/json"
}
if object == nil {
return exp.Body(nil)
}
switch t := object.(type) {
case []byte:
var object interface{}
err := json.Unmarshal(t, &object)
if err != nil {
exp.t.Fatalf("response expectation failed: could not parse to json: %+v", object)
}
resData, _ := json.Marshal(object)
return exp.Body(resData)
case string:
var object interface{}
err := json.Unmarshal([]byte(t), &object)
if err != nil {
exp.t.Fatalf("response expectation failed: could not parse to json: %+v", object)
}
resData, _ := json.Marshal(object)
return exp.Body(resData)
}
jsonBody, err := json.Marshal(object)
if err != nil {
exp.t.Fatalf("response expectation failed: could not parse to json: %+v", object)
}
return exp.Body(jsonBody)
}
// Body sets the body of the response to the given byte array (e.g. []byte("Hello World") or []byte(`{"foo":"bar"}`))
func (exp *responseExpectation) Body(data []byte) ResponseExpectation {
exp.resp.Body = data
return exp
}