Skip to content

Commit

Permalink
fix(client): return error in SetProxyURL instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Mar 4, 2024
1 parent 557ddca commit 2627b52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
11 changes: 5 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
urlpkg "net/url"
"os"
Expand Down Expand Up @@ -226,21 +227,19 @@ func (c *Client) SetRootCertificateFromString(pem string) *Client {
}

// SetProxyURL sets proxy url in client. It will apply via core to hostclient.
func (c *Client) SetProxyURL(proxyURL string) *Client {
func (c *Client) SetProxyURL(proxyURL string) error {
pURL, err := urlpkg.Parse(proxyURL)
if err != nil {
c.logger.Panicf("client: %v", err)
return c
return fmt.Errorf("client: %w", err)
}

if pURL.Scheme != "http" && pURL.Scheme != "https" {
c.logger.Panicf("client: %v", ErrInvalidProxyURL)
return c
return fmt.Errorf("client: %w", ErrInvalidProxyURL)
}

c.proxyURL = pURL.String()

return c
return nil
}

// RetryConfig returns retry config in client.
Expand Down
19 changes: 11 additions & 8 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,11 @@ func Test_Client_SetProxyURL(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()
client := NewClient().SetDial(dial)
client.SetProxyURL("http://test.com")
_, err := client.Get("http://localhost:3000")
err := client.SetProxyURL("http://test.com")

require.NoError(t, err)

_, err = client.Get("http://localhost:3000")

require.NoError(t, err)
})
Expand All @@ -1559,18 +1562,18 @@ func Test_Client_SetProxyURL(t *testing.T) {
t.Parallel()
client := NewClient()

require.Panics(t, func() {
client.SetProxyURL(":this is not a url")
})
err := client.SetProxyURL(":this is not a url")

require.Error(t, err)
})

t.Run("error", func(t *testing.T) {
t.Parallel()
client := NewClient()

require.Panics(t, func() {
client.SetProxyURL("htgdftp://test.com")
})
err := client.SetProxyURL("htgdftp://test.com")

require.Error(t, err)
})
}

Expand Down

0 comments on commit 2627b52

Please sign in to comment.