github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gorilla/websocket/client_test.go (about)

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