-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialer_test.go
52 lines (50 loc) · 1.01 KB
/
dialer_test.go
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
package ara_test
import (
"context"
"github.com/cevatbarisyilmaz/ara"
"net"
"testing"
)
func TestDialer(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
go func() {
conn, err := listener.Accept()
if err != nil {
t.Fatal(err)
}
err = conn.Close()
if err != nil {
t.Error(err)
}
err = listener.Close()
if err != nil {
t.Error(err)
}
}()
resolver := ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}})
dialer := ara.Dialer{
Resolver: resolver,
}
_, port, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
t.Fatal(err)
}
conn, err := dialer.DialContext(context.Background(), "tcp", "example.com:"+port)
if err != nil {
t.Fatal(err)
}
if conn.RemoteAddr().String() != listener.Addr().String() {
t.Fatal("connection failed")
}
conn, err = dialer.DialContext(context.Background(), "tcp", "google.com:80")
if err != nil {
t.Fatal(err)
}
err = conn.Close()
if err != nil {
t.Error(err)
}
}