Skip to content

Commit

Permalink
add client#UnSetProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ddo committed Aug 1, 2019
1 parent 8c9df21 commit 67656c5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
24 changes: 24 additions & 0 deletions client/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import (
)

// SetProxy sets client proxy by url string
// if url is empty, no proxy is used
func (c *Client) SetProxy(rawURL string) (err error) {
log.Info("url:", rawURL)

if rawURL == "" {
c.UnSetProxy()
return
}

u, err := url.Parse(rawURL)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -48,3 +54,21 @@ func (c *Client) SetProxy(rawURL string) (err error) {
log.Info("DONE:", u)
return
}

// UnSetProxy unsets client proxy
func (c *Client) UnSetProxy() {
log.Info()

client := c.httpClient
if client.Transport == nil {
return
}

transport, ok := client.Transport.(*http.Transport)
if !ok {
return
}

transport.Proxy = nil
return
}
36 changes: 30 additions & 6 deletions client/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ import (
"testing"
)

const (
test_proxy_url = "https://127.0.0.1:8888"
)

func TestSetProxy(t *testing.T) {
client := New(nil)
if client == nil {
t.Error()
return
}

err := client.SetProxy("https://127.0.0.1:8888")
err := client.SetProxy(test_proxy_url)
if err != nil {
t.Error()
return
}

proxy, err := client.httpClient.Transport.(*http.Transport).Proxy(nil)
transport := client.httpClient.Transport.(*http.Transport)
proxy, err := transport.Proxy(nil)
if err != nil {
t.Error()
return
}
if proxy.String() != "https://127.0.0.1:8888" {
if proxy.String() != test_proxy_url {
t.Error()
return
}
Expand All @@ -45,19 +50,38 @@ func TestSetProxyInvalidURL(t *testing.T) {

func TestSetProxyFromNew(t *testing.T) {
client := New(&Option{
Proxy: "https://127.0.0.1:8888",
Proxy: test_proxy_url,
})
if client == nil {
t.Error()
return
}

proxy, err := client.httpClient.Transport.(*http.Transport).Proxy(nil)
transport := client.httpClient.Transport.(*http.Transport)
proxy, err := transport.Proxy(nil)
if err != nil {
t.Error()
return
}
if proxy.String() != "https://127.0.0.1:8888" {
if proxy.String() != test_proxy_url {
t.Error()
return
}
}

func TestUnSetProxy(t *testing.T) {
client := New(&Option{
Proxy: test_proxy_url,
})
if client == nil {
t.Error()
return
}

client.UnSetProxy()

transport := client.httpClient.Transport.(*http.Transport)
if transport.Proxy != nil {
t.Error()
return
}
Expand Down

0 comments on commit 67656c5

Please sign in to comment.