-
Notifications
You must be signed in to change notification settings - Fork 4
/
dial_test.go
81 lines (74 loc) · 1.85 KB
/
dial_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2014 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 nett
import (
"bytes"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"testing"
)
func TestDialHTTP(t *testing.T) {
b := []byte("OK")
h := func(w http.ResponseWriter, r *http.Request) { w.Write(b) }
s := httptest.NewServer(http.HandlerFunc(h))
defer s.Close()
var d Dialer
c := http.Client{Transport: &http.Transport{Dial: d.Dial}}
resp, err := c.Get(s.URL)
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(body, b) {
t.Fatal("response doesn't match")
}
}
func TestDialMulti(t *testing.T) {
ips, err := lookupIPs("localhost")
if err != nil {
t.Fatalf("lookupIPs failed: %v", err)
}
if len(ips) < 2 || !supportsIPv4 || !supportsIPv6 {
t.Skip("localhost doesn't have a pair of different address family IP addresses")
}
touchServer := func(dss *dualStackServer, ln net.Listener) {
for {
c, err := ln.Accept()
if err != nil {
return
}
c.Close()
}
}
dss, err := newDualStackServer([]streamListener{
{net: "tcp4", addr: "127.0.0.1"},
{net: "tcp6", addr: "[::1]"},
})
if err != nil {
t.Fatalf("newDualStackServer failed: %v", err)
}
defer dss.teardown()
if err := dss.buildup(touchServer); err != nil {
t.Fatalf("dualStackServer.buildup failed: %v", err)
}
d := &Dialer{IPFilter: DualStack} // dial all addresses
for _ = range dss.lns {
if c, err := d.Dial("tcp", "localhost:"+dss.port); err != nil {
t.Errorf("Dial failed: %v", err)
} else {
if addr := c.LocalAddr().(*net.TCPAddr); addr.IP.To4() != nil {
dss.teardownNetwork("tcp4")
} else if addr.IP.To16() != nil && addr.IP.To4() == nil {
dss.teardownNetwork("tcp6")
}
c.Close()
}
}
}