1
2
3
4
5 package websocket
6
7 import (
8 "net/http"
9 "reflect"
10 "testing"
11 )
12
13 var equalASCIIFoldTests = []struct {
14 t, s string
15 eq bool
16 }{
17 {"WebSocket", "websocket", true},
18 {"websocket", "WebSocket", true},
19 {"Öyster", "öyster", false},
20 {"WebSocket", "WetSocket", false},
21 }
22
23 func TestEqualASCIIFold(t *testing.T) {
24 for _, tt := range equalASCIIFoldTests {
25 eq := equalASCIIFold(tt.s, tt.t)
26 if eq != tt.eq {
27 t.Errorf("equalASCIIFold(%q, %q) = %v, want %v", tt.s, tt.t, eq, tt.eq)
28 }
29 }
30 }
31
32 var tokenListContainsValueTests = []struct {
33 value string
34 ok bool
35 }{
36 {"WebSocket", true},
37 {"WEBSOCKET", true},
38 {"websocket", true},
39 {"websockets", false},
40 {"x websocket", false},
41 {"websocket x", false},
42 {"other,websocket,more", true},
43 {"other, websocket, more", true},
44 }
45
46 func TestTokenListContainsValue(t *testing.T) {
47 for _, tt := range tokenListContainsValueTests {
48 h := http.Header{"Upgrade": {tt.value}}
49 ok := tokenListContainsValue(h, "Upgrade", "websocket")
50 if ok != tt.ok {
51 t.Errorf("tokenListContainsValue(h, n, %q) = %v, want %v", tt.value, ok, tt.ok)
52 }
53 }
54 }
55
56 var isValidChallengeKeyTests = []struct {
57 key string
58 ok bool
59 }{
60 {"dGhlIHNhbXBsZSBub25jZQ==", true},
61 {"", false},
62 {"InvalidKey", false},
63 {"WHQ4eXhscUtKYjBvOGN3WEdtOEQ=", false},
64 }
65
66 func TestIsValidChallengeKey(t *testing.T) {
67 for _, tt := range isValidChallengeKeyTests {
68 ok := isValidChallengeKey(tt.key)
69 if ok != tt.ok {
70 t.Errorf("isValidChallengeKey returns %v, want %v", ok, tt.ok)
71 }
72 }
73 }
74
75 var parseExtensionTests = []struct {
76 value string
77 extensions []map[string]string
78 }{
79 {`foo`, []map[string]string{{"": "foo"}}},
80 {`foo, bar; baz=2`, []map[string]string{
81 {"": "foo"},
82 {"": "bar", "baz": "2"}}},
83 {`foo; bar="b,a;z"`, []map[string]string{
84 {"": "foo", "bar": "b,a;z"}}},
85 {`foo , bar; baz = 2`, []map[string]string{
86 {"": "foo"},
87 {"": "bar", "baz": "2"}}},
88 {`foo, bar; baz=2 junk`, []map[string]string{
89 {"": "foo"}}},
90 {`foo junk, bar; baz=2 junk`, nil},
91 {`mux; max-channels=4; flow-control, deflate-stream`, []map[string]string{
92 {"": "mux", "max-channels": "4", "flow-control": ""},
93 {"": "deflate-stream"}}},
94 {`permessage-foo; x="10"`, []map[string]string{
95 {"": "permessage-foo", "x": "10"}}},
96 {`permessage-foo; use_y, permessage-foo`, []map[string]string{
97 {"": "permessage-foo", "use_y": ""},
98 {"": "permessage-foo"}}},
99 {`permessage-deflate; client_max_window_bits; server_max_window_bits=10 , permessage-deflate; client_max_window_bits`, []map[string]string{
100 {"": "permessage-deflate", "client_max_window_bits": "", "server_max_window_bits": "10"},
101 {"": "permessage-deflate", "client_max_window_bits": ""}}},
102 {"permessage-deflate; server_no_context_takeover; client_max_window_bits=15", []map[string]string{
103 {"": "permessage-deflate", "server_no_context_takeover": "", "client_max_window_bits": "15"},
104 }},
105 }
106
107 func TestParseExtensions(t *testing.T) {
108 for _, tt := range parseExtensionTests {
109 h := http.Header{http.CanonicalHeaderKey("Sec-WebSocket-Extensions"): {tt.value}}
110 extensions := parseExtensions(h)
111 if !reflect.DeepEqual(extensions, tt.extensions) {
112 t.Errorf("parseExtensions(%q)\n = %v,\nwant %v", tt.value, extensions, tt.extensions)
113 }
114 }
115 }
116
View as plain text