-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvalidate.go
More file actions
228 lines (200 loc) · 6.5 KB
/
validate.go
File metadata and controls
228 lines (200 loc) · 6.5 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package httpdoc
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"github.com/golang/protobuf/proto"
"github.com/tenntenn/gpath"
)
var (
defaultUnmarshalFunc = json.Unmarshal
defaultAssertFunc = func(t *testing.T, expected, actual interface{}, desc string) {
if !reflect.DeepEqual(expected, actual) {
tFatalf(t, "%s: got %#v(%T), want %#v(%T)", desc, actual, actual, expected, expected)
}
}
defaultFatalFunc = func(t *testing.T, format string, args ...interface{}) {
t.Fatalf(format, args...)
}
protoUnmarshalFunc = func(data []byte, v interface{}) error {
unmashaler, ok := v.(proto.Unmarshaler)
if !ok {
return fmt.Errorf("failed to type assert to Unmashaler: %T must implement proto.Unmarshaler interface", v)
}
return unmashaler.Unmarshal(data)
}
)
var tFatalf fatalFunc = defaultFatalFunc
type (
assertFunc func(t *testing.T, expected, actual interface{}, desc string)
fatalFunc func(t *testing.T, format string, args ...interface{})
unmarshalFunc func(data []byte, v interface{}) error
)
// Validator takes test cases and checks whether recorded values are equal to the given expected values.
// If not, it fails in the given test context. If ok, it uses the result for documentation.
type Validator struct {
record *record
unmarshalFunc unmarshalFunc
assertFunc assertFunc
requestParams []Data
requestHeaders []Data
requestFields []Data
responseHeaders []Data
responseFields []Data
}
type record struct {
requestParams url.Values
requestHeaders http.Header
requestBody []byte
responseStatusCode int
responseHeaders http.Header
responseBody []byte
}
// TestCase is test case validator uses. Validator inspects and extract request & response value based on
// Target (e.g, when testing request params, target is parameter name. when testing response
// body, target is filed name) and asserts with Expected value.
//
// TestCase can be used like table-driven way.
//
// validator.RequestParams(t, []httpdoc.TestCase{
// NewTestCase("token","12345","Request token"),
// NewTestCase("pretty","true","Pretty print response message"),
// })
//
type TestCase struct {
Target string
Expected interface{}
Description string
AssertFunc assertFunc
}
// NewTestCase returns new TestCase.
func NewTestCase(target string, expected interface{}, description string) TestCase {
return TestCase{Target: target, Expected: expected, Description: description}
}
func newValidator() *Validator {
return &Validator{
unmarshalFunc: defaultUnmarshalFunc,
assertFunc: defaultAssertFunc,
record: &record{},
}
}
// ResponseStatusCode validates response status code is expected or not.
func (v *Validator) ResponseStatusCode(t *testing.T, expected int) {
v.assertFunc(t, expected, v.record.responseStatusCode, "response status code")
}
// RequestParams validated request params are expected or not.
func (v *Validator) RequestParams(t *testing.T, cases []TestCase) {
for _, tc := range cases {
data := Data{
Name: tc.Target,
Value: tc.Expected,
Description: tc.Description,
}
v.requestParams = append(v.requestParams, data)
pickAssertFunc(&tc, v)(t, tc.Expected, v.record.requestParams.Get(tc.Target), tc.Description)
}
}
// RequestHeaders validates request headers are expected or not.
func (v *Validator) RequestHeaders(t *testing.T, cases []TestCase) {
for _, tc := range cases {
data := Data{
Name: tc.Target,
Value: tc.Expected,
Description: tc.Description,
}
v.requestHeaders = append(v.requestHeaders, data)
actual := v.record.requestHeaders.Get(tc.Target)
if actual == "" {
h, ok := v.record.requestHeaders[tc.Target]
if !ok || len(h) == 0 {
tFatalf(t, "request header %q is not found", tc.Target)
return
}
actual = h[0]
}
pickAssertFunc(&tc, v)(t, tc.Expected, actual, tc.Description)
}
}
// ResponseHeaders validates response headers are expected or not.
func (v *Validator) ResponseHeaders(t *testing.T, cases []TestCase) {
for _, tc := range cases {
data := Data{
Name: tc.Target,
Value: tc.Expected,
Description: tc.Description,
}
v.responseHeaders = append(v.responseHeaders, data)
actual := v.record.responseHeaders.Get(tc.Target)
if actual == "" {
h, ok := v.record.responseHeaders[tc.Target]
if !ok || len(h) == 0 {
tFatalf(t, "request header %q is not found", tc.Target)
return
}
actual = h[0]
}
pickAssertFunc(&tc, v)(t, tc.Expected, actual, tc.Description)
}
}
// RequestBody validates request body's fileds are expected or not. The request body
// is unmarshaled to the given struct. To extract a filed to validate, this uses dot-seprated
// expression in TestCase.Target. For example, if you want to access `Email` value in the
// following struct use `Setting.Name` in Target.
//
// type User struct {
// Setting Setting
// }
//
// type Setting struct {
// Email string
// }
//
func (v *Validator) RequestBody(t *testing.T, cases []TestCase, request interface{}) {
// Unmarshal request body into the given struct
if err := v.unmarshalFunc(v.record.requestBody, request); err != nil {
tFatalf(t, "Failed to unmarshal request body: %s", err)
return
}
v.validateFields(t, cases, request, &v.requestFields)
}
// ResponseBody validates response body's fields are expected or not. The response body
// is unmarshaled to the given struct. To extract a filed to validate, this uses dot-seprated
// expression in TestCase.Target. For example, if you want to access `Email` value in the
// following struct use `Setting.Name` in Target.
//
// type User struct {
// Setting Setting
// }
//
// type Setting struct {
// Email string
// }
//
func (v *Validator) ResponseBody(t *testing.T, cases []TestCase, response interface{}) {
// Unmarshal request body into the given struct
if err := v.unmarshalFunc(v.record.responseBody, response); err != nil {
tFatalf(t, "Failed to unmarshal response body: %s", err)
}
v.validateFields(t, cases, response, &v.responseFields)
}
func (vl *Validator) validateFields(t *testing.T, cases []TestCase, v interface{}, fields *[]Data) {
for _, tc := range cases {
data := Data{
Name: tc.Target,
Value: tc.Expected,
Description: tc.Description,
}
*fields = append(*fields, data)
actual, _ := gpath.At(v, tc.Target)
pickAssertFunc(&tc, vl)(t, tc.Expected, actual, tc.Description)
}
}
func pickAssertFunc(tc *TestCase, v *Validator) assertFunc {
if tc.AssertFunc != nil {
return tc.AssertFunc
}
return v.assertFunc
}