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 }