This commit is contained in:
兔子 2025-08-15 15:07:51 +08:00
parent e3b7369e12
commit d260181adf
Signed by: b612
GPG Key ID: 99DD2222B612B612
2 changed files with 62 additions and 0 deletions

21
curl.go
View File

@ -143,6 +143,8 @@ func (r *Request) Clone() *Request {
}
if r.doRawClient {
clonedRequest.rawClient = r.rawClient
} else {
clonedRequest.rawClient = new(http.Client)
}
if r.doRawRequest {
clonedRequest.rawRequest = r.rawRequest
@ -726,6 +728,7 @@ func (r *RequestOpts) SkipTLSVerify() bool {
return r.skipTLSVerify
}
// SetSkipTLSVerify This function will Not Work when use rawClient,use SetClientSkipVerify instead
func (r *Request) SetSkipTLSVerify(skipTLSVerify bool) *Request {
r.skipTLSVerify = skipTLSVerify
return r
@ -1793,3 +1796,21 @@ func NewRequestWithContextWithClient(ctx context.Context, client *http.Client, u
req.SetDoRawTransport(true)
return req, err
}
func SetClientSkipVerify(c *http.Client, val bool) error {
switch tp := c.Transport.(type) {
case *http.Transport:
if tp.TLSClientConfig == nil {
tp.TLSClientConfig = &tls.Config{}
}
tp.TLSClientConfig.InsecureSkipVerify = val
case nil:
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: val},
}
c.Transport = transport
default:
return fmt.Errorf("unsupported transport type: %T", tp)
}
return nil
}

View File

@ -552,3 +552,44 @@ func TestUploadFile(t *testing.T) {
}
resp.CloseAll()
}
func TestTlsConfig(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(false))
if err != nil {
t.Error(err)
}
req := NewSimpleRequestWithClient(client, server.URL, "GET", WithHeader("hello", "world"))
//SetClientSkipVerify(client, true)
req.SetDoRawClient(false)
//req.SetDoRawTransport(false)
req.SetSkipTLSVerify(true)
resp, err := req.Do()
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
resp.CloseAll()
t.Errorf("status code is %d", resp.StatusCode)
}
resp.CloseAll()
req = req.Clone()
req.AddHeader("ok", "good")
resp, err = req.Do()
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
resp.CloseAll()
t.Errorf("status code is %d", resp.StatusCode)
}
resp.CloseAll()
}