-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquicktest_example_v2_test.go
216 lines (177 loc) · 5.2 KB
/
quicktest_example_v2_test.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
// 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())
// Out put: 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")
}
// Out put: 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")
}
// Out put: 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")
}
// Out put: 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()))
// Out put: 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())
// Out put: 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())
// Out put: 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)
// Out put: Response Status: 200 OK
}