summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gorilla/websocket/client_server_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gorilla/websocket/client_server_test.go')
-rw-r--r--vendor/github.com/gorilla/websocket/client_server_test.go124
1 files changed, 107 insertions, 17 deletions
diff --git a/vendor/github.com/gorilla/websocket/client_server_test.go b/vendor/github.com/gorilla/websocket/client_server_test.go
index 7d39da681..50063b7e0 100644
--- a/vendor/github.com/gorilla/websocket/client_server_test.go
+++ b/vendor/github.com/gorilla/websocket/client_server_test.go
@@ -5,11 +5,14 @@
package websocket
import (
+ "bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
+ "encoding/binary"
"io"
"io/ioutil"
+ "net"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
@@ -31,9 +34,10 @@ var cstUpgrader = Upgrader{
}
var cstDialer = Dialer{
- Subprotocols: []string{"p1", "p2"},
- ReadBufferSize: 1024,
- WriteBufferSize: 1024,
+ Subprotocols: []string{"p1", "p2"},
+ ReadBufferSize: 1024,
+ WriteBufferSize: 1024,
+ HandshakeTimeout: 30 * time.Second,
}
type cstHandler struct{ *testing.T }
@@ -143,8 +147,9 @@ func TestProxyDial(t *testing.T) {
s := newServer(t)
defer s.Close()
- surl, _ := url.Parse(s.URL)
+ surl, _ := url.Parse(s.Server.URL)
+ cstDialer := cstDialer // make local copy for modification on next line.
cstDialer.Proxy = http.ProxyURL(surl)
connect := false
@@ -160,8 +165,8 @@ func TestProxyDial(t *testing.T) {
}
if !connect {
- t.Log("connect not recieved")
- http.Error(w, "connect not recieved", 405)
+ t.Log("connect not received")
+ http.Error(w, "connect not received", 405)
return
}
origHandler.ServeHTTP(w, r)
@@ -173,16 +178,16 @@ func TestProxyDial(t *testing.T) {
}
defer ws.Close()
sendRecv(t, ws)
-
- cstDialer.Proxy = http.ProxyFromEnvironment
}
func TestProxyAuthorizationDial(t *testing.T) {
s := newServer(t)
defer s.Close()
- surl, _ := url.Parse(s.URL)
+ surl, _ := url.Parse(s.Server.URL)
surl.User = url.UserPassword("username", "password")
+
+ cstDialer := cstDialer // make local copy for modification on next line.
cstDialer.Proxy = http.ProxyURL(surl)
connect := false
@@ -200,8 +205,8 @@ func TestProxyAuthorizationDial(t *testing.T) {
}
if !connect {
- t.Log("connect with proxy authorization not recieved")
- http.Error(w, "connect with proxy authorization not recieved", 405)
+ t.Log("connect with proxy authorization not received")
+ http.Error(w, "connect with proxy authorization not received", 405)
return
}
origHandler.ServeHTTP(w, r)
@@ -213,8 +218,6 @@ func TestProxyAuthorizationDial(t *testing.T) {
}
defer ws.Close()
sendRecv(t, ws)
-
- cstDialer.Proxy = http.ProxyFromEnvironment
}
func TestDial(t *testing.T) {
@@ -237,7 +240,7 @@ func TestDialCookieJar(t *testing.T) {
d := cstDialer
d.Jar = jar
- u, _ := parseURL(s.URL)
+ u, _ := url.Parse(s.URL)
switch u.Scheme {
case "ws":
@@ -246,7 +249,7 @@ func TestDialCookieJar(t *testing.T) {
u.Scheme = "https"
}
- cookies := []*http.Cookie{&http.Cookie{Name: "gorilla", Value: "ws", Path: "/"}}
+ cookies := []*http.Cookie{{Name: "gorilla", Value: "ws", Path: "/"}}
d.Jar.SetCookies(u, cookies)
ws, _, err := d.Dial(s.URL, nil)
@@ -398,9 +401,17 @@ func TestBadMethod(t *testing.T) {
}))
defer s.Close()
- resp, err := http.PostForm(s.URL, url.Values{})
+ req, err := http.NewRequest("POST", s.URL, strings.NewReader(""))
+ if err != nil {
+ t.Fatalf("NewRequest returned error %v", err)
+ }
+ req.Header.Set("Connection", "upgrade")
+ req.Header.Set("Upgrade", "websocket")
+ req.Header.Set("Sec-Websocket-Version", "13")
+
+ resp, err := http.DefaultClient.Do(req)
if err != nil {
- t.Fatalf("PostForm returned error %v", err)
+ t.Fatalf("Do returned error %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusMethodNotAllowed {
@@ -510,3 +521,82 @@ func TestDialCompression(t *testing.T) {
defer ws.Close()
sendRecv(t, ws)
}
+
+func TestSocksProxyDial(t *testing.T) {
+ s := newServer(t)
+ defer s.Close()
+
+ proxyListener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("listen failed: %v", err)
+ }
+ defer proxyListener.Close()
+ go func() {
+ c1, err := proxyListener.Accept()
+ if err != nil {
+ t.Errorf("proxy accept failed: %v", err)
+ return
+ }
+ defer c1.Close()
+
+ c1.SetDeadline(time.Now().Add(30 * time.Second))
+
+ buf := make([]byte, 32)
+ if _, err := io.ReadFull(c1, buf[:3]); err != nil {
+ t.Errorf("read failed: %v", err)
+ return
+ }
+ if want := []byte{5, 1, 0}; !bytes.Equal(want, buf[:len(want)]) {
+ t.Errorf("read %x, want %x", buf[:len(want)], want)
+ }
+ if _, err := c1.Write([]byte{5, 0}); err != nil {
+ t.Errorf("write failed: %v", err)
+ return
+ }
+ if _, err := io.ReadFull(c1, buf[:10]); err != nil {
+ t.Errorf("read failed: %v", err)
+ return
+ }
+ if want := []byte{5, 1, 0, 1}; !bytes.Equal(want, buf[:len(want)]) {
+ t.Errorf("read %x, want %x", buf[:len(want)], want)
+ return
+ }
+ buf[1] = 0
+ if _, err := c1.Write(buf[:10]); err != nil {
+ t.Errorf("write failed: %v", err)
+ return
+ }
+
+ ip := net.IP(buf[4:8])
+ port := binary.BigEndian.Uint16(buf[8:10])
+
+ c2, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: int(port)})
+ if err != nil {
+ t.Errorf("dial failed; %v", err)
+ return
+ }
+ defer c2.Close()
+ done := make(chan struct{})
+ go func() {
+ io.Copy(c1, c2)
+ close(done)
+ }()
+ io.Copy(c2, c1)
+ <-done
+ }()
+
+ purl, err := url.Parse("socks5://" + proxyListener.Addr().String())
+ if err != nil {
+ t.Fatalf("parse failed: %v", err)
+ }
+
+ cstDialer := cstDialer // make local copy for modification on next line.
+ cstDialer.Proxy = http.ProxyURL(purl)
+
+ ws, _, err := cstDialer.Dial(s.URL, nil)
+ if err != nil {
+ t.Fatalf("Dial: %v", err)
+ }
+ defer ws.Close()
+ sendRecv(t, ws)
+}