1
0

util_test.go 2.9 KB

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