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}
21
22func TestEqualASCIIFold(t *testing.T) {
23	for _, tt := range equalASCIIFoldTests {
24		eq := equalASCIIFold(tt.s, tt.t)
25		if eq != tt.eq {
26			t.Errorf("equalASCIIFold(%q, %q) = %v, want %v", tt.s, tt.t, eq, tt.eq)
27		}
28	}
29}
30
31var tokenListContainsValueTests = []struct {
32	value string
33	ok    bool
34}{
35	{"WebSocket", true},
36	{"WEBSOCKET", true},
37	{"websocket", true},
38	{"websockets", false},
39	{"x websocket", false},
40	{"websocket x", false},
41	{"other,websocket,more", true},
42	{"other, websocket, more", true},
43}
44
45func TestTokenListContainsValue(t *testing.T) {
46	for _, tt := range tokenListContainsValueTests {
47		h := http.Header{"Upgrade": {tt.value}}
48		ok := tokenListContainsValue(h, "Upgrade", "websocket")
49		if ok != tt.ok {
50			t.Errorf("tokenListContainsValue(h, n, %q) = %v, want %v", tt.value, ok, tt.ok)
51		}
52	}
53}
54
55var parseExtensionTests = []struct {
56	value      string
57	extensions []map[string]string
58}{
59	{`foo`, []map[string]string{{"": "foo"}}},
60	{`foo, bar; baz=2`, []map[string]string{
61		{"": "foo"},
62		{"": "bar", "baz": "2"}}},
63	{`foo; bar="b,a;z"`, []map[string]string{
64		{"": "foo", "bar": "b,a;z"}}},
65	{`foo , bar; baz = 2`, []map[string]string{
66		{"": "foo"},
67		{"": "bar", "baz": "2"}}},
68	{`foo, bar; baz=2 junk`, []map[string]string{
69		{"": "foo"}}},
70	{`foo junk, bar; baz=2 junk`, nil},
71	{`mux; max-channels=4; flow-control, deflate-stream`, []map[string]string{
72		{"": "mux", "max-channels": "4", "flow-control": ""},
73		{"": "deflate-stream"}}},
74	{`permessage-foo; x="10"`, []map[string]string{
75		{"": "permessage-foo", "x": "10"}}},
76	{`permessage-foo; use_y, permessage-foo`, []map[string]string{
77		{"": "permessage-foo", "use_y": ""},
78		{"": "permessage-foo"}}},
79	{`permessage-deflate; client_max_window_bits; server_max_window_bits=10 , permessage-deflate; client_max_window_bits`, []map[string]string{
80		{"": "permessage-deflate", "client_max_window_bits": "", "server_max_window_bits": "10"},
81		{"": "permessage-deflate", "client_max_window_bits": ""}}},
82	{"permessage-deflate; server_no_context_takeover; client_max_window_bits=15", []map[string]string{
83		{"": "permessage-deflate", "server_no_context_takeover": "", "client_max_window_bits": "15"},
84	}},
85}
86
87func TestParseExtensions(t *testing.T) {
88	for _, tt := range parseExtensionTests {
89		h := http.Header{http.CanonicalHeaderKey("Sec-WebSocket-Extensions"): {tt.value}}
90		extensions := parseExtensions(h)
91		if !reflect.DeepEqual(extensions, tt.extensions) {
92			t.Errorf("parseExtensions(%q)\n    = %v,\nwant %v", tt.value, extensions, tt.extensions)
93		}
94	}
95}
96