-
Notifications
You must be signed in to change notification settings - Fork 11
/
dialer_test.go
54 lines (45 loc) · 1.06 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
53
54
package letsdane
import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDialTLS(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
wr.Write([]byte("foo"))
}))
var (
addrs = &addrList{}
ip string
d = newDialer()
)
addr := strings.TrimPrefix(srv.URL, "https://")
ip, addrs.Port, _ = net.SplitHostPort(addr)
addrs.Host = "example.com"
addrs.IPs = []net.IP{net.ParseIP("255.255.255.255"), net.ParseIP(ip)}
tlsa := newTLSA(3, 1, 1, srv.Certificate())
config := newTLSConfig("", tlsa, false)
conn, err := d.dialTLSContext(context.Background(), "tcp", addrs, config)
if err != nil {
t.Fatal(err)
}
conn.Write([]byte("GET / HTTP/1.1\nHost:example.org\n\r\n\r"))
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
c := string(body)
if c != "foo" {
t.Fatalf("body = '%s', wanted 'foo'", c)
}
}