-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_request_builder_test.go
More file actions
330 lines (278 loc) · 7.43 KB
/
example_request_builder_test.go
File metadata and controls
330 lines (278 loc) · 7.43 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
package httpx_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/slashdevops/httpx"
)
// ExampleRequestBuilder_simpleGET demonstrates how to create a simple GET request.
func ExampleRequestBuilder_simpleGET() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/users").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.String())
// Output:
// GET
// https://api.example.com/users
}
// ExampleRequestBuilder_postWithJSON demonstrates how to create a POST request with JSON body.
func ExampleRequestBuilder_postWithJSON() {
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
user := User{Name: "John Doe", Email: "john@example.com"}
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodPOST().
WithPath("/users").
WithJSONBody(user).
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.Header.Get("Content-Type"))
// Output:
// POST
// application/json
}
// ExampleRequestBuilder_withQueryParams demonstrates how to add query parameters.
func ExampleRequestBuilder_withQueryParams() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/search").
WithQueryParam("q", "golang").
WithQueryParam("limit", "10").
WithQueryParam("offset", "0").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.URL.String())
// Output:
// https://api.example.com/search?limit=10&offset=0&q=golang
}
// ExampleRequestBuilder_withBasicAuth demonstrates how to use basic authentication.
func ExampleRequestBuilder_withBasicAuth() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/protected").
WithBasicAuth("username", "password").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
authHeader := req.Header.Get("Authorization")
fmt.Println(authHeader[:6]) // Just print "Basic " prefix
// Output:
// Basic
}
// ExampleRequestBuilder_withBearerAuth demonstrates how to use bearer token authentication.
func ExampleRequestBuilder_withBearerAuth() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/api/data").
WithBearerAuth("your-token-here").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
authHeader := req.Header.Get("Authorization")
fmt.Println(authHeader[:7]) // Just print "Bearer " prefix
// Output:
// Bearer
}
// ExampleRequestBuilder_withAcceptHeader demonstrates how to set the Accept header.
func ExampleRequestBuilder_withAcceptHeader() {
// Using the WithAccept() convenience method
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/api/data").
WithAccept("application/json").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Header.Get("Accept"))
// Output:
// application/json
}
// ExampleRequestBuilder_withMultipleAcceptTypes demonstrates setting multiple Accept types with quality values.
func ExampleRequestBuilder_withMultipleAcceptTypes() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/content").
WithAccept("application/json, application/xml;q=0.9, */*;q=0.8").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Header.Get("Accept"))
// Output:
// application/json, application/xml;q=0.9, */*;q=0.8
}
// ExampleRequestBuilder_complexRequest demonstrates a complex request with multiple options.
func ExampleRequestBuilder_complexRequest() {
type RequestData struct {
Action string `json:"action"`
Count int `json:"count"`
}
data := RequestData{Action: "update", Count: 5}
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodPUT().
WithPath("/resources/123").
WithQueryParam("force", "true").
WithHeader("X-Custom-Header", "custom-value").
WithUserAgent("MyApp/1.0").
WithBearerAuth("token123").
WithJSONBody(data).
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.Path)
fmt.Println(req.Header.Get("User-Agent"))
fmt.Println(req.Header.Get("X-Custom-Header"))
fmt.Println(req.Header.Get("Content-Type"))
// Output:
// PUT
// /resources/123
// MyApp/1.0
// custom-value
// application/json
}
// ExampleRequestBuilder_withContext demonstrates how to use a context.
func ExampleRequestBuilder_withContext() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodGET().
WithPath("/data").
WithContext(ctx).
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Context() != nil)
// Output:
// true
}
// ExampleRequestBuilder_withMethodHEAD demonstrates how to create a HEAD request.
func ExampleRequestBuilder_withMethodHEAD() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodHEAD().
WithPath("/resource").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.Path)
// Output:
// HEAD
// /resource
}
// ExampleRequestBuilder_withMethodOPTIONS demonstrates how to create an OPTIONS request.
func ExampleRequestBuilder_withMethodOPTIONS() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodOPTIONS().
WithPath("/api/users").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.Path)
// Output:
// OPTIONS
// /api/users
}
// ExampleRequestBuilder_withMethodTRACE demonstrates how to create a TRACE request.
func ExampleRequestBuilder_withMethodTRACE() {
req, err := httpx.NewRequestBuilder("https://api.example.com").
WithMethodTRACE().
WithPath("/debug").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.Path)
// Output:
// TRACE
// /debug
}
// ExampleRequestBuilder_withMethodCONNECT demonstrates how to create a CONNECT request.
func ExampleRequestBuilder_withMethodCONNECT() {
req, err := httpx.NewRequestBuilder("https://proxy.example.com").
WithMethodCONNECT().
WithPath("/tunnel").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(req.Method)
fmt.Println(req.URL.Path)
// Output:
// CONNECT
// /tunnel
}
// ExampleRequestBuilder_fullExample demonstrates a complete end-to-end example with a test server.
func ExampleRequestBuilder_fullExample() {
// Create a test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"message":"success"}`))
}))
defer server.Close()
req, err := httpx.NewRequestBuilder(server.URL).
WithMethodGET().
WithPath("/api/test").
WithHeader("Accept", "application/json").
Build()
if err != nil {
fmt.Println("Error:", err)
return
}
// Execute the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(resp.StatusCode)
fmt.Println(string(body))
// Output:
// 200
// {"message":"success"}
}