From 3b3698d084d17def17f7d1f4aa0e01a0dcab2928 Mon Sep 17 00:00:00 2001 From: "jared.lu" Date: Tue, 5 Nov 2024 20:13:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?httpx:=E5=A2=9E=E5=8A=A0error=E5=88=A4?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/httpx/request.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/net/httpx/request.go b/net/httpx/request.go index e47e4d9..b4e361e 100644 --- a/net/httpx/request.go +++ b/net/httpx/request.go @@ -39,6 +39,9 @@ func NewRequest(ctx context.Context, method, url string) *Request { // JSONBody 使用 JSON body func (req *Request) JSONBody(val any) *Request { + if req.err != nil { + return req + } req.req.Body = io.NopCloser(iox.NewJSONReader(val)) req.req.Header.Set("Content-Type", "application/json") return req @@ -50,6 +53,9 @@ func (req *Request) Client(cli *http.Client) *Request { } func (req *Request) AddHeader(key string, value string) *Request { + if req.err != nil { + return req + } req.req.Header.Add(key, value) return req } @@ -57,6 +63,9 @@ func (req *Request) AddHeader(key string, value string) *Request { // AddParam 添加查询参数 // 这个方法性能不好,但是好用 func (req *Request) AddParam(key string, value string) *Request { + if req.err != nil { + return req + } q := req.req.URL.Query() q.Add(key, value) req.req.URL.RawQuery = q.Encode() From d869c7f4ebdddea4a8f987860f2f2645e0b63e71 Mon Sep 17 00:00:00 2001 From: "jared.lu" Date: Thu, 7 Nov 2024 21:02:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.sum | 2 ++ net/httpx/request_test.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/go.sum b/go.sum index 0f76ae2..9c0fd0f 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= diff --git a/net/httpx/request_test.go b/net/httpx/request_test.go index 1ed05e7..8d64904 100644 --- a/net/httpx/request_test.go +++ b/net/httpx/request_test.go @@ -116,3 +116,9 @@ func TestRequestAddHeader(t *testing.T) { type User struct { Name string } + +func TestNewRequest_ReturnError(t *testing.T) { + req := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a") + assert.NotNil(t, req.err) + assert.Nil(t, req.req) +} From 4b7c1456f032e9e4a229e2beef512d572f35bee4 Mon Sep 17 00:00:00 2001 From: "jared.lu" Date: Sun, 10 Nov 2024 15:40:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/httpx/request_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/net/httpx/request_test.go b/net/httpx/request_test.go index 8d64904..fd84ba9 100644 --- a/net/httpx/request_test.go +++ b/net/httpx/request_test.go @@ -40,6 +40,10 @@ func TestRequest_JSONBody(t *testing.T) { req = req.JSONBody(User{}) assert.NotNil(t, req.req.Body) assert.Equal(t, "application/json", req.req.Header.Get("Content-Type")) + + req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a") + assert.NotNil(t, req2.err) + assert.Nil(t, req2.req) } func TestRequest_Do(t *testing.T) { @@ -103,6 +107,10 @@ func TestRequest_AddParam(t *testing.T) { AddParam("key1", "value1"). AddParam("key2", "value2") assert.Equal(t, "http://localhost?key1=value1&key2=value2", req.req.URL.String()) + + req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a") + assert.NotNil(t, req2.err) + assert.Nil(t, req2.req) } func TestRequestAddHeader(t *testing.T) { @@ -111,14 +119,12 @@ func TestRequestAddHeader(t *testing.T) { AddHeader("head1", "val1").AddHeader("head1", "val2") vals := req.req.Header.Values("head1") assert.Equal(t, []string{"val1", "val2"}, vals) + + req2 := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a") + assert.NotNil(t, req2.err) + assert.Nil(t, req2.req) } type User struct { Name string } - -func TestNewRequest_ReturnError(t *testing.T) { - req := NewRequest(context.Background(), http.MethodGet, "://localhost:80/a") - assert.NotNil(t, req.err) - assert.Nil(t, req.req) -}