Skip to content

Commit ffc85fa

Browse files
committed
update: coverage
1 parent 735c6d3 commit ffc85fa

File tree

7 files changed

+764
-44
lines changed

7 files changed

+764
-44
lines changed

.github/workflows/go.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ jobs:
2323

2424
- name: Build
2525
run: go build -v ./...
26-
- name: Test
27-
run: go test -v -race ./...
2826
- name: Benchmark
2927
run: go test -race -run=^$ -bench=. -benchmem ./...
30-
- name: Coverage
31-
run: go test -coverprofile=coverage.txt
28+
- name: Test
29+
run: go test -v -race -coverprofile=coverage.txt ./...
3230
- name: Upload coverage reports to Codecov
3331
uses: codecov/codecov-action@v5
3432
with:

request.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func makeBody(body any) (io.Reader, error) {
3030
return bytes.NewReader(v), nil
3131
case string:
3232
return strings.NewReader(v), nil
33-
case *bytes.Buffer, bytes.Buffer:
33+
case *bytes.Buffer:
3434
return body.(io.Reader), nil
3535
case io.Reader, io.ReadSeeker, *bytes.Reader, *strings.Reader:
3636
return body.(io.Reader), nil

request_test.go

+113-38
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,110 @@ package requests
33
import (
44
"bytes"
55
"context"
6+
"io"
67
"net/http"
78
"net/url"
89
"strings"
910
"testing"
1011
)
1112

12-
func Test_makeBody(t *testing.T) {
13+
func TestMakeBody(t *testing.T) {
1314
tests := []struct {
1415
name string
1516
body any
17+
want string
1618
wantErr bool
1719
}{
1820
{
1921
name: "nil body",
2022
body: nil,
23+
want: "",
2124
},
2225
{
23-
name: "byte slice body",
26+
name: "byte slice",
2427
body: []byte("test data"),
28+
want: "test data",
2529
},
2630
{
27-
name: "string body",
28-
body: "test data",
31+
name: "string",
32+
body: "test string",
33+
want: "test string",
2934
},
3035
{
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",
3339
},
3440
{
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",
3744
},
3845
{
39-
name: "url.Values body",
46+
name: "url.Values",
4047
body: url.Values{"key": {"value"}},
48+
want: "key=value",
4149
},
4250
{
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",
4459
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,
4775
},
4876
}
4977

5078
for _, tt := range tests {
5179
t.Run(tt.name, func(t *testing.T) {
5280
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+
}
5586
return
5687
}
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)
6191
return
6292
}
63-
if reader == nil {
64-
t.Error("expected non-nil reader")
93+
94+
if reader == nil && tt.want != "" {
95+
t.Error("期望非空 reader 但得到 nil")
6596
return
6697
}
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+
}
67110
})
68111
}
69112
}
@@ -72,48 +115,80 @@ func TestNewRequestWithContext(t *testing.T) {
72115
tests := []struct {
73116
name string
74117
opts []Option
118+
want func(*http.Request) bool
75119
wantErr bool
76120
}{
77121
{
78-
name: "basic request",
122+
name: "基本请求",
79123
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+
},
80128
},
81129
{
82-
name: "request with path",
130+
name: "带路径参数",
83131
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+
},
84135
},
85136
{
86-
name: "request with query",
137+
name: "带查询参数",
87138
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+
},
88143
},
89144
{
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+
},
92150
},
93151
{
94-
name: "request with cookies",
152+
name: "带Cookie",
95153
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,
96170
},
97171
}
98172

99173
for _, tt := range tests {
100174
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+
}
105182
return
106183
}
107-
if req == nil {
108-
t.Error("expected non-nil request")
184+
185+
if err != nil {
186+
t.Errorf("NewRequestWithContext() 错误 = %v", err)
109187
return
110188
}
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("请求不符合预期条件")
117192
}
118193
})
119194
}

response_writer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func TestResponseWriterRead(t *testing.T) {
245245
// 测试读取完后的EOF
246246
n, err = w.Read(buf)
247247
if err != io.EOF {
248-
t.Errorf("Expected EOF after reading all data, got %v", err)
248+
t.Errorf("Expected EOF after reading all data, got n=%d, err=%v", n, err)
249249
}
250250
}
251251

0 commit comments

Comments
 (0)