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