-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
195 lines (181 loc) · 3.86 KB
/
request_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
package requests
import (
"bytes"
"context"
"io"
"net/http"
"net/url"
"strings"
"testing"
)
func TestMakeBody(t *testing.T) {
tests := []struct {
name string
body any
want string
wantErr bool
}{
{
name: "nil body",
body: nil,
want: "",
},
{
name: "byte slice",
body: []byte("test data"),
want: "test data",
},
{
name: "string",
body: "test string",
want: "test string",
},
{
name: "bytes.Buffer pointer",
body: bytes.NewBuffer([]byte("buffer data")),
want: "buffer data",
},
{
name: "strings.Reader",
body: strings.NewReader("reader data"),
want: "reader data",
},
{
name: "url.Values",
body: url.Values{"key": {"value"}},
want: "key=value",
},
{
name: "func returning ReadCloser",
body: func() (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("func data")), nil
},
want: "func data",
},
{
name: "struct to JSON",
body: struct {
Key string `json:"key"`
}{Key: "value"},
want: `{"key":"value"}`,
},
{
name: "error func",
body: func() (io.ReadCloser, error) {
return nil, io.EOF
},
wantErr: true,
},
{
name: "invalid JSON",
body: make(chan int),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader, err := makeBody(tt.body)
if tt.wantErr {
if err == nil {
t.Error("期望错误但得到 nil")
}
return
}
if err != nil {
t.Errorf("makeBody() 错误 = %v", err)
return
}
if reader == nil && tt.want != "" {
t.Error("期望非空 reader 但得到 nil")
return
}
if reader != nil {
got, err := io.ReadAll(reader)
if err != nil {
t.Errorf("读取 body 失败: %v", err)
return
}
if string(got) != tt.want {
t.Errorf("makeBody() = %v, 期望 %v", string(got), tt.want)
}
}
})
}
}
func TestNewRequestWithContext(t *testing.T) {
tests := []struct {
name string
opts []Option
want func(*http.Request) bool
wantErr bool
}{
{
name: "基本请求",
opts: []Option{MethodGet, URL("http://example.com")},
want: func(r *http.Request) bool {
return r.Method == "GET" &&
r.URL.String() == "http://example.com"
},
},
{
name: "带路径参数",
opts: []Option{MethodGet, URL("http://example.com"), Path("/api"), Path("/v1")},
want: func(r *http.Request) bool {
return r.URL.Path == "/api/v1"
},
},
{
name: "带查询参数",
opts: []Option{MethodGet, URL("http://example.com"), Param("key", "value")},
want: func(r *http.Request) bool {
return r.URL.RawQuery == "key=value"
},
},
{
name: "带请求头",
opts: []Option{MethodGet, URL("http://example.com"), Header("X-Test", "test-value")},
want: func(r *http.Request) bool {
return r.Header.Get("X-Test") == "test-value"
},
},
{
name: "带Cookie",
opts: []Option{MethodGet, URL("http://example.com"), Cookie(http.Cookie{Name: "session", Value: "123"})},
want: func(r *http.Request) bool {
cookies := r.Cookies()
return len(cookies) == 1 &&
cookies[0].Name == "session" &&
cookies[0].Value == "123"
},
},
{
name: "无效URL",
opts: []Option{MethodGet, URL("://invalid"), Cookie(http.Cookie{Name: "session", Value: "123"})},
wantErr: true,
},
{
name: "无效body",
opts: []Option{MethodGet, URL("http://example.com"), Body(make(chan int))},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
req, err := NewRequestWithContext(ctx, newOptions(tt.opts))
if tt.wantErr {
if err == nil {
t.Error("期望错误但得到 nil")
}
return
}
if err != nil {
t.Errorf("NewRequestWithContext() 错误 = %v", err)
return
}
if !tt.want(req) {
t.Errorf("请求不符合预期条件")
}
})
}
}