From 0e2f91eee217ec2187797ea456427bcdcff13e9a Mon Sep 17 00:00:00 2001 From: starainrt Date: Tue, 14 Oct 2025 10:08:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BD=BF=E7=94=A8Client=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=9A=84=E5=8F=82=E6=95=B0=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- curl.go | 6 +++++- curl_test.go | 30 ++++++++++++++++++++++++++++++ curl_transport.go | 13 +++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/curl.go b/curl.go index 2570dfe..be7896d 100644 --- a/curl.go +++ b/curl.go @@ -1856,7 +1856,11 @@ func NewSimpleRequestWithContextWithClient(ctx context.Context, client Client, u } func NewRequestWithContextWithClient(ctx context.Context, client Client, uri string, method string, opts ...RequestOpt) (*Request, error) { - req, err := newRequest(ctx, uri, method, opts...) + if client.opts == nil { + client.opts = []RequestOpt{} + } + cOpts := append(client.opts, opts...) + req, err := newRequest(ctx, uri, method, cOpts...) if err != nil { return nil, err } diff --git a/curl_test.go b/curl_test.go index e8a853c..8e8dc3e 100644 --- a/curl_test.go +++ b/curl_test.go @@ -696,3 +696,33 @@ func TestWithTimeout(t *testing.T) { resp.CloseAll() } } + +func TestConfigWithClient(t *testing.T) { + server := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + if req.Header.Get("hello") != "world" { + rw.WriteHeader(http.StatusBadRequest) + rw.Write([]byte("hello world failed")) + return + } + rw.Write([]byte(`OK`)) + })) + defer server.Close() + client, err := NewHttpClient(WithSkipTLSVerify(true)) + if err != nil { + t.Error(err) + } + req := client.NewSimpleRequest(server.URL, "GET", WithHeader("hello", "world")) + //SetClientSkipVerify(client, true) + //req.SetDoRawClient(false) + //req.SetDoRawTransport(false) + resp, err := req.Do() + if err != nil { + t.Error(err) + } + fmt.Println(resp.Proto) + if resp.StatusCode != 200 { + resp.CloseAll() + t.Errorf("status code is %d", resp.StatusCode) + } + resp.CloseAll() +} diff --git a/curl_transport.go b/curl_transport.go index e14ae6d..943f1fe 100644 --- a/curl_transport.go +++ b/curl_transport.go @@ -10,6 +10,18 @@ import ( type Client struct { *http.Client + opts []RequestOpt +} + +func (c Client) Options() []RequestOpt { + return c.opts +} + +func (c Client) SetOptions(opts ...RequestOpt) Client { + return Client{ + Client: c.Client, + opts: opts, + } } // NewHttpClient creates a new http.Client with the specified options. @@ -24,6 +36,7 @@ func NewHttpClient(opts ...RequestOpt) (Client, error) { cl, err := req.HttpClient() return Client{ Client: cl, + opts: opts, }, err }