golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/net/http/proxy_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http 6 7 import ( 8 "net/url" 9 "os" 10 "testing" 11 ) 12 13 // TODO(mattn): 14 // test ProxyAuth 15 16 var UseProxyTests = []struct { 17 host string 18 match bool 19 }{ 20 // Never proxy localhost: 21 {"localhost", false}, 22 {"127.0.0.1", false}, 23 {"127.0.0.2", false}, 24 {"[::1]", false}, 25 {"[::2]", true}, // not a loopback address 26 27 {"barbaz.net", false}, // match as .barbaz.net 28 {"foobar.com", false}, // have a port but match 29 {"foofoobar.com", true}, // not match as a part of foobar.com 30 {"baz.com", true}, // not match as a part of barbaz.com 31 {"localhost.net", true}, // not match as suffix of address 32 {"local.localhost", true}, // not match as prefix as address 33 {"barbarbaz.net", true}, // not match because NO_PROXY have a '.' 34 {"www.foobar.com", false}, // match because NO_PROXY includes "foobar.com" 35 } 36 37 func TestUseProxy(t *testing.T) { 38 ResetProxyEnv() 39 os.Setenv("NO_PROXY", "foobar.com, .barbaz.net") 40 for _, test := range UseProxyTests { 41 if useProxy(test.host+":80") != test.match { 42 t.Errorf("useProxy(%v) = %v, want %v", test.host, !test.match, test.match) 43 } 44 } 45 } 46 47 var cacheKeysTests = []struct { 48 proxy string 49 scheme string 50 addr string 51 key string 52 }{ 53 {"", "http", "foo.com", "|http|foo.com"}, 54 {"", "https", "foo.com", "|https|foo.com"}, 55 {"http://foo.com", "http", "foo.com", "http://foo.com|http|"}, 56 {"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"}, 57 } 58 59 func TestCacheKeys(t *testing.T) { 60 for _, tt := range cacheKeysTests { 61 var proxy *url.URL 62 if tt.proxy != "" { 63 u, err := url.Parse(tt.proxy) 64 if err != nil { 65 t.Fatal(err) 66 } 67 proxy = u 68 } 69 cm := connectMethod{proxy, tt.scheme, tt.addr} 70 if got := cm.key().String(); got != tt.key { 71 t.Fatalf("{%q, %q, %q} cache key = %q; want %q", tt.proxy, tt.scheme, tt.addr, got, tt.key) 72 } 73 } 74 } 75 76 func ResetProxyEnv() { 77 for _, v := range []string{"HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy"} { 78 os.Unsetenv(v) 79 } 80 ResetCachedEnvironment() 81 } 82 83 func TestInvalidNoProxy(t *testing.T) { 84 ResetProxyEnv() 85 os.Setenv("NO_PROXY", ":1") 86 useProxy("example.com:80") // should not panic 87 }