2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-02-17 21:01:47 +00:00
|
|
|
|
|
|
|
package tshttpproxy
|
|
|
|
|
|
|
|
import (
|
2022-11-13 17:58:47 +00:00
|
|
|
"net/http"
|
2021-02-17 21:01:47 +00:00
|
|
|
"net/url"
|
2022-11-13 17:58:47 +00:00
|
|
|
"os"
|
2021-02-18 05:23:39 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-02-17 21:01:47 +00:00
|
|
|
"testing"
|
2022-11-13 17:58:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/util/must"
|
2021-02-17 21:01:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetAuthHeaderNoResult(t *testing.T) {
|
2021-02-18 05:23:39 +00:00
|
|
|
const proxyURL = "http://127.0.0.1:38274"
|
2021-02-17 21:01:47 +00:00
|
|
|
|
|
|
|
u, err := url.Parse(proxyURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't parse %q: %v", proxyURL, err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 05:23:39 +00:00
|
|
|
got, err := GetAuthHeader(u)
|
2021-02-17 21:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't get auth header value: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 05:23:39 +00:00
|
|
|
if runtime.GOOS == "windows" && strings.HasPrefix(got, "Negotiate") {
|
|
|
|
t.Logf("didn't get empty result, but got acceptable Windows Negotiate header")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != "" {
|
|
|
|
t.Fatalf("GetAuthHeader(%q) = %q; want empty string", proxyURL, got)
|
2021-02-17 21:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAuthHeaderBasicAuth(t *testing.T) {
|
2021-02-18 05:23:39 +00:00
|
|
|
const proxyURL = "http://user:password@127.0.0.1:38274"
|
|
|
|
const want = "Basic dXNlcjpwYXNzd29yZA=="
|
2021-02-17 21:01:47 +00:00
|
|
|
|
|
|
|
u, err := url.Parse(proxyURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't parse %q: %v", proxyURL, err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 05:23:39 +00:00
|
|
|
got, err := GetAuthHeader(u)
|
2021-02-17 21:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't get auth header value: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 05:23:39 +00:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("GetAuthHeader(%q) = %q; want %q", proxyURL, got, want)
|
2021-02-17 21:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-13 17:58:47 +00:00
|
|
|
|
|
|
|
func TestProxyFromEnvironment_setNoProxyUntil(t *testing.T) {
|
|
|
|
const fakeProxyEnv = "10.1.2.3:456"
|
|
|
|
const fakeProxyFull = "http://" + fakeProxyEnv
|
|
|
|
|
|
|
|
defer os.Setenv("HTTPS_PROXY", os.Getenv("HTTPS_PROXY"))
|
|
|
|
os.Setenv("HTTPS_PROXY", fakeProxyEnv)
|
|
|
|
|
|
|
|
req := &http.Request{URL: must.Get(url.Parse("https://example.com/"))}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
switch i {
|
|
|
|
case 1:
|
|
|
|
setNoProxyUntil(time.Minute)
|
|
|
|
case 2:
|
|
|
|
setNoProxyUntil(0)
|
|
|
|
}
|
|
|
|
got, err := ProxyFromEnvironment(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("[%d] ProxyFromEnvironment: %v", i, err)
|
|
|
|
}
|
|
|
|
if got == nil || got.String() != fakeProxyFull {
|
|
|
|
t.Errorf("[%d] Got proxy %v; want %v", i, got, fakeProxyFull)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|