-
Notifications
You must be signed in to change notification settings - Fork 6
/
case.go
328 lines (293 loc) · 7.04 KB
/
case.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) 2014 Yeung Shu Hung (Koala Yeung)
//
// This file is part of RESTit.
//
// RESTit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RESTit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Use of this source code is governed by the GPL v3 license. A copy
// of the licence can be found in the LICENSE.md file along with RESTit.
// If not, see <http://www.gnu.org/licenses/>.
package restit
import (
"fmt"
"github.com/jmcvetta/napping"
"net/http"
"net/url"
"path"
"runtime"
)
type Case struct {
Request *napping.Request
Name string
Session Session
Expectations []Expectation
Tester *Tester
Result *Result
}
// short hand to initialize and enable defaults
// even if tester is not present
func (c *Case) InitForRun() *Case {
// Tester must be there to provide log
if c.Tester == nil {
c.Tester = new(Tester)
}
// load default logging behaviour
c.Tester.LogDefault()
// c.Request must be a valid napping.Request
// this will be sent through napping.Send
if c.Request == nil {
c.Request = new(napping.Request)
}
// if result is not specified,
// substitute nilResp as response type
if c.Request.Result == nil {
c.Request.Result = NewResponse("items", nilType{})
}
// if error is not specified,
// presume it is the same as result
if c.Request.Error == nil {
c.Request.Error = c.Request.Result
}
// trigger result reset
r, ok := c.Request.Result.(Response)
if !ok {
panic(fmt.Errorf(
"The provided response %T does not implement restit.Response",
r))
}
r.Reset()
// trigger error reset
r, ok = c.Request.Error.(Response)
if !ok {
panic(fmt.Errorf(
"The provided error %T does not implement restit.Response",
r))
}
r.Reset()
return c
}
// To actually run the test case
func (c *Case) Run() (r *Result, err error) {
// setup default tester
c.InitForRun()
// get caller information
_, file, line, _ := runtime.Caller(2)
// send request
res, err := c.Session.Send(c.Request)
if err != nil {
return
}
c.Tester.Trace.Printf("[%s:%d][%s][%s] Raw Response: \"%s\"\n",
path.Base(file),
line,
c.Tester.Name,
c.Name,
res.RawText())
result := Result{
Response: res,
}
c.Result = &result
r = &result
var resp Response
// switch between Request.Result and Request.Error
// depends on the status code
if c.Result.Response.Status() < 300 {
if (*c).Request.Result == nil {
err = fmt.Errorf("[%s:%d][%s][%s] "+
"No request result struct",
path.Base(file),
line,
c.Tester.Name,
c.Name)
}
resp = (*c).Request.Result.(Response)
} else if c.Result.Response.Status() >= 400 {
if (*c).Request.Error == nil {
err = fmt.Errorf("[%s:%d][%s][%s] "+
"No request result struct",
path.Base(file),
line,
c.Tester.Name,
c.Name)
}
resp = (*c).Request.Error.(Response)
} else {
err = fmt.Errorf("[%s:%d][%s][%s] "+
"Result Redirected. Not Result or Error",
path.Base(file),
line,
c.Tester.Name,
c.Name)
}
// test each expectations
for i := 0; i < len(c.Expectations); i++ {
err = c.Expectations[i].Test(resp)
if err != nil {
err = fmt.Errorf("[%s:%d][%s][%s][%s] "+
"Failed: \"%s\"",
path.Base(file),
line,
c.Tester.Name,
c.Name,
c.Expectations[i].Desc,
err.Error())
c.Tester.Err.Println(err.Error())
return
}
}
return
}
// To run the test case and panic on error
func (c *Case) RunOrPanic() (r *Result) {
r, err := c.Run()
if err != nil {
panic(err)
}
return
}
// Add a header parameter
func (c *Case) AddHeader(key, value string) *Case {
if c.Request.Header == nil {
c.Request.Header = &http.Header{}
}
c.Request.Header.Add(key, value)
return c
}
// Set the result to the given interface{}
func (c *Case) WithResponseAs(r Response) *Case {
c.Request.Result = r
return c
}
// Set the error message to the given interface{}
func (c *Case) WithErrorAs(e Response) *Case {
c.Request.Error = e
return c
}
// Set the query parameter
func (c *Case) WithParams(p *url.Values) *Case {
c.Request.Params = p
return c
}
// Append Test to Expectations
// Tests if the result count equal to n
func (c *Case) ExpectResultCount(n int) *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: "Test Result Count",
Test: func(r Response) (err error) {
count := r.Count()
if count != n {
err = fmt.Errorf(
"Result count is %d "+
"(expected %d)",
count, n)
}
return
},
})
return c
}
// Append Test to Expectations
// Tests if the result count not equal to n
func (c *Case) ExpectResultCountNot(n int) *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: "Test Result Count",
Test: func(r Response) (err error) {
count := r.Count()
if count == n {
err = fmt.Errorf(
"Result count is %d "+
"(expected %d)",
count, n)
}
return
},
})
return c
}
// Append Test to Expectations
// Tests if the item is valid
func (c *Case) ExpectResultsValid() *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: "Test Results Valid",
Test: func(r Response) (err error) {
for i := 0; i < r.Count(); i++ {
err = r.NthValid(i)
if err != nil {
err = fmt.Errorf(
"Item %d invalid: %s",
i, err.Error())
return
}
}
return
},
})
return c
}
// Append Test to Expectation
// Tests if the nth item matches the provided one
func (c *Case) ExpectResultNth(n int, b interface{}) *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: fmt.Sprintf("Test #%d Result Valid", n),
Test: func(r Response) (err error) {
a, err := r.GetNth(n)
if err != nil {
return
}
err = r.Match(a, b)
return
},
})
return c
}
// Append Test to Expectations
// Tests if the response status is
func (c *Case) ExpectStatus(ec int) *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: "Test Status Code",
Test: func(r Response) (err error) {
rc := c.Result.Response.Status()
if rc != ec {
err = fmt.Errorf("Status code is %d (expected %d)",
rc, ec)
}
return
},
})
return c
}
// Append Custom Test to Expectation
// Allow user to inject user defined tests
func (c *Case) ExpectResultsToPass(
desc string, test func(Response) error) *Case {
c.Expectations = append(c.Expectations, Expectation{
Desc: desc,
Test: test,
})
return c
}
// Expection to the response in a Case
type Expectation struct {
Desc string
Test func(Response) error
}
// Test Result of a Case
type Result struct {
Response *napping.Response
}
// Wrap the napping.Session in Session
// to make unit testing easier
type Session interface {
Send(*napping.Request) (*napping.Response, error)
}
// nilType is the default type in default response in default case
type nilType struct {
}