-
Notifications
You must be signed in to change notification settings - Fork 6
/
assertions.go
189 lines (153 loc) · 5.54 KB
/
assertions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package httpfake
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)
const assertErrorTemplate = "assertion error: %s"
// Assertor provides an interface for setting assertions for http requests
type Assertor interface {
Assert(r *http.Request) error
Log(t testing.TB)
Error(t testing.TB, err error)
}
// requiredHeaders provides an Assertor for the presence of the provided http header keys
type requiredHeaders struct {
Keys []string
}
// Assert runs the required headers assertion against the provided request
func (h *requiredHeaders) Assert(r *http.Request) error {
var missingHeaders []string
for _, key := range h.Keys {
if value := r.Header.Get(key); len(value) == 0 {
missingHeaders = append(missingHeaders, key)
}
}
if len(missingHeaders) > 0 {
return fmt.Errorf("missing required header(s): %s", strings.Join(missingHeaders, ", "))
}
return nil
}
// Log prints a testing info log for the requiredHeaders Assertor
func (h *requiredHeaders) Log(t testing.TB) {
t.Log("Testing request for required headers")
}
// Error prints a testing error for the requiredHeaders Assertor
func (h *requiredHeaders) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
// requiredHeaderValue provides an Assertor for a header and its expected value
type requiredHeaderValue struct {
Key string
ExpectedValue string
}
// Assert runs the required header value assertion against the provided request
func (h *requiredHeaderValue) Assert(r *http.Request) error {
if value := r.Header.Get(h.Key); value != h.ExpectedValue {
return fmt.Errorf("header %s does not have the expected value; expected %s to equal %s",
h.Key,
value,
h.ExpectedValue)
}
return nil
}
// Log prints a testing info log for the requiredHeaderValue Assertor
func (h *requiredHeaderValue) Log(t testing.TB) {
t.Logf("Testing request for a required header value [%s: %s]", h.Key, h.ExpectedValue)
}
// Error prints a testing error for the requiredHeaderValue Assertor
func (h *requiredHeaderValue) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
// requiredQueries provides an Assertor for the presence of the provided query parameter keys
type requiredQueries struct {
Keys []string
}
// Assert runs the required queries assertion against the provided request
func (q *requiredQueries) Assert(r *http.Request) error {
queryVals := r.URL.Query()
var missingParams []string
for _, key := range q.Keys {
if value := queryVals.Get(key); len(value) == 0 {
missingParams = append(missingParams, key)
}
}
if len(missingParams) > 0 {
return fmt.Errorf("missing required query parameter(s): %s", strings.Join(missingParams, ", "))
}
return nil
}
// Log prints a testing info log for the requiredQueries Assertor
func (q *requiredQueries) Log(t testing.TB) {
t.Log("Testing request for required query parameters")
}
// Error prints a testing error for the requiredQueries Assertor
func (q *requiredQueries) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
// requiredQueryValue provides an Assertor for a query parameter and its expected value
type requiredQueryValue struct {
Key string
ExpectedValue string
}
// Assert runs the required query value assertion against the provided request
func (q *requiredQueryValue) Assert(r *http.Request) error {
if value := r.URL.Query().Get(q.Key); value != q.ExpectedValue {
return fmt.Errorf("query %s does not have the expected value; expected %s to equal %s", q.Key, value, q.ExpectedValue)
}
return nil
}
// Log prints a testing info log for the requiredQueryValue Assertor
func (q *requiredQueryValue) Log(t testing.TB) {
t.Logf("Testing request for a required query parameter value [%s: %s]", q.Key, q.ExpectedValue)
}
// Error prints a testing error for the requiredQueryValue Assertor
func (q *requiredQueryValue) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
// requiredBody provides an Assertor for the expected value of the request body
type requiredBody struct {
ExpectedBody []byte
}
// Assert runs the required body assertion against the provided request
func (b *requiredBody) Assert(r *http.Request) error {
if r.Body == nil {
return fmt.Errorf("error reading the request body; the request body is nil")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("error reading the request body: %s", err.Error())
}
if !bytes.EqualFold(b.ExpectedBody, body) {
return fmt.Errorf("request body does not have the expected value; expected %s to equal %s",
string(body[:]),
string(b.ExpectedBody[:]))
}
return nil
}
// Log prints a testing info log for the requiredBody Assertor
func (b *requiredBody) Log(t testing.TB) {
t.Log("Testing request for a required body value")
}
// Error prints a testing error for the requiredBody Assertor
func (b *requiredBody) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}
// CustomAssertor provides a function signature that implements the Assertor interface. This allows for
// adhoc creation of a custom assertion for use with the AssertCustom assertor.
type CustomAssertor func(r *http.Request) error
// Assert runs the CustomAssertor assertion against the provided request
func (c CustomAssertor) Assert(r *http.Request) error {
return c(r)
}
// Log prints a testing info log for the CustomAssertor
func (c CustomAssertor) Log(t testing.TB) {
t.Log("Testing request with a custom assertor")
}
// Error prints a testing error for the CustomAssertor
func (c CustomAssertor) Error(t testing.TB, err error) {
t.Errorf(assertErrorTemplate, err)
}