util_test.go 2.9 KB

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