Newer
Older
package websocket
import (
"net/http/httptest"
"strings"
"testing"
)
func TestAccept(t *testing.T) {
t.Parallel()
t.Run("badClientHandshake", func(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
if err == nil {
t.Fatalf("unexpected error value: %v", err)
}
})
t.Run("requireHttpHijacker", func(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Connection", "Upgrade")
r.Header.Set("Upgrade", "websocket")
r.Header.Set("Sec-WebSocket-Version", "13")
r.Header.Set("Sec-WebSocket-Key", "meow123")
if err == nil || !strings.Contains(err.Error(), "http.Hijacker") {
t.Fatalf("unexpected error value: %v", err)
}
})
}
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
func Test_verifyClientHandshake(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
method string
h map[string]string
success bool
}{
{
name: "badConnection",
h: map[string]string{
"Connection": "notUpgrade",
},
},
{
name: "badUpgrade",
h: map[string]string{
"Connection": "Upgrade",
"Upgrade": "notWebSocket",
},
},
{
name: "badMethod",
method: "POST",
h: map[string]string{
"Connection": "Upgrade",
"Upgrade": "websocket",
},
},
{
name: "badWebSocketVersion",
h: map[string]string{
"Connection": "Upgrade",
"Upgrade": "websocket",
"Sec-WebSocket-Version": "14",
},
},
{
name: "badWebSocketKey",
h: map[string]string{
"Connection": "Upgrade",
"Upgrade": "websocket",
"Sec-WebSocket-Version": "13",
"Sec-WebSocket-Key": "",
},
},
{
name: "success",
h: map[string]string{
"Connection": "Upgrade",
"Upgrade": "websocket",
"Sec-WebSocket-Version": "13",
"Sec-WebSocket-Key": "meow123",
},
success: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
r := httptest.NewRequest(tc.method, "/", nil)
for k, v := range tc.h {
r.Header.Set(k, v)
}
err := verifyClientRequest(w, r)
if (err == nil) != tc.success {
t.Fatalf("unexpected error value: %+v", err)
}
})
}
}
func Test_selectSubprotocol(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
clientProtocols []string
serverProtocols []string
negotiated string
}{
{
name: "empty",
clientProtocols: nil,
serverProtocols: nil,
negotiated: "",
},
{
name: "basic",
clientProtocols: []string{"echo", "echo2"},
serverProtocols: []string{"echo2", "echo"},
negotiated: "echo2",
},
{
name: "none",
clientProtocols: []string{"echo", "echo3"},
serverProtocols: []string{"echo2", "echo4"},
negotiated: "",
},
{
name: "fallback",
clientProtocols: []string{"echo", "echo3"},
serverProtocols: []string{"echo2", "echo3"},
negotiated: "echo3",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Sec-WebSocket-Protocol", strings.Join(tc.clientProtocols, ","))
negotiated := selectSubprotocol(r, tc.serverProtocols)
if tc.negotiated != negotiated {
t.Fatalf("expected %q but got %q", tc.negotiated, negotiated)
}
})
}
}
func Test_authenticateOrigin(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
origin string
host string
success bool
},
{
name: "invalid",
origin: "$#)(*)$#@*$(#@*$)#@*%)#(@*%)#(@%#@$#@$#$#@$#@}{}{}",
name: "unauthorized",
origin: "https://example.com",
host: "example1.com",
success: false,
name: "authorized",
origin: "https://example.com",
host: "example.com",
success: true,
name: "authorizedCaseInsensitive",
origin: "https://examplE.com",
host: "example.com",
success: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
r := httptest.NewRequest("GET", "http://"+tc.host+"/", nil)
err := authenticateOrigin(r)
if (err == nil) != tc.success {
t.Fatalf("unexpected error value: %+v", err)
}
})
}
}