client_test.go 2.3 KB

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