Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Export transport method to return current transport from the client #605

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func (c *Client) SetRetryResetReaders(b bool) *Client {
//
// Note: This method overwrites existing `TLSClientConfig`.
func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
transport, err := c.transport()
transport, err := c.Transport()
if err != nil {
c.log.Errorf("%v", err)
return c
Expand All @@ -809,7 +809,7 @@ func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
//
// Refer to godoc `http.ProxyFromEnvironment`.
func (c *Client) SetProxy(proxyURL string) *Client {
transport, err := c.transport()
transport, err := c.Transport()
if err != nil {
c.log.Errorf("%v", err)
return c
Expand All @@ -830,7 +830,7 @@ func (c *Client) SetProxy(proxyURL string) *Client {
//
// client.RemoveProxy()
func (c *Client) RemoveProxy() *Client {
transport, err := c.transport()
transport, err := c.Transport()
if err != nil {
c.log.Errorf("%v", err)
return c
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func (c *Client) execute(req *Request) (*Response, error) {

// getting TLS client config if not exists then create one
func (c *Client) tlsConfig() (*tls.Config, error) {
transport, err := c.transport()
transport, err := c.Transport()
if err != nil {
return nil, err
}
Expand All @@ -1152,7 +1152,7 @@ func (c *Client) tlsConfig() (*tls.Config, error) {

// Transport method returns `*http.Transport` currently in use or error
// in case currently used `transport` is not a `*http.Transport`.
func (c *Client) transport() (*http.Transport, error) {
func (c *Client) Transport() (*http.Transport, error) {
if transport, ok := c.httpClient.Transport.(*http.Transport); ok {
return transport, nil
}
Expand Down
16 changes: 8 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestClientSetCertificates(t *testing.T) {
client := dc()
client.SetCertificates(tls.Certificate{})

transport, err := client.transport()
transport, err := client.Transport()

assertNil(t, err)
assertEqual(t, 1, len(transport.TLSClientConfig.Certificates))
Expand All @@ -258,7 +258,7 @@ func TestClientSetRootCertificate(t *testing.T) {
client := dc()
client.SetRootCertificate(filepath.Join(getTestDataPath(), "sample-root.pem"))

transport, err := client.transport()
transport, err := client.Transport()

assertNil(t, err)
assertNotNil(t, transport.TLSClientConfig.RootCAs)
Expand All @@ -268,7 +268,7 @@ func TestClientSetRootCertificateNotExists(t *testing.T) {
client := dc()
client.SetRootCertificate(filepath.Join(getTestDataPath(), "not-exists-sample-root.pem"))

transport, err := client.transport()
transport, err := client.Transport()

assertNil(t, err)
assertNil(t, transport.TLSClientConfig)
Expand All @@ -281,7 +281,7 @@ func TestClientSetRootCertificateFromString(t *testing.T) {

client.SetRootCertificateFromString(string(rootPemData))

transport, err := client.transport()
transport, err := client.Transport()

assertNil(t, err)
assertNotNil(t, transport.TLSClientConfig.RootCAs)
Expand All @@ -295,7 +295,7 @@ func TestClientSetRootCertificateFromStringErrorTls(t *testing.T) {
assertNil(t, err)
rt := &CustomRoundTripper{}
client.SetTransport(rt)
transport, err := client.transport()
transport, err := client.Transport()

client.SetRootCertificateFromString(string(rootPemData))

Expand Down Expand Up @@ -349,7 +349,7 @@ func TestClientSetTransport(t *testing.T) {
},
}
client.SetTransport(transport)
transportInUse, err := client.transport()
transportInUse, err := client.Transport()

assertNil(t, err)
assertEqual(t, true, transport == transportInUse)
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestClientOptions(t *testing.T) {
}

client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
transport, transportErr := client.transport()
transport, transportErr := client.Transport()

assertNil(t, transportErr)
assertEqual(t, true, transport.TLSClientConfig.InsecureSkipVerify)
Expand Down Expand Up @@ -562,7 +562,7 @@ func TestClientRoundTripper(t *testing.T) {
rt := &CustomRoundTripper{}
c.SetTransport(rt)

ct, err := c.transport()
ct, err := c.Transport()
assertNotNil(t, err)
assertNil(t, ct)
assertEqual(t, "current transport is not an *http.Transport instance", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ func TestRawFileUploadByBody(t *testing.T) {
func TestProxySetting(t *testing.T) {
c := dc()

transport, err := c.transport()
transport, err := c.Transport()

assertNil(t, err)

Expand Down