fix:使用Client时,设置的参数不生效

This commit is contained in:
兔子 2025-10-14 10:08:53 +08:00
parent b90c59d6e7
commit 0e2f91eee2
Signed by: b612
GPG Key ID: 99DD2222B612B612
3 changed files with 48 additions and 1 deletions

View File

@ -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) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -696,3 +696,33 @@ func TestWithTimeout(t *testing.T) {
resp.CloseAll() 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()
}

View File

@ -10,6 +10,18 @@ import (
type Client struct { type Client struct {
*http.Client *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. // NewHttpClient creates a new http.Client with the specified options.
@ -24,6 +36,7 @@ func NewHttpClient(opts ...RequestOpt) (Client, error) {
cl, err := req.HttpClient() cl, err := req.HttpClient()
return Client{ return Client{
Client: cl, Client: cl,
opts: opts,
}, err }, err
} }