forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_framework.go
executable file
·320 lines (293 loc) · 8.18 KB
/
template_framework.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
// +build -ignore
// You can use this file as an template to build your own framework. Just change / add the functions you need.
// See also examples/extensibility
package main
import (
"io"
"net/http"
"github.com/Eun/go-hit"
)
// Send sends the specified data as the body payload
//
// Examples:
// MustDo(
// Post("https://example.com"),
// Send("Hello World"),
// )
//
// MustDo(
// Post("https://example.com"),
// Send().Body("Hello World")
// )
func Send(data ...interface{}) hit.ISend {
return hit.Send(data...)
}
// Expect expects the body to be equal the specified value, omit the parameter to get more options
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Expect().Body().Contains("Hello World")
// )
//
// MustDo(
// Get("https://example.com"),
// Expect("Hello World"),
// )
func Expect(data ...interface{}) hit.IExpect {
return hit.Expect(data...)
}
// Debug prints the current Request and Response to hit.Stdout(), you can filter the output based on expressions
//
// Examples:
// MustDo(
// Get("https://example.com"),
// Debug(),
// )
//
// MustDo(
// Get("https://example.com"),
// Debug("Response.Headers"),
// )
func Debug(expression ...string) hit.IStep {
return hit.Debug(expression...)
}
// HTTPClient sets the client for the request
//
// Example:
// var client http.Client
// MustDo(
// Get("https://example.com"),
// HTTPClient(&client),
// )
func HTTPClient(client *http.Client) hit.IStep {
return hit.HTTPClient(client)
}
// Stdout sets the output to the specified writer
//
// Example:
// MustDo(
// Get("https://example.com"),
// Stdout(os.Stderr),
// Debug(),
// )
func Stdout(w io.Writer) hit.IStep {
return hit.Stdout(w)
}
// BaseURL sets the base url for each Connect, Delete, Get, Head, Post, Options, Put, Trace or Method
//
// Examples:
// MustDo(
// BaseURL("https://example.com")
// )
//
// MustDo(
// BaseURL("https://%s/%s", "example.com", "index.html")
// )
func BaseURL(url string, a ...interface{}) hit.IStep {
return hit.BaseURL(url, a...)
}
// Request creates a new Hit instance with an existing http request
//
// Example:
// request, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
// MustDo(
// Request(request),
// )
func Request(request *http.Request) hit.IStep {
return hit.Request(request)
}
// Method creates a new Hit instance with the specified method and url
//
// Examples:
// MustDo(
// Method(http.MethodGet, "https://example.com"),
// )
//
// MustDo(
// Method(http.MethodGet, "https://%s/%s", "example.com", "index.html"),
// )
func Method(method, url string, a ...interface{}) hit.IStep {
return hit.Method(method, url, a...)
}
// Connect creates a new Hit instance with CONNECT as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Connect("https://example.com"),
// )
//
// MustDo(
// Connect("https://%s/%s", "example.com", "index.html"),
// )
func Connect(url string, a ...interface{}) hit.IStep {
return hit.Connect(url, a...)
}
// Delete creates a new Hit instance with DELETE as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Delete("https://example.com"),
// )
//
// MustDo(
// Delete("https://%s/%s", "example.com", "index.html"),
// )
//
func Delete(url string, a ...interface{}) hit.IStep {
return hit.Delete(url, a...)
}
// Get creates a new Hit instance with GET as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Get("https://example.com"),
// )
//
// MustDo(
// Get("https://%s/%s", "example.com", "index.html"),
// )
//
func Get(url string, a ...interface{}) hit.IStep {
return hit.Get(url, a...)
}
// Head creates a new Hit instance with HEAD as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Head("https://example.com"),
// )
//
// MustDo(
// Head("https://%s/%s", "example.com", "index.html"),
// )
//
func Head(url string, a ...interface{}) hit.IStep {
return hit.Head(url, a...)
}
// Post creates a new Hit instance with POST as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Post("https://example.com"),
// )
//
// MustDo(
// Post("https://%s/%s", "example.com", "index.html"),
// )
//
func Post(url string, a ...interface{}) hit.IStep {
return hit.Post(url, a...)
}
// Options creates a new Hit instance with OPTIONS as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Options("https://example.com"),
// )
//
// MustDo(
// Options("https://%s/%s", "example.com", "index.html"),
// )
//
func Options(url string, a ...interface{}) hit.IStep {
return hit.Options(url, a...)
}
// Put creates a new Hit instance with PUT as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Put("https://example.com"),
// )
//
// MustDo(
// Put("https://%s/%s", "example.com", "index.html"),
// )
//
func Put(url string, a ...interface{}) hit.IStep {
return hit.Put(url, a...)
}
// Trace creates a new Hit instance with TRACE as the http makeMethodStep, use the optional arguments to format the url
//
// Examples:
// MustDo(
// Trace("https://example.com"),
// )
//
// MustDo(
// Trace("https://%s/%s", "example.com", "index.html"),
// )
//
func Trace(url string, a ...interface{}) hit.IStep {
return hit.Trace(url, a...)
}
// Test runs the specified steps and calls t.Error() if any error occurs during execution
func Test(t hit.TestingT, steps ...hit.IStep) {
hit.Test(t, steps...)
}
// Do runs the specified steps and returns error if something was wrong
func Do(steps ...hit.IStep) error {
return hit.Do(steps...)
}
// MustDo runs the specified steps and panics with the error if something was wrong
func MustDo(steps ...hit.IStep) {
hit.MustDo(steps...)
}
// CombineSteps combines multiple steps to one
//
// Example:
// MustDo(
// Get("https://example.com"),
// CombineSteps(
// Expect().Status(http.StatusOK),
// Expect().Body("Hello World"),
// ),
// )
func CombineSteps(steps ...hit.IStep) hit.IStep {
return hit.CombineSteps(steps...)
}
// Description sets a custom description for this test.
// The description will be printed in an error case
//
// Example:
// MustDo(
// Description("Check if example.com is available"),
// Get("https://example.com"),
// )
func Description(description string) hit.IStep {
return hit.Description(description)
}
// Clear can be used to remove previous steps.
//
// Usage:
// Clear().Send("Hello World") // will remove all Send("Hello World") steps
// Clear().Send().Body("Hello World") // will remove all Send().Body("Hello World") steps
// Clear().Expect().Body() // will remove all Expect().Body() steps and all chained steps to Body() e.g. Expect().Body().Equal("Hello World")
// Clear().Expect().Body("Hello World") // will remove all Expect().Body("Hello World") steps
//
// Example:
// MustDo(
// Post("https://example.com"),
// Expect().Status(http.StatusOK),
// Expect().Body().Contains("Welcome to example.com"),
// Clear().Expect(),
// Expect().Status(http.NotFound),
// Expect().Body().Contains("Not found!"),
// )
func Clear() hit.IClear {
return hit.Clear()
}
// Custom can be used to run custom logic during various steps.
//
// Example:
// MustDo(
// Post("https://example.com"),
// Custom(ExpectStep, func(hit Hit) {
// if hit.Response().Body().String() != "Hello Earth" {
// panic("Expected Hello Earth")
// }
// }),
// )
func Custom(when hit.StepTime, exec hit.Callback) hit.IStep {
return hit.Custom(when, exec)
}