2013-10-16 20:41:47 +04:00
|
|
|
// Copyright 2013 Gary Burd. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
package websocket
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
import (
|
2013-12-14 09:30:22 +04:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2013-10-16 20:41:47 +04:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2013-12-14 19:48:43 +04:00
|
|
|
"reflect"
|
2013-10-16 20:41:47 +04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
type handshakeHandler struct {
|
2013-10-16 20:41:47 +04:00
|
|
|
*testing.T
|
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
func (t handshakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2013-10-16 20:41:47 +04:00
|
|
|
if r.Method != "GET" {
|
|
|
|
http.Error(w, "Method not allowed", 405)
|
2013-12-14 19:48:43 +04:00
|
|
|
t.Logf("method = %s, want GET", r.Method)
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
2013-12-14 19:48:43 +04:00
|
|
|
if origin := r.Header.Get("Origin"); origin != "http://"+r.Host {
|
2013-10-16 20:41:47 +04:00
|
|
|
http.Error(w, "Origin not allowed", 403)
|
2013-12-14 19:48:43 +04:00
|
|
|
t.Logf("Origin = %s, want %s", origin, r.Host)
|
2013-10-16 20:41:47 +04:00
|
|
|
return
|
|
|
|
}
|
2013-12-14 19:48:43 +04:00
|
|
|
subprotos := Subprotocols(r)
|
|
|
|
if !reflect.DeepEqual(subprotos, handshakeDialer.Subprotocols) {
|
|
|
|
http.Error(w, "bad protocol", 400)
|
|
|
|
t.Logf("Subprotocols = %v, want %v", subprotos, handshakeDialer.Subprotocols)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws, err := Upgrade(w, r, http.Header{
|
|
|
|
"Set-Cookie": {"sessionID=1234"},
|
|
|
|
"Sec-Websocket-Protocol": {subprotos[0]},
|
|
|
|
}, 1024, 1024)
|
2013-12-14 09:30:22 +04:00
|
|
|
if _, ok := err.(HandshakeError); ok {
|
2013-10-16 20:41:47 +04:00
|
|
|
t.Logf("bad handshake: %v", err)
|
|
|
|
http.Error(w, "Not a websocket handshake", 400)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Logf("upgrade error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
2013-12-14 19:48:43 +04:00
|
|
|
|
|
|
|
if ws.Subprotocol() != subprotos[0] {
|
|
|
|
t.Logf("ws.Subprotocol() = %s, want %s", ws.Subprotocol(), subprotos[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
for {
|
|
|
|
op, r, err := ws.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Logf("NextReader: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w, err := ws.NextWriter(op)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("NextWriter: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(w, r); err != nil {
|
|
|
|
t.Logf("Copy: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
t.Logf("Close: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:48:43 +04:00
|
|
|
var handshakeDialer = &Dialer{
|
|
|
|
Subprotocols: []string{"p1", "p2"},
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
2013-12-14 09:30:22 +04:00
|
|
|
func TestHandshake(t *testing.T) {
|
|
|
|
s := httptest.NewServer(handshakeHandler{t})
|
2013-10-16 20:41:47 +04:00
|
|
|
defer s.Close()
|
2013-12-14 19:48:43 +04:00
|
|
|
ws, resp, err := handshakeDialer.Dial(httpToWs(s.URL), http.Header{"Origin": {s.URL}})
|
2013-10-16 20:41:47 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial: %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
var sessionID string
|
|
|
|
for _, c := range resp.Cookies() {
|
|
|
|
if c.Name == "sessionID" {
|
|
|
|
sessionID = c.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sessionID != "1234" {
|
|
|
|
t.Error("Set-Cookie not received from the server.")
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:48:43 +04:00
|
|
|
if ws.Subprotocol() != handshakeDialer.Subprotocols[0] {
|
|
|
|
t.Errorf("ws.Subprotocol() = %s, want %s", ws.Subprotocol(), handshakeDialer.Subprotocols[0])
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2013-12-14 19:48:43 +04:00
|
|
|
|
|
|
|
sendRecv(t, ws)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
|
|
|
|
type dialHandler struct {
|
|
|
|
*testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t dialHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
if _, ok := err.(HandshakeError); ok {
|
|
|
|
t.Logf("bad handshake: %v", err)
|
|
|
|
http.Error(w, "Not a websocket handshake", 400)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Logf("upgrade error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
for {
|
|
|
|
mt, p, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Logf("ReadMessage: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := ws.WriteMessage(mt, p); err != nil {
|
|
|
|
t.Logf("WriteMessage: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendRecv(t *testing.T, ws *Conn) {
|
|
|
|
const message = "Hello World!"
|
2013-12-14 19:48:43 +04:00
|
|
|
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetWriteDeadline: %v", err)
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
if err := ws.WriteMessage(TextMessage, []byte(message)); err != nil {
|
|
|
|
t.Fatalf("WriteMessage: %v", err)
|
|
|
|
}
|
2013-12-14 19:48:43 +04:00
|
|
|
if err := ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
|
|
|
|
t.Fatalf("SetReadDeadline: %v", err)
|
|
|
|
}
|
2013-12-14 09:30:22 +04:00
|
|
|
_, p, err := ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadMessage: %v", err)
|
|
|
|
}
|
|
|
|
if string(p) != message {
|
|
|
|
t.Fatalf("message=%s, want %s", p, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpToWs(u string) string {
|
|
|
|
return "ws" + u[len("http"):]
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDial(t *testing.T) {
|
|
|
|
s := httptest.NewServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
ws, _, err := DefaultDialer.Dial(httpToWs(s.URL), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial() returned error %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialTLS(t *testing.T) {
|
|
|
|
s := httptest.NewTLSServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
certs := x509.NewCertPool()
|
|
|
|
for _, c := range s.TLS.Certificates {
|
|
|
|
roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing server's root cert: %v", err)
|
|
|
|
}
|
|
|
|
for _, root := range roots {
|
|
|
|
certs.AddCert(root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := url.Parse(s.URL)
|
|
|
|
d := &Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) { return net.Dial(network, u.Host) },
|
|
|
|
TLSClientConfig: &tls.Config{RootCAs: certs},
|
|
|
|
}
|
|
|
|
ws, _, err := d.Dial("wss://example.com/", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial() returned error %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialTLSBadCert(t *testing.T) {
|
|
|
|
s := httptest.NewTLSServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
_, _, err := DefaultDialer.Dial(httpToWs(s.URL), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Dial() did not return error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialTLSNoVerify(t *testing.T) {
|
|
|
|
s := httptest.NewTLSServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
d := &Dialer{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
|
|
|
|
ws, _, err := d.Dial(httpToWs(s.URL), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial() returned error %v", err)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
sendRecv(t, ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialTimeout(t *testing.T) {
|
|
|
|
s := httptest.NewServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
d := &Dialer{
|
|
|
|
HandshakeTimeout: -1,
|
|
|
|
}
|
|
|
|
_, _, err := d.Dial(httpToWs(s.URL), nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Dial() did not return error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialBadScheme(t *testing.T) {
|
|
|
|
s := httptest.NewServer(dialHandler{t})
|
|
|
|
defer s.Close()
|
|
|
|
_, _, err := DefaultDialer.Dial(s.URL, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Dial() did not return error")
|
|
|
|
}
|
|
|
|
}
|