Skip to content

Commit f78ed9f

Browse files
KSDaemonjaitaiwan
authored andcommitted
Added tests for subprotocol selection
1 parent 17f4072 commit f78ed9f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

server_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ func TestIsWebSocketUpgrade(t *testing.T) {
5656
}
5757
}
5858

59+
func TestSubProtocolSelection(t *testing.T) {
60+
upgrader := Upgrader{
61+
Subprotocols: []string{"foo", "bar", "baz"},
62+
}
63+
64+
r := http.Request{Header: http.Header{"Sec-Websocket-Protocol": {"foo", "bar"}}}
65+
s := upgrader.selectSubprotocol(&r, nil)
66+
if s != "foo" {
67+
t.Errorf("Upgrader.selectSubprotocol returned %v, want %v", s, "foo")
68+
}
69+
70+
r = http.Request{Header: http.Header{"Sec-Websocket-Protocol": {"bar", "foo"}}}
71+
s = upgrader.selectSubprotocol(&r, nil)
72+
if s != "bar" {
73+
t.Errorf("Upgrader.selectSubprotocol returned %v, want %v", s, "bar")
74+
}
75+
76+
r = http.Request{Header: http.Header{"Sec-Websocket-Protocol": {"baz"}}}
77+
s = upgrader.selectSubprotocol(&r, nil)
78+
if s != "baz" {
79+
t.Errorf("Upgrader.selectSubprotocol returned %v, want %v", s, "baz")
80+
}
81+
82+
r = http.Request{Header: http.Header{"Sec-Websocket-Protocol": {"quux"}}}
83+
s = upgrader.selectSubprotocol(&r, nil)
84+
if s != "" {
85+
t.Errorf("Upgrader.selectSubprotocol returned %v, want %v", s, "empty string")
86+
}
87+
}
88+
5989
var checkSameOriginTests = []struct {
6090
ok bool
6191
r *http.Request

0 commit comments

Comments
 (0)