forked from stripe/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
https_test.go
67 lines (59 loc) · 2.2 KB
/
https_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
package goproxy
import (
"net/url"
"os"
"testing"
)
var proxytests = map[string]struct {
noProxy string
envHttpsProxy string
customHttpsProxy string
url string
expectProxy string
}{
"do not proxy without a proxy configured": {"", "", "", "https://foo.bar/baz", ""},
"proxy with a proxy configured": {"", "daproxy", "", "https://foo.bar/baz", "http://daproxy:http"},
"proxy without a scheme": {"", "daproxy", "", "//foo.bar/baz", "http://daproxy:http"},
"proxy with a proxy configured with a port": {"", "http://daproxy:123", "", "https://foo.bar/baz", "http://daproxy:123"},
"proxy with an https proxy configured": {"", "https://daproxy", "", "https://foo.bar/baz", "https://daproxy:https"},
"proxy with a non-matching no_proxy": {"other.bar", "daproxy", "", "https://foo.bar/baz", "http://daproxy:http"},
"do not proxy with a full no_proxy match": {"foo.bar", "daproxy", "", "https://foo.bar/baz", ""},
"do not proxy with a suffix no_proxy match": {".bar", "daproxy", "", "https://foo.bar/baz", ""},
"proxy with an custom https proxy": {"", "https://daproxy", "https://customproxy", "https://foo.bar/baz", "https://customproxy:https"},
}
var envKeys = []string{"no_proxy", "http_proxy", "https_proxy", "NO_PROXY", "HTTP_PROXY", "HTTPS_PROXY"}
func TestHttpsProxyAddr(t *testing.T) {
for _, k := range envKeys {
v, ok := os.LookupEnv(k)
if ok {
defer func() {
os.Setenv(k, v)
}()
os.Unsetenv(k)
} else {
defer func() {
os.Unsetenv(k)
}()
}
}
os.Setenv("http_proxy", "should.never.use.this")
for name, spec := range proxytests {
t.Run(name, func(t *testing.T) {
os.Setenv("no_proxy", spec.noProxy)
os.Setenv("https_proxy", spec.envHttpsProxy)
url, err := url.Parse(spec.url)
if err != nil {
t.Fatalf("bad test input URL %s: %v", spec.url, err)
}
actual, err := httpsProxyAddr(url, spec.customHttpsProxy)
if err != nil {
t.Fatalf("unexpected error parsing proxy from env: %#v", err)
}
if actual != spec.expectProxy {
t.Errorf("expected proxy url '%s' but got '%s'", spec.expectProxy, actual)
}
os.Unsetenv("no_proxy")
os.Unsetenv("https_proxy")
})
}
}