55package proxy
66
77import (
8+ "context"
89 "errors"
910 "net"
1011 "reflect"
@@ -21,10 +22,6 @@ func (r *recordingProxy) Dial(network, addr string) (net.Conn, error) {
2122}
2223
2324func 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-
2825 expectedDef := []string {
2926 "example.com:123" ,
3027 "1.2.3.4:123" ,
@@ -39,17 +36,41 @@ func TestPerHost(t *testing.T) {
3936 "[1000::]:123" ,
4037 }
4138
42- for _ , addr := range expectedDef {
43- perHost .Dial ("tcp" , addr )
44- }
45- for _ , addr := range expectedBypass {
46- perHost .Dial ("tcp" , addr )
47- }
39+ t .Run ("Dial" , func (t * testing.T ) {
40+ var def , bypass recordingProxy
41+ perHost := NewPerHost (& def , & bypass )
42+ perHost .AddFromString ("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16" )
43+ for _ , addr := range expectedDef {
44+ perHost .Dial ("tcp" , addr )
45+ }
46+ for _ , addr := range expectedBypass {
47+ perHost .Dial ("tcp" , addr )
48+ }
4849
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- }
50+ if ! reflect .DeepEqual (expectedDef , def .addrs ) {
51+ t .Errorf ("Hosts which went to the default proxy didn't match. Got %v, want %v" , def .addrs , expectedDef )
52+ }
53+ if ! reflect .DeepEqual (expectedBypass , bypass .addrs ) {
54+ t .Errorf ("Hosts which went to the bypass proxy didn't match. Got %v, want %v" , bypass .addrs , expectedBypass )
55+ }
56+ })
57+
58+ t .Run ("DialContext" , func (t * testing.T ) {
59+ var def , bypass recordingProxy
60+ perHost := NewPerHost (& def , & bypass )
61+ perHost .AddFromString ("localhost,*.zone,127.0.0.1,10.0.0.1/8,1000::/16" )
62+ for _ , addr := range expectedDef {
63+ perHost .DialContext (context .Background (), "tcp" , addr )
64+ }
65+ for _ , addr := range expectedBypass {
66+ perHost .DialContext (context .Background (), "tcp" , addr )
67+ }
68+
69+ if ! reflect .DeepEqual (expectedDef , def .addrs ) {
70+ t .Errorf ("Hosts which went to the default proxy didn't match. Got %v, want %v" , def .addrs , expectedDef )
71+ }
72+ if ! reflect .DeepEqual (expectedBypass , bypass .addrs ) {
73+ t .Errorf ("Hosts which went to the bypass proxy didn't match. Got %v, want %v" , bypass .addrs , expectedBypass )
74+ }
75+ })
5576}
0 commit comments