1// Copyright 2014 The Gorilla WebSocket Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package websocket
6
7import (
8	"net/http"
9	"reflect"
10	"testing"
11)
12
13var 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
23func 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
32var 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
46func 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
56var parseExtensionTests = []struct {
57	value      string
58	extensions []map[string]string
59}{
60	{`foo`, []map[string]string{{"": "foo"}}},
61	{`foo, bar; baz=2`, []map[string]string{
62		{"": "foo"},
63		{"": "bar", "baz": "2"}}},
64	{`foo; bar="b,a;z"`, []map[string]string{
65		{"": "foo", "bar": "b,a;z"}}},
66	{`foo , bar; baz = 2`, []map[string]string{
67		{"": "foo"},
68		{"": "bar", "baz": "2"}}},
69	{`foo, bar; baz=2 junk`, []map[string]string{
70		{"": "foo"}}},
71	{`foo junk, bar; baz=2 junk`, nil},
72	{`mux; max-channels=4; flow-control, deflate-stream`, []map[string]string{
73		{"": "mux", "max-channels": "4", "flow-control": ""},
74		{"": "deflate-stream"}}},
75	{`permessage-foo; x="10"`, []map[string]string{
76		{"": "permessage-foo", "x": "10"}}},
77	{`permessage-foo; use_y, permessage-foo`, []map[string]string{
78		{"": "permessage-foo", "use_y": ""},
79		{"": "permessage-foo"}}},
80	{`permessage-deflate; client_max_window_bits; server_max_window_bits=10 , permessage-deflate; client_max_window_bits`, []map[string]string{
81		{"": "permessage-deflate", "client_max_window_bits": "", "server_max_window_bits": "10"},
82		{"": "permessage-deflate", "client_max_window_bits": ""}}},
83	{"permessage-deflate; server_no_context_takeover; client_max_window_bits=15", []map[string]string{
84		{"": "permessage-deflate", "server_no_context_takeover": "", "client_max_window_bits": "15"},
85	}},
86}
87
88func TestParseExtensions(t *testing.T) {
89	for _, tt := range parseExtensionTests {
90		h := http.Header{http.CanonicalHeaderKey("Sec-WebSocket-Extensions"): {tt.value}}
91		extensions := parseExtensions(h)
92		if !reflect.DeepEqual(extensions, tt.extensions) {
93			t.Errorf("parseExtensions(%q)\n    = %v,\nwant %v", tt.value, extensions, tt.extensions)
94		}
95	}
96}
97