1
0

server_test.go 1.3 KB

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