-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathquicktest_example_v2_test.go
More file actions
351 lines (285 loc) · 8.36 KB
/
Copy pathquicktest_example_v2_test.go
File metadata and controls
351 lines (285 loc) · 8.36 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Package quick provides a fast and flexible web framework with built-in
// HTTP testing utilities. This file contains example functions demonstrating
// how to use Quick's testing utilities.
package quick
import (
"fmt"
"net/http"
)
// ExampleQuick_Qtest demonstrates how to use Qtest to test a simple GET route.
//
// The "/hello" route returns a response with status 200 and body "Hello, Quick!".
func ExampleQuick_Qtest() {
// Creating a Quick instance
q := New()
// Defining a simple GET route
q.Get("/hello", func(c *Ctx) error {
return c.Status(200).String("Hello, Quick!")
})
// Defining the request parameters
opts := QuickTestOptions{
Method: "GET",
URI: "/hello",
}
// Performing the HTTP test using the Quick instance
res, err := q.Qtest(opts)
// Handling errors
if err != nil {
fmt.Println("Error:", err)
return
}
// Printing response details
fmt.Println("Status Code:", res.StatusCode())
fmt.Println("Body:", res.BodyStr())
// Output:
// Status Code: 200
// Body: Hello, Quick!
}
// ExampleQTestPlus_AssertStatus demonstrates how to verify the response status code.
//
// The "/notfound" route returns a 404 status, which is validated in the assertion.
func ExampleQTestPlus_AssertStatus() {
// Creating a Quick instance
q := New()
// Defining a route that returns 404
q.Get("/notfound", func(c *Ctx) error {
return c.Status(404).String("Not Found")
})
// Performing the HTTP test
opts := QuickTestOptions{
Method: "GET",
URI: "/notfound",
}
res, err := q.Qtest(opts)
if err != nil {
fmt.Println("Error:", err)
return
}
// Validating the response status
err = res.AssertStatus(404)
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Status code is correct")
}
// Output: Status code is correct
}
// ExampleQTestPlus_AssertHeader demonstrates how to validate an HTTP response header.
//
// The "/header" route sets an "X-Custom-Header", which is then validated.
func ExampleQTestPlus_AssertHeader() {
// Creating a Quick instance
q := New()
// Defining a route with a custom header
q.Get("/header", func(c *Ctx) error {
c.Set("X-Custom-Header", "QuickFramework")
return c.Status(200).String("OK")
})
// Performing the HTTP test
opts := QuickTestOptions{
Method: "GET",
URI: "/header",
}
res, err := q.Qtest(opts)
if err != nil {
fmt.Println("Error:", err)
return
}
// Validating the header
err = res.AssertHeader("X-Custom-Header", "QuickFramework")
if err != nil {
fmt.Println("Header assertion failed:", err)
} else {
fmt.Println("Header is correct")
}
// Output: Header is correct
}
// ExampleQTestPlus_AssertBodyContains demonstrates how to validate the response body.
//
// The "/json" route returns a JSON response, and the test verifies the presence of the "message" key.
func ExampleQTestPlus_AssertBodyContains() {
// Creating a Quick instance
q := New()
// Defining a route that returns JSON
q.Get("/json", func(c *Ctx) error {
data := map[string]string{"message": "Hello, Quick!"}
return c.JSON(data)
})
// Performing the HTTP test
opts := QuickTestOptions{
Method: "GET",
URI: "/json",
}
res, err := q.Qtest(opts)
if err != nil {
fmt.Println("Error:", err)
return
}
// Validating the response body
err = res.AssertBodyContains(`"message":"Hello, Quick!"`)
if err != nil {
fmt.Println("Body assertion failed:", err)
} else {
fmt.Println("Body contains expected content")
}
// Output: Body contains expected content
}
// ExampleQTestPlus_Body demonstrates how to retrieve the response body as a byte slice.
//
// The simulated response object contains "Hello, Quick!" as its body content.
func ExampleQTestPlus_Body() {
// Simulating a response object with a predefined body
res := &QTestPlus{
body: []byte("Hello, Quick!"),
}
// Retrieving and printing the body content
fmt.Println(string(res.Body()))
// Output: Hello, Quick!
}
// ExampleQTestPlus_BodyStr demonstrates how to retrieve the response body as a string.
//
// The simulated response object contains "Hello, Quick!" as its body content.
func ExampleQTestPlus_BodyStr() {
// Simulating a response object with a predefined body
res := &QTestPlus{
bodyStr: "Hello, Quick!",
}
// Retrieving and printing the body content as a string
fmt.Println(res.BodyStr())
// Output: Hello, Quick!
}
// ExampleQTestPlus_StatusCode demonstrates how to retrieve the response status code.
//
// The simulated response object contains a status code of 200.
func ExampleQTestPlus_StatusCode() {
res := &QTestPlus{
statusCode: 200,
}
// Retrieving and printing the response status code
fmt.Println("Status Code:", res.StatusCode())
// Output: Status Code: 200
}
// ExampleQTestPlus_Response demonstrates how to retrieve the complete HTTP response object.
//
// The simulated HTTP response object contains a status of "200 OK".
func ExampleQTestPlus_Response() {
// Simulating an HTTP response object
httpResponse := &http.Response{
Status: "200 OK",
StatusCode: 200,
}
// Simulating a response object containing the HTTP response
res := &QTestPlus{
response: httpResponse,
}
// Retrieving and printing the response status
fmt.Println("Response Status:", res.Response().Status)
// Output: Response Status: 200 OK
}
// ExampleQTestPlus_AssertNoHeader demonstrates how to check that a header is not present in the response.
//
// The simulated HTTP response does not include the "X-Powered-By" header.
func ExampleQTestPlus_AssertNoHeader() {
q := New()
q.Get("/no-header", func(c *Ctx) error {
return c.String("No custom header here.")
})
res, err := q.Qtest(QuickTestOptions{
Method: "GET",
URI: "/no-header",
})
if err != nil {
fmt.Println("Error:", err)
return
}
err = res.AssertNoHeader("X-Powered-By")
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Header is not present as expected")
}
// Output: Header is not present as expected
}
// ExampleQTestPlus_AssertString demonstrates how to compare the response body with an expected string.
//
// The simulated HTTP response body contains "pong".
func ExampleQTestPlus_AssertString() {
q := New()
q.Get("/ping", func(c *Ctx) error {
return c.String("pong")
})
res, _ := q.Qtest(QuickTestOptions{
Method: "GET",
URI: "/ping",
})
err := res.AssertString("pong")
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Body matches expected string")
}
// Output: Body matches expected string
}
// ExampleQTestPlus_AssertHeaderHasPrefix demonstrates how to verify if a header starts with a prefix.
//
// The simulated HTTP response includes "X-Version" header with value "v1.2.3".
func ExampleQTestPlus_AssertHeaderHasPrefix() {
q := New()
q.Get("/prefix", func(c *Ctx) error {
c.Set("X-Version", "v1.2.3")
return c.String("OK")
})
res, _ := q.Qtest(QuickTestOptions{
Method: "GET",
URI: "/prefix",
})
err := res.AssertHeaderHasPrefix("X-Version", "v1")
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Header has expected prefix")
}
// Output: Header has expected prefix
}
// ExampleQTestPlus_AssertHeaderHasValueInSet demonstrates how to verify if a header value matches one of the allowed values.
//
// The simulated HTTP response includes "X-Env" header with value "staging".
func ExampleQTestPlus_AssertHeaderHasValueInSet() {
q := New()
q.Get("/variant", func(c *Ctx) error {
c.Set("X-Env", "staging")
return c.String("OK")
})
res, _ := q.Qtest(QuickTestOptions{
Method: "GET",
URI: "/variant",
})
err := res.AssertHeaderHasValueInSet("X-Env", []string{"dev", "staging", "prod"})
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Header value is in allowed set")
}
// Output: Header value is in allowed set
}
// ExampleQTestPlus_AssertHeaderContains demonstrates how to verify if a header contains a substring.
//
// The simulated HTTP response includes "X-Custom" header with value "PoweredByQuick".
func ExampleQTestPlus_AssertHeaderContains() {
q := New()
q.Get("/header", func(c *Ctx) error {
c.Set("X-Custom", "PoweredByQuick")
return c.String("OK")
})
res, _ := q.Qtest(QuickTestOptions{
Method: "GET",
URI: "/header",
})
err := res.AssertHeaderContains("X-Custom", "Quick")
if err != nil {
fmt.Println("Assertion failed:", err)
} else {
fmt.Println("Header contains expected substring")
}
// Output: Header contains expected substring
}