websocket/client_server_test.go

250 lines
5.2 KiB
Go
Raw Normal View History

// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
2013-10-16 20:41:47 +04:00
// 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"
)
2014-07-05 19:39:32 +04:00
var cstUpgrader = Upgrader{
2014-04-20 23:08:24 +04:00
Subprotocols: []string{"p0", "p1"},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
2014-07-05 19:39:32 +04:00
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
http.Error(w, reason.Error(), status)
},
2014-04-20 23:08:24 +04:00
}
2014-07-05 19:39:32 +04:00
var cstDialer = Dialer{
2014-04-20 23:08:24 +04:00
Subprotocols: []string{"p1", "p2"},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
2014-07-05 19:39:32 +04:00
type cstHandler struct{ *testing.T }
type Server struct {
*httptest.Server
URL string
2013-10-16 20:41:47 +04:00
}
2014-07-05 19:39:32 +04:00
func newServer(t *testing.T) *Server {
var s Server
s.Server = httptest.NewServer(cstHandler{t})
s.URL = "ws" + s.Server.URL[len("http"):]
return &s
}
func newTLSServer(t *testing.T) *Server {
var s Server
s.Server = httptest.NewTLSServer(cstHandler{t})
s.URL = "ws" + s.Server.URL[len("http"):]
return &s
}
func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2013-10-16 20:41:47 +04:00
if r.Method != "GET" {
2014-07-05 19:39:32 +04:00
t.Logf("method %s not allowed", r.Method)
http.Error(w, "method not allowed", 405)
2013-10-16 20:41:47 +04:00
return
}
2013-12-14 19:48:43 +04:00
subprotos := Subprotocols(r)
2014-07-05 19:39:32 +04:00
if !reflect.DeepEqual(subprotos, cstDialer.Subprotocols) {
t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols)
2013-12-14 19:48:43 +04:00
http.Error(w, "bad protocol", 400)
return
}
2014-07-05 19:39:32 +04:00
ws, err := cstUpgrader.Upgrade(w, r, http.Header{"Set-Cookie": {"sessionID=1234"}})
2014-04-20 23:08:24 +04:00
if err != nil {
2014-07-05 19:39:32 +04:00
t.Logf("Upgrade: %v", err)
2013-10-16 20:41:47 +04:00
return
}
defer ws.Close()
2013-12-14 19:48:43 +04:00
2014-04-20 23:08:24 +04:00
if ws.Subprotocol() != "p1" {
2014-07-05 19:39:32 +04:00
t.Logf("Subprotocol() = %s, want p1", ws.Subprotocol())
ws.Close()
2013-12-14 19:48:43 +04:00
return
}
2014-07-05 19:39:32 +04:00
op, rd, err := ws.NextReader()
2013-10-16 20:41:47 +04:00
if err != nil {
2014-07-05 19:39:32 +04:00
t.Logf("NextReader: %v", err)
return
2013-10-16 20:41:47 +04:00
}
2014-07-05 19:39:32 +04:00
wr, err := ws.NextWriter(op)
if err != nil {
t.Logf("NextWriter: %v", err)
return
2013-10-16 20:41:47 +04:00
}
2014-07-05 19:39:32 +04:00
if _, err = io.Copy(wr, rd); err != nil {
t.Logf("NextWriter: %v", err)
return
2013-10-16 20:41:47 +04:00
}
2014-07-05 19:39:32 +04:00
if err := wr.Close(); err != nil {
t.Logf("Close: %v", err)
return
2013-10-16 20:41:47 +04:00
}
}
2013-12-14 09:30:22 +04:00
2014-07-05 19:39:32 +04:00
func sendRecv(t *testing.T, ws *Conn) {
const message = "Hello World!"
if err := ws.SetWriteDeadline(time.Now().Add(time.Second)); err != nil {
t.Fatalf("SetWriteDeadline: %v", err)
}
if err := ws.WriteMessage(TextMessage, []byte(message)); err != nil {
t.Fatalf("WriteMessage: %v", err)
}
if err := ws.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
t.Fatalf("SetReadDeadline: %v", err)
}
_, p, err := ws.ReadMessage()
2014-04-20 23:08:24 +04:00
if err != nil {
2014-07-05 19:39:32 +04:00
t.Fatalf("ReadMessage: %v", err)
2013-12-14 09:30:22 +04:00
}
2014-07-05 19:39:32 +04:00
if string(p) != message {
t.Fatalf("message=%s, want %s", p, message)
2013-12-14 09:30:22 +04:00
}
}
func TestDial(t *testing.T) {
2014-07-05 19:39:32 +04:00
s := newServer(t)
2013-12-14 09:30:22 +04:00
defer s.Close()
2014-07-05 19:39:32 +04:00
ws, _, err := cstDialer.Dial(s.URL, nil)
2013-12-14 09:30:22 +04:00
if err != nil {
2014-07-05 19:39:32 +04:00
t.Fatalf("Dial: %v", err)
2013-12-14 09:30:22 +04:00
}
defer ws.Close()
sendRecv(t, ws)
}
func TestDialTLS(t *testing.T) {
2014-07-05 19:39:32 +04:00
s := newTLSServer(t)
2013-12-14 09:30:22 +04:00
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)
2014-07-05 19:39:32 +04:00
d := cstDialer
d.NetDial = func(network, addr string) (net.Conn, error) { return net.Dial(network, u.Host) }
d.TLSClientConfig = &tls.Config{RootCAs: certs}
2013-12-14 09:30:22 +04:00
ws, _, err := d.Dial("wss://example.com/", nil)
if err != nil {
2014-07-05 19:39:32 +04:00
t.Fatalf("Dial: %v", err)
2013-12-14 09:30:22 +04:00
}
defer ws.Close()
sendRecv(t, ws)
}
2014-07-05 19:39:32 +04:00
func xTestDialTLSBadCert(t *testing.T) {
s := newTLSServer(t)
2013-12-14 09:30:22 +04:00
defer s.Close()
2014-07-05 19:39:32 +04:00
ws, _, err := cstDialer.Dial(s.URL, nil)
2013-12-14 09:30:22 +04:00
if err == nil {
2014-07-05 19:39:32 +04:00
ws.Close()
t.Fatalf("Dial: nil")
2013-12-14 09:30:22 +04:00
}
}
2014-07-05 19:39:32 +04:00
func xTestDialTLSNoVerify(t *testing.T) {
s := newTLSServer(t)
2013-12-14 09:30:22 +04:00
defer s.Close()
2014-07-05 19:39:32 +04:00
d := cstDialer
d.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
ws, _, err := d.Dial(s.URL, nil)
2013-12-14 09:30:22 +04:00
if err != nil {
2014-07-05 19:39:32 +04:00
t.Fatalf("Dial: %v", err)
2013-12-14 09:30:22 +04:00
}
defer ws.Close()
sendRecv(t, ws)
}
func TestDialTimeout(t *testing.T) {
2014-07-05 19:39:32 +04:00
s := newServer(t)
2013-12-14 09:30:22 +04:00
defer s.Close()
2014-07-05 19:39:32 +04:00
d := cstDialer
d.HandshakeTimeout = -1
ws, _, err := d.Dial(s.URL, nil)
2013-12-14 09:30:22 +04:00
if err == nil {
2014-07-05 19:39:32 +04:00
ws.Close()
t.Fatalf("Dial: nil")
2013-12-14 09:30:22 +04:00
}
}
func TestDialBadScheme(t *testing.T) {
2014-07-05 19:39:32 +04:00
s := newServer(t)
2013-12-14 09:30:22 +04:00
defer s.Close()
2014-07-05 19:39:32 +04:00
ws, _, err := cstDialer.Dial(s.Server.URL, nil)
2013-12-14 09:30:22 +04:00
if err == nil {
2014-07-05 19:39:32 +04:00
ws.Close()
t.Fatalf("Dial: nil")
2013-12-14 09:30:22 +04:00
}
}
func TestDialBadOrigin(t *testing.T) {
2014-07-05 19:39:32 +04:00
s := newServer(t)
defer s.Close()
2014-07-05 19:39:32 +04:00
ws, resp, err := cstDialer.Dial(s.URL, http.Header{"Origin": {"bad"}})
if err == nil {
2014-07-05 19:39:32 +04:00
ws.Close()
t.Fatalf("Dial: nil")
}
if resp == nil {
t.Fatalf("resp=nil, err=%v", err)
}
if resp.StatusCode != http.StatusForbidden {
t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
}
}
func TestHandshake(t *testing.T) {
s := newServer(t)
defer s.Close()
ws, resp, err := cstDialer.Dial(s.URL, http.Header{"Origin": {s.URL}})
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
}
}
2014-07-05 19:39:32 +04:00
if sessionID != "1234" {
t.Error("Set-Cookie not received from the server.")
}
if ws.Subprotocol() != "p1" {
t.Errorf("ws.Subprotocol() = %s, want p1", ws.Subprotocol())
}
sendRecv(t, ws)
}