Skip to content

Commit c3424bf

Browse files
committed
feat: add InsecureSkipVerify field to RequestOptions.
Signed-off-by: ghosind <ghosind@gmail.com>
1 parent 46718f2 commit c3424bf

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

client.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package request
22

33
import (
4+
"crypto/tls"
45
"net"
56
"net/http"
67
"net/url"
@@ -408,12 +409,15 @@ func (cli *Client) defaultValidateStatus(status int) bool {
408409
// getTransport gets the transport by the request options.
409410
func (cli *Client) getTransport(opt RequestOptions) http.RoundTripper {
410411
proxy := cli.getProxy(opt)
411-
if proxy == nil {
412+
tls := cli.getTLSConfig(opt)
413+
414+
if proxy == nil && tls == nil {
412415
return nil
413416
}
414417

415418
return &http.Transport{
416-
Proxy: proxy,
419+
Proxy: proxy,
420+
TLSClientConfig: tls,
417421
}
418422
}
419423

@@ -437,3 +441,15 @@ func (cli *Client) getProxy(opt RequestOptions) func(*http.Request) (*url.URL, e
437441

438442
return http.ProxyURL(proxyUrl)
439443
}
444+
445+
// getTLSConfig tries to get the config that used to configure a TLS client or server.
446+
func (cli *Client) getTLSConfig(opt RequestOptions) *tls.Config {
447+
if !opt.InsecureSkipVerify {
448+
return nil
449+
}
450+
451+
cfg := new(tls.Config)
452+
cfg.InsecureSkipVerify = opt.InsecureSkipVerify
453+
454+
return cfg
455+
}

request_options.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ type RequestOptions struct {
100100
// environment variables. If no proxy config in the request options or the client config, the
101101
// request will try to get a proxy from the environment variables.
102102
Proxy *ProxyConfig
103+
// InsecureSkipVerify controls whether the HTTP client verifies the server's certificate and host
104+
// name.
105+
InsecureSkipVerify bool
103106
// Timeout specifies the number of milliseconds before the request times out. This value will be
104107
// ignored if the `Content` field in the request options is set. It indicates no time-out
105108
// limitation if the value is -1.
@@ -470,6 +473,14 @@ func (opt *RequestOptions) SetProxy(proxy ProxyConfig) *RequestOptions {
470473
return opt
471474
}
472475

476+
// SetInsecureSkipVerify sets and controls whether the HTTP client verifies the server's
477+
// certificate and host name.
478+
func (opt *RequestOptions) SetInsecureSkipVerify(skipVerify bool) *RequestOptions {
479+
opt.InsecureSkipVerify = skipVerify
480+
481+
return opt
482+
}
483+
473484
// SetTimeout sets the timeout of the request in the milliseconds.
474485
//
475486
// request.Req("http://example.com").

request_options_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ func TestSetProxyChain(t *testing.T) {
280280
a.Equal((*data.Headers)["X-Forward-For"], []string{"127.0.0.1"})
281281
}
282282

283+
func TestSetInsecureSkipVerify(t *testing.T) {
284+
a := assert.New(t)
285+
286+
// TODO: Add HTTPS test cases
287+
_, _, err := ToObject[testResponse](Req("http://localhost:8080").
288+
SetInsecureSkipVerify(true).
289+
Do())
290+
a.NilNow(err)
291+
}
292+
283293
func TestSetTimeoutChain(t *testing.T) {
284294
a := assert.New(t)
285295

0 commit comments

Comments
 (0)