fix:使用Client时,设置的参数不生效
This commit is contained in:
parent
b90c59d6e7
commit
0e2f91eee2
6
curl.go
6
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
|
||||
}
|
||||
|
30
curl_test.go
30
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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user