1// Copyright 2013 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 subprotocolTests = []struct { 14 h string 15 protocols []string 16}{ 17 {"", nil}, 18 {"foo", []string{"foo"}}, 19 {"foo,bar", []string{"foo", "bar"}}, 20 {"foo, bar", []string{"foo", "bar"}}, 21 {" foo, bar", []string{"foo", "bar"}}, 22 {" foo, bar ", []string{"foo", "bar"}}, 23} 24 25func TestSubprotocols(t *testing.T) { 26 for _, st := range subprotocolTests { 27 r := http.Request{Header: http.Header{"Sec-Websocket-Protocol": {st.h}}} 28 protocols := Subprotocols(&r) 29 if !reflect.DeepEqual(st.protocols, protocols) { 30 t.Errorf("SubProtocols(%q) returned %#v, want %#v", st.h, protocols, st.protocols) 31 } 32 } 33} 34 35var isWebSocketUpgradeTests = []struct { 36 ok bool 37 h http.Header 38}{ 39 {false, http.Header{"Upgrade": {"websocket"}}}, 40 {false, http.Header{"Connection": {"upgrade"}}}, 41 {true, http.Header{"Connection": {"upgRade"}, "Upgrade": {"WebSocket"}}}, 42} 43 44func TestIsWebSocketUpgrade(t *testing.T) { 45 for _, tt := range isWebSocketUpgradeTests { 46 ok := IsWebSocketUpgrade(&http.Request{Header: tt.h}) 47 if tt.ok != ok { 48 t.Errorf("IsWebSocketUpgrade(%v) returned %v, want %v", tt.h, ok, tt.ok) 49 } 50 } 51} 52