Skip to content

Commit 54999ed

Browse files
Jared-luflycash
authored andcommitted
httpx:增加error判断 (#264)
* httpx:增加error判断 * 补充测试 * 补充方法的测试用例
1 parent b871931 commit 54999ed

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN
2222
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
2323
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
2424
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
25+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
26+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2527
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
2628
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
2729
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=

net/httpx/request.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func NewRequest(ctx context.Context, method, url string) *Request {
3939

4040
// JSONBody 使用 JSON body
4141
func (req *Request) JSONBody(val any) *Request {
42+
if req.err != nil {
43+
return req
44+
}
4245
req.req.Body = io.NopCloser(iox.NewJSONReader(val))
4346
req.req.Header.Set("Content-Type", "application/json")
4447
return req
@@ -50,13 +53,19 @@ func (req *Request) Client(cli *http.Client) *Request {
5053
}
5154

5255
func (req *Request) AddHeader(key string, value string) *Request {
56+
if req.err != nil {
57+
return req
58+
}
5359
req.req.Header.Add(key, value)
5460
return req
5561
}
5662

5763
// AddParam 添加查询参数
5864
// 这个方法性能不好,但是好用
5965
func (req *Request) AddParam(key string, value string) *Request {
66+
if req.err != nil {
67+
return req
68+
}
6069
q := req.req.URL.Query()
6170
q.Add(key, value)
6271
req.req.URL.RawQuery = q.Encode()

net/httpx/request_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func TestRequest_JSONBody(t *testing.T) {
4040
req = req.JSONBody(User{})
4141
assert.NotNil(t, req.req.Body)
4242
assert.Equal(t, "application/json", req.req.Header.Get("Content-Type"))
43+
44+
req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a")
45+
assert.NotNil(t, req2.err)
46+
assert.Nil(t, req2.req)
4347
}
4448

4549
func TestRequest_Do(t *testing.T) {
@@ -103,6 +107,10 @@ func TestRequest_AddParam(t *testing.T) {
103107
AddParam("key1", "value1").
104108
AddParam("key2", "value2")
105109
assert.Equal(t, "http://localhost?key1=value1&key2=value2", req.req.URL.String())
110+
111+
req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a")
112+
assert.NotNil(t, req2.err)
113+
assert.Nil(t, req2.req)
106114
}
107115

108116
func TestRequestAddHeader(t *testing.T) {
@@ -111,6 +119,10 @@ func TestRequestAddHeader(t *testing.T) {
111119
AddHeader("head1", "val1").AddHeader("head1", "val2")
112120
vals := req.req.Header.Values("head1")
113121
assert.Equal(t, []string{"val1", "val2"}, vals)
122+
123+
req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a")
124+
assert.NotNil(t, req2.err)
125+
assert.Nil(t, req2.req)
114126
}
115127

116128
type User struct {

0 commit comments

Comments
 (0)