forked from docker/go-connections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets_test.go
113 lines (105 loc) · 2.24 KB
/
sockets_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package sockets
import (
"net/http"
"net/url"
"os"
"testing"
)
var (
httpProxy = "http://proxy.example.com"
httpsProxy = "https://proxy.example.com"
)
func TestConfigureTransportProxy(t *testing.T) {
// roughly based on defaultHTTPClient in the docker client
u := &url.URL{
Scheme: "tcp",
Host: "docker.acme.example.com",
}
transport := new(http.Transport)
err := ConfigureTransport(transport, u.Scheme, u.Host)
if err != nil {
t.Fatal(err)
}
if err := os.Setenv("HTTP_PROXY", httpProxy); err != nil {
t.Fatal(err)
}
if err := os.Setenv("HTTPS_PROXY", httpsProxy); err != nil {
t.Fatal(err)
}
defer func() {
_ = os.Unsetenv("HTTP_PROXY")
_ = os.Unsetenv("HTTPS_PROXY")
}()
request, err := http.NewRequest(http.MethodGet, "tcp://docker.acme.example.com:2376", nil)
if err != nil {
t.Fatal(err)
}
proxyURL, err := transport.Proxy(request)
if err != nil {
t.Fatal(err)
}
if proxyURL.String() != httpProxy {
t.Fatalf("expected %s, got %s", httpProxy, proxyURL)
}
}
func TestTCPProxyFromEnvironment(t *testing.T) {
if err := os.Setenv("HTTP_PROXY", httpProxy); err != nil {
t.Fatal(err)
}
if err := os.Setenv("HTTPS_PROXY", httpsProxy); err != nil {
t.Fatal(err)
}
defer func() {
_ = os.Unsetenv("HTTP_PROXY")
_ = os.Unsetenv("HTTPS_PROXY")
}()
tests := []struct {
url string
expected *string
}{
{
url: "example.com",
expected: nil,
},
{
url: "ftp://example.com",
expected: nil,
},
{
url: "unix:///var/run/docker.sock",
expected: nil,
},
{
url: "tcp://example.com:2376",
expected: &httpProxy,
},
{
url: "http://example.com:2375",
expected: &httpProxy,
},
{
url: "https://example.com:2376",
expected: &httpsProxy,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.url, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, tc.url, nil)
if err != nil {
t.Fatal(err)
}
proxyURL, err := TCPProxyFromEnvironment(request)
if err != nil {
t.Fatal(err)
}
if tc.expected == nil {
if proxyURL != nil {
t.Fatalf("expected no proxy, got %s", proxyURL)
}
} else if proxyURL.String() != *tc.expected {
t.Fatalf("expected %s, got %s", *tc.expected, proxyURL)
}
})
}
}