-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hit.go
299 lines (256 loc) · 6.85 KB
/
hit.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
// Package hit provides an http integration test framework.
//
// It is designed to be flexible as possible, but to keep a simple to use interface for developers.
// Example:
//
// package main
//
// import (
// "net/http"
// . "github.com/Eun/go-hit"
// )
//
// func main() {
// MustDo(
// Description("Post to httpbin.org"),
// Get("https://httpbin.org/post"),
// Expect().Status().Equal(http.StatusMethodNotAllowed),
// Expect().Body().String().Contains("Method Not Allowed"),
// )
// }
//
// Or use the `Test()` function:
// package main_test
// import (
// "testing"
// "net/http"
// . "github.com/Eun/go-hit"
// )
//
// func TestHttpBin(t *testing.T) {
// Test(t,
// Description("Post to httpbin.org"),
// Get("https://httpbin.org/post"),
// Expect().Status().Equal(http.StatusMethodNotAllowed),
// Expect().Body().String().Contains("Method Not Allowed"),
// )
// }
package hit
import (
"context"
"net/http"
"golang.org/x/xerrors"
)
// Callback will be used for Custom() functions.
type Callback func(hit Hit) error
// Hit is the interface that will be passed in for Custom() steps.
type Hit interface {
// Request returns the current request.
Request() *HTTPRequest
// SetRequest sets the request for the current instance.
SetRequest(request *http.Request) error
// Response returns the current Response.
Response() *HTTPResponse
// HTTPClient gets the current http.Client.
HTTPClient() *http.Client
// SetHTTPClient sets the http client for the request.
SetHTTPClient(client *http.Client) error
// BaseURL returns the current base url.
BaseURL() string
// CurrentStep returns the current working step.
CurrentStep() IStep
// Steps returns the current step list.
Steps() []IStep
// AddSteps appends the specified steps to the step list.
AddSteps(steps ...IStep)
// InsertSteps inserts the specified steps right after the current step.
InsertSteps(steps ...IStep)
// RemoveSteps removes the specified steps from the step list.
RemoveSteps(steps ...IStep)
// Do runs the specified steps in in this context.
//
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Custom(func(hit Hit) error {
// return hit.Do(
// Expect().Status().Equal(http.StatusOK),
// )
// }),
// )
Do(steps ...IStep) error
// MustDo runs the specified steps in in this context and panics on failure.
//
// Example:
// MustDo(
// Get("https://example.com"),
// Expect().Custom(func(hit Hit) error {
// hit.MustDo(
// Expect().Status().Equal(http.StatusOK),
// )
// return nil
// }),
// )
MustDo(steps ...IStep)
// Description gets the current description that will be printed in an error case.
Description() string
// SetDescription sets a custom description for this test.
// The description will be printed in an error case
SetDescription(string)
// Context returns the current request context
Context() context.Context
}
type hitImpl struct {
steps []IStep
currentStep IStep
request *HTTPRequest
response *HTTPResponse
client *http.Client
state StepTime
baseURL string
description string
}
func (hit *hitImpl) Request() *HTTPRequest {
return hit.request
}
func (hit *hitImpl) SetRequest(request *http.Request) error {
if request == nil {
return xerrors.New("request cannot be nil")
}
hit.request = newHTTPRequest(hit, request)
return nil
}
func (hit *hitImpl) Response() *HTTPResponse {
return hit.response
}
func (hit *hitImpl) HTTPClient() *http.Client {
return hit.client
}
func (hit *hitImpl) SetHTTPClient(client *http.Client) error {
if client == nil {
return xerrors.New("client cannot be nil")
}
hit.client = client
return nil
}
func (hit *hitImpl) BaseURL() string {
return hit.baseURL
}
func (hit *hitImpl) collectSteps(state StepTime) []IStep {
var collectedSteps []IStep
for i := 0; i < len(hit.steps); i++ {
if hit.steps[i] == nil {
continue
}
w := hit.steps[i].when()
if w == state {
collectedSteps = append(collectedSteps, hit.steps[i])
}
}
return collectedSteps
}
func (hit *hitImpl) runSteps(state StepTime) *Error {
// find all steps we want to run
stepsToRun := hit.collectSteps(state)
size := len(stepsToRun)
// be optimistic and hope nobody changes the size
// if not the slice will resize
executedSteps := make([]IStep, 0, size)
nextStep:
for i := 0; i < size; i++ {
for _, step := range executedSteps {
// step already executed
if step == stepsToRun[i] {
continue nextStep
}
}
hit.currentStep = stepsToRun[i]
if err := execStep(hit, stepsToRun[i]); err != nil {
return err
}
executedSteps = append(executedSteps, stepsToRun[i])
// maybe the steps got modified in some way
// lets quickly get the steps we need to execute and compare them with the ones we currently execute
newSteps := hit.collectSteps(state)
if !stepsAreEqual(stepsToRun, newSteps) {
// yep something changed in our scope
// start over again
i = -1
size = len(newSteps)
stepsToRun = newSteps
continue nextStep
}
}
return nil
}
func stepsAreEqual(a, b []IStep) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func (hit *hitImpl) CurrentStep() IStep {
return hit.currentStep
}
func (hit *hitImpl) Steps() []IStep {
return hit.steps
}
func (hit *hitImpl) AddSteps(steps ...IStep) {
hit.steps = append(hit.steps, steps...)
}
func (hit *hitImpl) InsertSteps(steps ...IStep) {
for i, step := range hit.steps {
if step != hit.currentStep {
continue
}
hit.steps = append(hit.steps[:i+1], append(steps, hit.steps[i+1:]...)...)
return
}
}
func (hit *hitImpl) RemoveSteps(steps ...IStep) {
for j := len(steps) - 1; j >= 0; j-- {
for i := len(hit.steps) - 1; i >= 0; i-- {
if hit.steps[i] == steps[j] {
// remove the step from steps and hit.steps
steps = append(steps[:j], steps[j+1:]...)
hit.steps = append(hit.steps[:i], hit.steps[i+1:]...)
hit.RemoveSteps(steps...)
return
}
}
}
}
func (hit *hitImpl) Do(steps ...IStep) error {
for _, step := range steps {
if step.when() != hit.state && step.when() != cleanStep {
return xerrors.Errorf(
"unable to execute %s during %s, can only be run during %s",
step.callPath().CallString(true),
hit.state.String(), step.when().String(),
)
}
if err := execStep(hit, step); err != nil {
return err
}
}
return nil
}
func (hit *hitImpl) MustDo(steps ...IStep) {
if err := hit.Do(steps...); err != nil {
panic(err)
}
}
func (hit *hitImpl) Description() string {
return hit.description
}
func (hit *hitImpl) SetDescription(description string) {
hit.description = description
}
func (hit *hitImpl) Context() context.Context {
return hit.request.Context()
}