Retro68/gcc/libgo/go/net/http/proxy_test.go

82 lines
2.1 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
2014-09-21 17:33:12 +00:00
"net/url"
2012-03-27 23:13:14 +00:00
"os"
"testing"
)
// TODO(mattn):
// test ProxyAuth
var UseProxyTests = []struct {
host string
match bool
}{
// Never proxy localhost:
2017-04-10 11:32:00 +00:00
{"localhost", false},
2012-03-27 23:13:14 +00:00
{"127.0.0.1", false},
{"127.0.0.2", false},
{"[::1]", false},
{"[::2]", true}, // not a loopback address
{"barbaz.net", false}, // match as .barbaz.net
2014-09-21 17:33:12 +00:00
{"foobar.com", false}, // have a port but match
2012-03-27 23:13:14 +00:00
{"foofoobar.com", true}, // not match as a part of foobar.com
{"baz.com", true}, // not match as a part of barbaz.com
{"localhost.net", true}, // not match as suffix of address
{"local.localhost", true}, // not match as prefix as address
{"barbarbaz.net", true}, // not match because NO_PROXY have a '.'
2014-09-21 17:33:12 +00:00
{"www.foobar.com", false}, // match because NO_PROXY includes "foobar.com"
2012-03-27 23:13:14 +00:00
}
func TestUseProxy(t *testing.T) {
2015-08-28 15:33:40 +00:00
ResetProxyEnv()
os.Setenv("NO_PROXY", "foobar.com, .barbaz.net")
2012-03-27 23:13:14 +00:00
for _, test := range UseProxyTests {
if useProxy(test.host+":80") != test.match {
t.Errorf("useProxy(%v) = %v, want %v", test.host, !test.match, test.match)
}
}
}
2014-09-21 17:33:12 +00:00
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}
2015-08-28 15:33:40 +00:00
if got := cm.key().String(); got != tt.key {
t.Fatalf("{%q, %q, %q} cache key = %q; want %q", tt.proxy, tt.scheme, tt.addr, got, tt.key)
2014-09-21 17:33:12 +00:00
}
}
}
2015-08-28 15:33:40 +00:00
func ResetProxyEnv() {
for _, v := range []string{"HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy"} {
os.Setenv(v, "")
}
ResetCachedEnvironment()
}