@@ -3,67 +3,110 @@ package requests
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "io"
6
7
"net/http"
7
8
"net/url"
8
9
"strings"
9
10
"testing"
10
11
)
11
12
12
- func Test_makeBody (t * testing.T ) {
13
+ func TestMakeBody (t * testing.T ) {
13
14
tests := []struct {
14
15
name string
15
16
body any
17
+ want string
16
18
wantErr bool
17
19
}{
18
20
{
19
21
name : "nil body" ,
20
22
body : nil ,
23
+ want : "" ,
21
24
},
22
25
{
23
- name : "byte slice body " ,
26
+ name : "byte slice" ,
24
27
body : []byte ("test data" ),
28
+ want : "test data" ,
25
29
},
26
30
{
27
- name : "string body" ,
28
- body : "test data" ,
31
+ name : "string" ,
32
+ body : "test string" ,
33
+ want : "test string" ,
29
34
},
30
35
{
31
- name : "bytes buffer body" ,
32
- body : bytes .NewBuffer ([]byte ("test data" )),
36
+ name : "bytes.Buffer pointer" ,
37
+ body : bytes .NewBuffer ([]byte ("buffer data" )),
38
+ want : "buffer data" ,
33
39
},
34
40
{
35
- name : "io.Reader body" ,
36
- body : strings .NewReader ("test data" ),
41
+ name : "strings.Reader" ,
42
+ body : strings .NewReader ("reader data" ),
43
+ want : "reader data" ,
37
44
},
38
45
{
39
- name : "url.Values body " ,
46
+ name : "url.Values" ,
40
47
body : url.Values {"key" : {"value" }},
48
+ want : "key=value" ,
41
49
},
42
50
{
43
- name : "struct body" ,
51
+ name : "func returning ReadCloser" ,
52
+ body : func () (io.ReadCloser , error ) {
53
+ return io .NopCloser (strings .NewReader ("func data" )), nil
54
+ },
55
+ want : "func data" ,
56
+ },
57
+ {
58
+ name : "struct to JSON" ,
44
59
body : struct {
45
- Name string `json:"name"`
46
- }{"test" },
60
+ Key string `json:"key"`
61
+ }{Key : "value" },
62
+ want : `{"key":"value"}` ,
63
+ },
64
+ {
65
+ name : "error func" ,
66
+ body : func () (io.ReadCloser , error ) {
67
+ return nil , io .EOF
68
+ },
69
+ wantErr : true ,
70
+ },
71
+ {
72
+ name : "invalid JSON" ,
73
+ body : make (chan int ),
74
+ wantErr : true ,
47
75
},
48
76
}
49
77
50
78
for _ , tt := range tests {
51
79
t .Run (tt .name , func (t * testing.T ) {
52
80
reader , err := makeBody (tt .body )
53
- if (err != nil ) != tt .wantErr {
54
- t .Errorf ("makeBody() error = %v, wantErr %v" , err , tt .wantErr )
81
+
82
+ if tt .wantErr {
83
+ if err == nil {
84
+ t .Error ("期望错误但得到 nil" )
85
+ }
55
86
return
56
87
}
57
- if tt .body == nil {
58
- if reader != nil {
59
- t .Error ("expected nil reader for nil body" )
60
- }
88
+
89
+ if err != nil {
90
+ t .Errorf ("makeBody() 错误 = %v" , err )
61
91
return
62
92
}
63
- if reader == nil {
64
- t .Error ("expected non-nil reader" )
93
+
94
+ if reader == nil && tt .want != "" {
95
+ t .Error ("期望非空 reader 但得到 nil" )
65
96
return
66
97
}
98
+
99
+ if reader != nil {
100
+ got , err := io .ReadAll (reader )
101
+ if err != nil {
102
+ t .Errorf ("读取 body 失败: %v" , err )
103
+ return
104
+ }
105
+
106
+ if string (got ) != tt .want {
107
+ t .Errorf ("makeBody() = %v, 期望 %v" , string (got ), tt .want )
108
+ }
109
+ }
67
110
})
68
111
}
69
112
}
@@ -72,48 +115,80 @@ func TestNewRequestWithContext(t *testing.T) {
72
115
tests := []struct {
73
116
name string
74
117
opts []Option
118
+ want func (* http.Request ) bool
75
119
wantErr bool
76
120
}{
77
121
{
78
- name : "basic request " ,
122
+ name : "基本请求 " ,
79
123
opts : []Option {MethodGet , URL ("http://example.com" )},
124
+ want : func (r * http.Request ) bool {
125
+ return r .Method == "GET" &&
126
+ r .URL .String () == "http://example.com"
127
+ },
80
128
},
81
129
{
82
- name : "request with path " ,
130
+ name : "带路径参数 " ,
83
131
opts : []Option {MethodGet , URL ("http://example.com" ), Path ("/api" ), Path ("/v1" )},
132
+ want : func (r * http.Request ) bool {
133
+ return r .URL .Path == "/api/v1"
134
+ },
84
135
},
85
136
{
86
- name : "request with query " ,
137
+ name : "带查询参数 " ,
87
138
opts : []Option {MethodGet , URL ("http://example.com" ), Param ("key" , "value" )},
139
+
140
+ want : func (r * http.Request ) bool {
141
+ return r .URL .RawQuery == "key=value"
142
+ },
88
143
},
89
144
{
90
- name : "request with headers" ,
91
- opts : []Option {MethodGet , URL ("http://example.com" ), Header ("Content-Type" , "application/json" )},
145
+ name : "带请求头" ,
146
+ opts : []Option {MethodGet , URL ("http://example.com" ), Header ("X-Test" , "test-value" )},
147
+ want : func (r * http.Request ) bool {
148
+ return r .Header .Get ("X-Test" ) == "test-value"
149
+ },
92
150
},
93
151
{
94
- name : "request with cookies " ,
152
+ name : "带Cookie " ,
95
153
opts : []Option {MethodGet , URL ("http://example.com" ), Cookie (http.Cookie {Name : "session" , Value : "123" })},
154
+ want : func (r * http.Request ) bool {
155
+ cookies := r .Cookies ()
156
+ return len (cookies ) == 1 &&
157
+ cookies [0 ].Name == "session" &&
158
+ cookies [0 ].Value == "123"
159
+ },
160
+ },
161
+ {
162
+ name : "无效URL" ,
163
+ opts : []Option {MethodGet , URL ("://invalid" ), Cookie (http.Cookie {Name : "session" , Value : "123" })},
164
+ wantErr : true ,
165
+ },
166
+ {
167
+ name : "无效body" ,
168
+ opts : []Option {MethodGet , URL ("http://example.com" ), Body (make (chan int ))},
169
+ wantErr : true ,
96
170
},
97
171
}
98
172
99
173
for _ , tt := range tests {
100
174
t .Run (tt .name , func (t * testing.T ) {
101
- options := newOptions (tt .opts )
102
- req , err := NewRequestWithContext (context .Background (), options )
103
- if (err != nil ) != tt .wantErr {
104
- t .Errorf ("NewRequestWithContext() error = %v, wantErr %v" , err , tt .wantErr )
175
+ ctx := context .Background ()
176
+ req , err := NewRequestWithContext (ctx , newOptions (tt .opts ))
177
+
178
+ if tt .wantErr {
179
+ if err == nil {
180
+ t .Error ("期望错误但得到 nil" )
181
+ }
105
182
return
106
183
}
107
- if req == nil {
108
- t .Error ("expected non-nil request" )
184
+
185
+ if err != nil {
186
+ t .Errorf ("NewRequestWithContext() 错误 = %v" , err )
109
187
return
110
188
}
111
- // Verify request properties
112
- if req .Method != options .Method {
113
- t .Errorf ("expected method %s, got %s" , options .Method , req .Method )
114
- }
115
- if ! strings .HasPrefix (req .URL .String (), options .URL ) {
116
- t .Errorf ("expected URL to start with %s, got %s" , options .URL , req .URL .String ())
189
+
190
+ if ! tt .want (req ) {
191
+ t .Errorf ("请求不符合预期条件" )
117
192
}
118
193
})
119
194
}
0 commit comments