File: per_host_test.go

package info (click to toggle)
golang-golang-x-net-dev 1%3A0.0%2Bgit20181201.351d144%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports, buster-backports-sloppy, experimental
  • size: 5,676 kB
  • sloc: makefile: 53; asm: 18
file content (55 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2011 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 proxy

import (
	"errors"
	"net"
	"reflect"
	"testing"
)

type recordingProxy struct {
	addrs []string
}

func (r *recordingProxy) Dial(network, addr string) (net.Conn, error) {
	r.addrs = append(r.addrs, addr)
	return nil, errors.New("recordingProxy")
}

func TestPerHost(t *testing.T) {
	var def, bypass recordingProxy
	perHost := NewPerHost(&def, &bypass)
	perHost.AddFromString("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16")

	expectedDef := []string{
		"example.com:123",
		"1.2.3.4:123",
		"[1001::]:123",
	}
	expectedBypass := []string{
		"localhost:123",
		"zone:123",
		"foo.zone:123",
		"127.0.0.1:123",
		"10.1.2.3:123",
		"[1000::]:123",
	}

	for _, addr := range expectedDef {
		perHost.Dial("tcp", addr)
	}
	for _, addr := range expectedBypass {
		perHost.Dial("tcp", addr)
	}

	if !reflect.DeepEqual(expectedDef, def.addrs) {
		t.Errorf("Hosts which went to the default proxy didn't match. Got %v, want %v", def.addrs, expectedDef)
	}
	if !reflect.DeepEqual(expectedBypass, bypass.addrs) {
		t.Errorf("Hosts which went to the bypass proxy didn't match. Got %v, want %v", bypass.addrs, expectedBypass)
	}
}