github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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:80", 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 oldenv := os.Getenv("NO_PROXY") 39 defer os.Setenv("NO_PROXY", oldenv) 40 41 no_proxy := "foobar.com, .barbaz.net" 42 os.Setenv("NO_PROXY", no_proxy) 43 44 for _, test := range UseProxyTests { 45 if useProxy(test.host+":80") != test.match { 46 t.Errorf("useProxy(%v) = %v, want %v", test.host, !test.match, test.match) 47 } 48 } 49 } 50 51 var cacheKeysTests = []struct { 52 proxy string 53 scheme string 54 addr string 55 key string 56 }{ 57 {"", "http", "foo.com", "|http|foo.com"}, 58 {"", "https", "foo.com", "|https|foo.com"}, 59 {"http://foo.com", "http", "foo.com", "http://foo.com|http|"}, 60 {"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"}, 61 } 62 63 func TestCacheKeys(t *testing.T) { 64 for _, tt := range cacheKeysTests { 65 var proxy *url.URL 66 if tt.proxy != "" { 67 u, err := url.Parse(tt.proxy) 68 if err != nil { 69 t.Fatal(err) 70 } 71 proxy = u 72 } 73 cm := connectMethod{proxy, tt.scheme, tt.addr} 74 if cm.String() != tt.key { 75 t.Fatalf("{%q, %q, %q} cache key %q; want %q", tt.proxy, tt.scheme, tt.addr, cm.String(), tt.key) 76 } 77 } 78 }