mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix(netutil): Add proxy IP to send request (#210)
* fix(netutil): Add proxy ip to send request * fix(netutil): Add proxy IP to send request --------- Co-authored-by: 杨崟 <yangyin@addcn.com>
This commit is contained in:
@@ -108,6 +108,7 @@ type HttpClientConfig struct {
|
|||||||
HandshakeTimeout time.Duration
|
HandshakeTimeout time.Duration
|
||||||
ResponseTimeout time.Duration
|
ResponseTimeout time.Duration
|
||||||
Verbose bool
|
Verbose bool
|
||||||
|
Proxy *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultHttpClientConfig defalut client config.
|
// defaultHttpClientConfig defalut client config.
|
||||||
@@ -163,6 +164,11 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
|||||||
client.TLS = config.TLSConfig
|
client.TLS = config.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Proxy != nil {
|
||||||
|
transport := client.Client.Transport.(*http.Transport)
|
||||||
|
transport.Proxy = http.ProxyURL(config.Proxy)
|
||||||
|
}
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
@@ -361,3 +362,23 @@ func TestSendRequestWithFilePath(t *testing.T) {
|
|||||||
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProxy(t *testing.T) {
|
||||||
|
config := &HttpClientConfig{
|
||||||
|
HandshakeTimeout: 20 * time.Second,
|
||||||
|
ResponseTimeout: 40 * time.Second,
|
||||||
|
// Use the proxy ip to add it here
|
||||||
|
//Proxy: &url.URL{
|
||||||
|
// Scheme: "http",
|
||||||
|
// Host: "46.17.63.166:18888",
|
||||||
|
//},
|
||||||
|
}
|
||||||
|
httpClient := NewHttpClientWithConfig(config)
|
||||||
|
resp, err := httpClient.Get("https://www.ipplus360.com/getLocation")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user