package netproxy import ( "net/http" "testing" ) func TestParseWindowsProxyServer(t *testing.T) { cases := []struct { server string scheme string want string }{ {"127.0.0.1:7890", "https", "http://127.0.0.1:7890"}, {"http://127.0.0.1:7890", "https", "http://127.0.0.1:7890"}, {"socks5://127.0.0.1:7891", "https", "socks5://127.0.0.1:7891"}, {"http=127.0.0.1:7890;https=127.0.0.1:7891", "https", "http://127.0.0.1:7891"}, {"http=127.0.0.1:7890;https=127.0.0.1:7891", "http", "http://127.0.0.1:7890"}, {"socks=127.0.0.1:10808", "https", "socks5://127.0.0.1:10808"}, {"http=127.0.0.1:8080", "https", "http://127.0.0.1:8080"}, {"", "https", ""}, } for _, tc := range cases { u, err := parseWindowsProxyServer(tc.server, tc.scheme) if err != nil { t.Fatalf("parseWindowsProxyServer(%q, %q) err = %v", tc.server, tc.scheme, err) } got := "" if u != nil { got = u.String() } if got != tc.want { t.Errorf("parseWindowsProxyServer(%q, %q) = %q, want %q", tc.server, tc.scheme, got, tc.want) } } } func TestShouldBypassWindowsProxy(t *testing.T) { override := "localhost;127.*;10.*;*.local;" for host, want := range map[string]bool{ "localhost": true, "127.0.0.1": true, "10.0.0.5": true, "nas.local": true, "intranet": true, "api.github.com": false, "github.com": false, } { if got := shouldBypassWindowsProxy(override, host); got != want { t.Errorf("shouldBypassWindowsProxy(%q, %q) = %v, want %v", override, host, got, want) } } } func TestProxyFromEnvTakesPrecedence(t *testing.T) { t.Setenv("HTTPS_PROXY", "http://127.0.0.1:19999") req, _ := http.NewRequest(http.MethodGet, "https://api.github.com/repos/Nwflower/reims-go/releases", nil) u, err := ProxyFromEnvOrSystem(req) if err != nil { t.Fatal(err) } if u == nil || u.String() != "http://127.0.0.1:19999" { t.Fatalf("got %v, want http://127.0.0.1:19999", u) } }