-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer_test.go
132 lines (102 loc) · 3.08 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package websocket
import (
"encoding/base64"
"net/http"
"net/url"
"strings"
"testing"
)
func TestDialerCreateRequestNilHeader(t *testing.T) {
d := &Dialer{Header: nil}
q := d.createRequest(&url.URL{})
if q.Header == nil {
t.Errorf("expected header to be initialized")
}
}
func TestDialerCreateRequestNonNilHeader(t *testing.T) {
h := make(http.Header)
k := "testKey"
v := "testValue"
h.Add(k, v)
d := &Dialer{Header: h}
q := d.createRequest(&url.URL{})
if q.Header.Get(k) != v {
t.Errorf("expected header to be the one provided in dialer instance")
}
}
func TestDialerCreateRequestHostHeader(t *testing.T) {
d := &Dialer{}
type testCase struct {
u *url.URL
v string
}
testCases := []testCase{
{u: &url.URL{Scheme: "ws", Host: "localhost:22"}, v: "localhost"},
{u: &url.URL{Scheme: "wss", Host: "localhost:443"}, v: "localhost"},
{u: &url.URL{Scheme: "ws", Host: "localhost:80"}, v: "localhost:80"},
{u: &url.URL{Scheme: "wss", Host: "localhost:80"}, v: "localhost:80"},
}
for i, c := range testCases {
q := d.createRequest(c.u)
v := q.Header.Get("Host")
if v != c.v {
t.Errorf(`test case %d: expected Host header field value to be "%s", but it is "%s"`, i, c.v, v)
}
}
}
func TestDialerCreateRequestHeaders(t *testing.T) {
d := &Dialer{
SubProtocols: []string{"chat", "v1"},
}
q := d.createRequest(&url.URL{Scheme: "ws", Host: "localhost"})
v := q.Header.Get("Upgrade")
e := "websocket"
if strings.ToLower(v) != e {
t.Errorf(`expected Upgrade header field value to be "%s", but it is "%s"`, v, e)
}
v = q.Header.Get("Connection")
e = "upgrade"
if strings.ToLower(v) != e {
t.Errorf(`expected Connection header field value to be "%s", but it is "%s"`, v, e)
}
v = q.Header.Get("Sec-WebSocket-Version")
e = "13"
if strings.ToLower(v) != e {
t.Errorf(`expected Sec-WebSocket-Version header field value to be "%s", but it is "%s"`, v, e)
}
v = q.Header.Get("Sec-WebSocket-Protocol")
e = strings.Join(d.SubProtocols, ", ")
if strings.ToLower(v) != e {
t.Errorf(`expected Sec-WebSocket-Protocol header field value to be "%s", but it is "%s"`, v, e)
}
l, err := base64.StdEncoding.DecodeString(q.Header.Get("Sec-WebSocket-Key"))
if err != nil {
t.Errorf(`unexpected error returned when decoding Sec-WebSocket-Key %s`, err)
}
if len(l) != 16 {
t.Errorf(`expected Sec-WebSocket-Protocol header field value to be '%d' in length, but it is '%d'`, len(l), 16)
}
}
func TestDialerCreateRequestRequest(t *testing.T) {
d := &Dialer{}
u := &url.URL{
Scheme: "ws",
Host: "localhost:8080",
}
q := d.createRequest(u)
if q.URL != u {
t.Errorf("expected URL instance to be the one provided")
}
if q.Method != "GET" {
t.Errorf(`expected method to be "GET", but it is "%s"`, q.Method)
}
if q.Proto != "HTTP/1.1" {
t.Errorf(`expected http protocol to be "HTTP/1.1", but it is "%s"`, q.Proto)
}
if !q.ProtoAtLeast(1, 1) {
t.Errorf("expected http protocol used to be at least version 1.1, but it is %d.%d", q.ProtoMajor, q.ProtoMinor)
}
if q.Host != u.Host {
t.Errorf(`expected host to be "%s", but it is "%s"`, u.Host, q.Host)
}
}