Repository navigation
Expand file tree
/
Copy pathbuilder_test.go
More file actions
477 lines (401 loc) · 12.3 KB
/
Copy pathbuilder_test.go
File metadata and controls
477 lines (401 loc) · 12.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package greq_test
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/gookit/goutil/x/assert"
"github.com/gookit/greq"
)
func TestBuilder_NewBuilder(t *testing.T) {
b := greq.NewBuilder()
assert.NotNil(t, b)
assert.NotNil(t, b.Options)
assert.NotNil(t, b.Header)
assert.NotNil(t, b.Query)
}
func TestBuilder_BuilderWithClient(t *testing.T) {
client := greq.New("https://api.example.com")
b := greq.BuilderWithClient(client)
assert.NotNil(t, b)
// Builder does not expose a Client() method, so we can't directly test it
// We can test that the builder was created with the client by using it
}
func TestBuilder_WithMethod(t *testing.T) {
b := greq.NewBuilder().WithMethod("POST")
assert.Equal(t, "POST", b.Method)
}
func TestBuilder_PathURL(t *testing.T) {
b := greq.NewBuilder().PathURL("/api/users")
// pathURL is a private field, so we can't directly test it
// Instead, we'll test through the Builder's behavior
assert.NotNil(t, b)
// We could test the actual URL through the Build method, but that requires more setup
}
func TestBuilder_QueryParams(t *testing.T) {
// Test with map[string]string
b := greq.NewBuilder().QueryParams(map[string]string{
"name": "inhere",
"age": "30",
})
assert.Equal(t, "inhere", b.Query.Get("name"))
assert.Equal(t, "30", b.Query.Get("age"))
// Test with url.Values
params := make(map[string][]string)
params["city"] = []string{"Beijing"}
b.QueryParams(params)
assert.Equal(t, "Beijing", b.Query.Get("city"))
}
func TestBuilder_AddQuery(t *testing.T) {
b := greq.NewBuilder().AddQuery("key", "value")
assert.Equal(t, "value", b.Query.Get("key"))
b.AddQuery("key", 123)
values := b.Query["key"]
assert.Len(t, values, 2)
assert.Contains(t, values, "value")
assert.Contains(t, values, "123")
}
func TestBuilder_HeaderOperations(t *testing.T) {
b := greq.NewBuilder()
// Test AddHeader
b.AddHeader("X-Custom", "value1")
assert.Equal(t, "value1", b.Header.Get("X-Custom"))
// Test SetHeader
b.SetHeader("X-Custom", "value2")
assert.Equal(t, "value2", b.Header.Get("X-Custom"))
// Test AddHeaders
headers := make(http.Header)
headers.Add("X-Test1", "val1")
headers.Add("X-Test2", "val2")
b.AddHeaders(headers)
assert.Equal(t, "val1", b.Header.Get("X-Test1"))
assert.Equal(t, "val2", b.Header.Get("X-Test2"))
// Test SetHeaders
newHeaders := make(http.Header)
newHeaders.Set("X-Test1", "new-val1")
newHeaders.Set("X-Test2", "new-val2")
b.SetHeaders(newHeaders)
assert.Equal(t, "new-val1", b.Header.Get("X-Test1"))
assert.Equal(t, "new-val2", b.Header.Get("X-Test2"))
b.RemoveHeaders("X-Test1", "X-Test2")
assert.Equal(t, "", b.Header.Get("X-Test1"))
assert.Equal(t, "", b.Header.Get("X-Test2"))
// Test AddHeaderMap
b.AddHeaderMap(map[string]string{
"X-Map1": "map-val1",
"X-Map2": "map-val2",
})
assert.Equal(t, "map-val1", b.Header.Get("X-Map1"))
assert.Equal(t, "map-val2", b.Header.Get("X-Map2"))
// Test SetHeaderMap
b.SetHeaderMap(map[string]string{
"X-Map1": "new-map-val1",
"X-Map2": "",
})
assert.Equal(t, "new-map-val1", b.Header.Get("X-Map1"))
assert.Equal(t, "", b.Header.Get("X-Map2"))
}
func TestBuilder_UserAgent(t *testing.T) {
b := greq.NewBuilder().UserAgent("test-agent/1.0")
assert.Equal(t, "test-agent/1.0", b.HeaderM["User-Agent"])
}
func TestBuilder_BasicAuth(t *testing.T) {
b := greq.NewBuilder().BasicAuth("user", "pass")
authHeader := b.Header.Get("Authorization")
assert.NotEmpty(t, authHeader)
assert.Contains(t, authHeader, "Basic")
}
func TestBuilder_ContentType(t *testing.T) {
b := greq.NewBuilder()
// Test WithContentType
b.WithContentType("application/custom")
assert.Equal(t, "application/custom", b.ContentType)
// Test JSONType
b.JSONType()
assert.Equal(t, "application/json; charset=utf-8", b.ContentType)
// Test XMLType
b.XMLType()
assert.Equal(t, "application/xml; charset=utf-8", b.ContentType)
// Test FormType
b.FormType()
assert.Equal(t, "application/x-www-form-urlencoded; charset=utf-8", b.ContentType)
// Test MultipartType
b.MultipartType()
assert.Equal(t, "multipart/form-data", b.ContentType)
}
func TestBuilder_BodyOperations(t *testing.T) {
b := greq.NewBuilder()
// Test StringBody
b.StringBody("test body")
assert.NotNil(t, b.Provider)
reader, err := b.Provider.Body()
assert.NoErr(t, err)
body, _ := io.ReadAll(reader)
assert.Equal(t, "test body", string(body))
// Test BytesBody
b.BytesBody([]byte("bytes body"))
reader, err = b.Provider.Body()
assert.NoErr(t, err)
body, _ = io.ReadAll(reader)
assert.Equal(t, "bytes body", string(body))
// Test JSONBody
data := map[string]string{"key": "value"}
b.JSONBody(data)
assert.NotNil(t, b.Provider)
reader, err = b.Provider.Body()
assert.NoErr(t, err)
// Check if it's valid JSON
var result map[string]string
err = json.NewDecoder(reader).Decode(&result)
assert.NoErr(t, err)
assert.Equal(t, "value", result["key"])
// Test FormBody
formData := map[string][]string{
"name": {"inhere"},
"age": {"30"},
}
b.FormBody(formData)
reader, err = b.Provider.Body()
assert.NoErr(t, err)
body, _ = io.ReadAll(reader)
assert.Contains(t, string(body), "name=inhere")
assert.Contains(t, string(body), "age=30")
}
func TestBuilder_Build(t *testing.T) {
b := greq.NewBuilder().
WithMethod("GET").
PathURL("/test")
req, err := b.Build("GET", "/test")
assert.NoErr(t, err)
assert.NotNil(t, req)
assert.Equal(t, "GET", req.Method)
assert.Equal(t, "/test", req.URL.Path)
}
func TestBuilder_HTTPMethods(t *testing.T) {
b := greq.NewBuilder()
// Test Get
b.Get("/get")
assert.Equal(t, "GET", b.Method)
// Note: pathURL is not directly accessible, we test through other means
// Test Post
b.Post("/post", "data")
assert.Equal(t, "POST", b.Method)
assert.Equal(t, "data", b.Body)
// Test Put
b.Put("/put", "data")
assert.Equal(t, "PUT", b.Method)
assert.Equal(t, "data", b.Body)
// Test Delete
b.Delete("/delete")
assert.Equal(t, "DELETE", b.Method)
// Test Patch
b.Patch("/patch", "data")
assert.Equal(t, "PATCH", b.Method)
assert.Equal(t, "data", b.Body)
}
func TestBuilder_String(t *testing.T) {
b := greq.NewBuilder().
WithMethod("GET").
PathURL("/test").
AddHeader("X-Custom", "value")
str := b.String()
assert.NotEmpty(t, str)
assert.Contains(t, str, "GET")
assert.Contains(t, str, "/test")
assert.Contains(t, str, "X-Custom")
}
func TestBodyProvider_Interface(t *testing.T) {
// Test that BodyProvider interface is properly defined
// Using the actual constructor functions from builder.go
b := greq.NewBuilder()
b.BodyReader(strings.NewReader("test"))
assert.NotNil(t, b.Provider)
assert.Equal(t, "", b.Provider.ContentType())
b.JSONBody(map[string]string{"key": "value"})
assert.NotNil(t, b.Provider)
assert.Equal(t, "application/json; charset=utf-8", b.Provider.ContentType())
b.FormBody(url.Values{"key": []string{"value"}})
assert.NotNil(t, b.Provider)
assert.Equal(t, "application/x-www-form-urlencoded; charset=utf-8", b.Provider.ContentType())
}
func TestBodyProvider_ContentType(t *testing.T) {
// Test bodyProvider ContentType
b := greq.NewBuilder().BodyReader(strings.NewReader("test"))
assert.Equal(t, "", b.Provider.ContentType())
ir, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, ir)
bs, err := io.ReadAll(ir)
assert.NoErr(t, err)
assert.Equal(t, "test", string(bs))
}
func TestBodyProvider_Body(t *testing.T) {
reader := strings.NewReader("test body")
b := greq.NewBuilder().BodyReader(reader)
bodyReader, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, bodyReader)
body, _ := io.ReadAll(bodyReader)
assert.Equal(t, "test body", string(body))
}
func TestJsonBodyProvider_ContentType(t *testing.T) {
b := greq.NewBuilder().JSONBody(map[string]string{"key": "value"})
assert.Equal(t, "application/json; charset=utf-8", b.Provider.ContentType())
}
func TestJsonBodyProvider_Body(t *testing.T) {
data := map[string]string{
"name": "inhere",
"age": "30",
}
b := greq.NewBuilder().JSONBody(data)
reader, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, reader)
// Read and decode JSON
body, _ := io.ReadAll(reader)
var result map[string]string
err = json.Unmarshal(body, &result)
assert.NoErr(t, err)
assert.Equal(t, "inhere", result["name"])
assert.Equal(t, "30", result["age"])
}
func TestFormBodyProvider_ContentType(t *testing.T) {
b := greq.NewBuilder().FormBody(url.Values{})
assert.Equal(t, "application/x-www-form-urlencoded; charset=utf-8", b.Provider.ContentType())
}
func TestFormBodyProvider_Body_WithUrlValues(t *testing.T) {
values := make(url.Values)
values.Set("name", "inhere")
values.Set("age", "30")
b := greq.NewBuilder().FormBody(values)
reader, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, reader)
body, _ := io.ReadAll(reader)
bodyStr := string(body)
assert.Contains(t, bodyStr, "name=inhere")
assert.Contains(t, bodyStr, "age=30")
}
func TestFormBodyProvider_Body_WithMapStringSlice(t *testing.T) {
data := make(map[string][]string)
data["name"] = []string{"inhere"}
data["age"] = []string{"30"}
b := greq.NewBuilder().FormBody(data)
reader, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, reader)
body, _ := io.ReadAll(reader)
bodyStr := string(body)
assert.Contains(t, bodyStr, "name=inhere")
assert.Contains(t, bodyStr, "age=30")
}
func TestFormBodyProvider_Body_WithString(t *testing.T) {
data := "name=inhere&age=30"
b := greq.NewBuilder().FormBody(data)
reader, err := b.Provider.Body()
assert.NoErr(t, err)
assert.NotNil(t, reader)
body, _ := io.ReadAll(reader)
assert.Equal(t, data, string(body))
}
func TestFormBodyProvider_WithInvalidData(t *testing.T) {
// We can't directly create formBodyProvider since it's not exported
// Instead, we'll test the behavior through the Builder's FormBody method
// which should handle invalid data gracefully
b := greq.NewBuilder().FormBody(12345) // Passing an invalid type (int)
reader, err := b.Provider.Body()
assert.Err(t, err)
assert.Nil(t, reader)
assert.ErrMsg(t, err, "formBodyProvider: invalid form data type: int")
}
func TestOptions_Struct(t *testing.T) {
opt := greq.NewOpt()
assert.NotNil(t, opt)
assert.NotNil(t, opt.Header)
assert.NotNil(t, opt.HeaderM)
assert.NotNil(t, opt.Query)
}
func TestNewOpt(t *testing.T) {
opt := greq.NewOpt()
assert.NotNil(t, opt)
assert.NotNil(t, opt.Header)
assert.NotNil(t, opt.Query)
}
func TestNewOpt2(t *testing.T) {
opt := greq.NewOpt2(nil, "POST")
assert.NotNil(t, opt)
assert.Equal(t, "POST", opt.Method)
// Test with option functions
opt = greq.NewOpt2([]greq.OptionFn{
greq.WithMethod("GET"),
greq.WithContentType("application/json"),
}, "POST")
assert.Equal(t, "GET", opt.Method) // Should be overridden by option function
assert.Equal(t, "application/json", opt.ContentType)
}
func TestWithMethod(t *testing.T) {
fn := greq.WithMethod("POST")
opt := &greq.Options{}
fn(opt)
assert.Equal(t, "POST", opt.Method)
// Test with empty method (should not change)
fn = greq.WithMethod("")
opt = &greq.Options{Method: "GET"}
fn(opt)
assert.Equal(t, "GET", opt.Method) // Should remain unchanged
}
func TestWithContentType(t *testing.T) {
fn := greq.WithContentType("application/json")
opt := &greq.Options{}
fn(opt)
assert.Equal(t, "application/json", opt.ContentType)
}
func TestWithUserAgent(t *testing.T) {
fn := greq.WithUserAgent("test-agent/1.0")
opt := &greq.Options{
Header: make(http.Header),
}
fn(opt)
assert.Equal(t, "test-agent/1.0", opt.Header.Get("User-Agent"))
}
func TestWithHeader(t *testing.T) {
fn := greq.WithHeader("X-Custom", "value")
opt := &greq.Options{
Header: make(http.Header),
}
fn(opt)
assert.Equal(t, "value", opt.Header.Get("X-Custom"))
}
func TestWithHeaders(t *testing.T) {
fn := greq.WithHeaders(map[string]string{
"X-Custom": "value",
"X-Trace": "abc",
})
opt := &greq.Options{
Header: make(http.Header),
}
fn(opt)
assert.Equal(t, "value", opt.Header.Get("X-Custom"))
assert.Equal(t, "abc", opt.Header.Get("X-Trace"))
}
func TestWithBody(t *testing.T) {
fn := greq.WithBody("test body")
opt := &greq.Options{}
fn(opt)
assert.Equal(t, "test body", opt.Body)
}
func TestWithData(t *testing.T) {
fn := greq.WithData("test data")
opt := &greq.Options{}
fn(opt)
assert.Equal(t, "test data", opt.Data)
}
func TestWithTimeout(t *testing.T) {
// Test that timeout is set correctly
fn := greq.WithTimeout(5000)
opt := &greq.Options{}
fn(opt)
assert.Equal(t, 5000, opt.Timeout)
}