client_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/url"
  7. "reflect"
  8. "testing"
  9. )
  10. var parseURLTests = []struct {
  11. s string
  12. u *url.URL
  13. rui string
  14. }{
  15. {"ws://example.com/", &url.URL{Scheme: "ws", Host: "example.com", Opaque: "/"}, "/"},
  16. {"ws://example.com", &url.URL{Scheme: "ws", Host: "example.com", Opaque: "/"}, "/"},
  17. {"ws://example.com:7777/", &url.URL{Scheme: "ws", Host: "example.com:7777", Opaque: "/"}, "/"},
  18. {"wss://example.com/", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/"}, "/"},
  19. {"wss://example.com/a/b", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b"}, "/a/b"},
  20. {"ss://example.com/a/b", nil, ""},
  21. {"ws://webmaster@example.com/", nil, ""},
  22. {"wss://example.com/a/b?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b", RawQuery: "x=y"}, "/a/b?x=y"},
  23. {"wss://example.com?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/", RawQuery: "x=y"}, "/?x=y"},
  24. }
  25. func TestParseURL(t *testing.T) {
  26. for _, tt := range parseURLTests {
  27. u, err := parseURL(tt.s)
  28. if tt.u != nil && err != nil {
  29. t.Errorf("parseURL(%q) returned error %v", tt.s, err)
  30. continue
  31. }
  32. if tt.u == nil {
  33. if err == nil {
  34. t.Errorf("parseURL(%q) did not return error", tt.s)
  35. }
  36. continue
  37. }
  38. if !reflect.DeepEqual(u, tt.u) {
  39. t.Errorf("parseURL(%q) = %v, want %v", tt.s, u, tt.u)
  40. continue
  41. }
  42. if u.RequestURI() != tt.rui {
  43. t.Errorf("parseURL(%q).RequestURI() = %v, want %v", tt.s, u.RequestURI(), tt.rui)
  44. }
  45. }
  46. }
  47. var hostPortNoPortTests = []struct {
  48. u *url.URL
  49. hostPort, hostNoPort string
  50. }{
  51. {&url.URL{Scheme: "ws", Host: "example.com"}, "example.com:80", "example.com"},
  52. {&url.URL{Scheme: "wss", Host: "example.com"}, "example.com:443", "example.com"},
  53. {&url.URL{Scheme: "ws", Host: "example.com:7777"}, "example.com:7777", "example.com"},
  54. {&url.URL{Scheme: "wss", Host: "example.com:7777"}, "example.com:7777", "example.com"},
  55. }
  56. func TestHostPortNoPort(t *testing.T) {
  57. for _, tt := range hostPortNoPortTests {
  58. hostPort, hostNoPort := hostPortNoPort(tt.u)
  59. if hostPort != tt.hostPort {
  60. t.Errorf("hostPortNoPort(%v) returned hostPort %q, want %q", tt.u, hostPort, tt.hostPort)
  61. }
  62. if hostNoPort != tt.hostNoPort {
  63. t.Errorf("hostPortNoPort(%v) returned hostNoPort %q, want %q", tt.u, hostNoPort, tt.hostNoPort)
  64. }
  65. }
  66. }