package http
import (
+ "net/url"
"os"
"testing"
)
}
}
}
+
+var cacheKeysTests = []struct {
+ proxy string
+ scheme string
+ addr string
+ key string
+}{
+ {"", "http", "foo.com", "|http|foo.com"},
+ {"", "https", "foo.com", "|https|foo.com"},
+ {"http://foo.com", "http", "foo.com", "http://foo.com|http|"},
+ {"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"},
+}
+
+func TestCacheKeys(t *testing.T) {
+ for _, tt := range cacheKeysTests {
+ var proxy *url.URL
+ if tt.proxy != "" {
+ u, err := url.Parse(tt.proxy)
+ if err != nil {
+ t.Fatal(err)
+ }
+ proxy = u
+ }
+ cm := connectMethod{proxy, tt.scheme, tt.addr}
+ if cm.String() != tt.key {
+ t.Fatalf("{%q, %q, %q} cache key %q; want %q", tt.proxy, tt.scheme, tt.addr, cm.String(), tt.key)
+ }
+ }
+}
func (ck *connectMethod) String() string {
proxyStr := ""
+ targetAddr := ck.targetAddr
if ck.proxyURL != nil {
proxyStr = ck.proxyURL.String()
+ if ck.targetScheme == "http" {
+ targetAddr = ""
+ }
}
- return strings.Join([]string{proxyStr, ck.targetScheme, ck.targetAddr}, "|")
+ return strings.Join([]string{proxyStr, ck.targetScheme, targetAddr}, "|")
}
// addr returns the first hop "host:port" to which we need to TCP connect.