/http/proxy_test.go

http://github.com/petar/GoHTTP · Go · 48 lines · 34 code · 8 blank · 6 comment · 4 complexity · 19dd20a2bb5836acd29bb9243cb67e00 MD5 · raw file

  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. package http
  5. import (
  6. "os"
  7. "testing"
  8. )
  9. // TODO(mattn):
  10. // test ProxyAuth
  11. var UseProxyTests = []struct {
  12. host string
  13. match bool
  14. }{
  15. // Never proxy localhost:
  16. {"localhost:80", false},
  17. {"127.0.0.1", false},
  18. {"127.0.0.2", false},
  19. {"[::1]", false},
  20. {"[::2]", true}, // not a loopback address
  21. {"barbaz.net", false}, // match as .barbaz.net
  22. {"foobar.com", false}, // have a port but match
  23. {"foofoobar.com", true}, // not match as a part of foobar.com
  24. {"baz.com", true}, // not match as a part of barbaz.com
  25. {"localhost.net", true}, // not match as suffix of address
  26. {"local.localhost", true}, // not match as prefix as address
  27. {"barbarbaz.net", true}, // not match because NO_PROXY have a '.'
  28. {"www.foobar.com", true}, // not match because NO_PROXY is not .foobar.com
  29. }
  30. func TestUseProxy(t *testing.T) {
  31. oldenv := os.Getenv("NO_PROXY")
  32. defer os.Setenv("NO_PROXY", oldenv)
  33. no_proxy := "foobar.com, .barbaz.net"
  34. os.Setenv("NO_PROXY", no_proxy)
  35. for _, test := range UseProxyTests {
  36. if useProxy(test.host+":80") != test.match {
  37. t.Errorf("useProxy(%v) = %v, want %v", test.host, !test.match, test.match)
  38. }
  39. }
  40. }