per_host_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2011 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 proxy
  5. import (
  6. "errors"
  7. "net"
  8. "reflect"
  9. "testing"
  10. )
  11. type recordingProxy struct {
  12. addrs []string
  13. }
  14. func (r *recordingProxy) Dial(network, addr string) (net.Conn, error) {
  15. r.addrs = append(r.addrs, addr)
  16. return nil, errors.New("recordingProxy")
  17. }
  18. func TestPerHost(t *testing.T) {
  19. var def, bypass recordingProxy
  20. perHost := NewPerHost(&def, &bypass)
  21. perHost.AddFromString("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16")
  22. expectedDef := []string{
  23. "example.com:123",
  24. "1.2.3.4:123",
  25. "[1001::]:123",
  26. }
  27. expectedBypass := []string{
  28. "localhost:123",
  29. "zone:123",
  30. "foo.zone:123",
  31. "127.0.0.1:123",
  32. "10.1.2.3:123",
  33. "[1000::]:123",
  34. }
  35. for _, addr := range expectedDef {
  36. perHost.Dial("tcp", addr)
  37. }
  38. for _, addr := range expectedBypass {
  39. perHost.Dial("tcp", addr)
  40. }
  41. if !reflect.DeepEqual(expectedDef, def.addrs) {
  42. t.Errorf("Hosts which went to the default proxy didn't match. Got %v, want %v", def.addrs, expectedDef)
  43. }
  44. if !reflect.DeepEqual(expectedBypass, bypass.addrs) {
  45. t.Errorf("Hosts which went to the bypass proxy didn't match. Got %v, want %v", bypass.addrs, expectedBypass)
  46. }
  47. }