2009-11-29 15:22:44 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:41:46 -07:00
|
|
|
"bytes"
|
2010-02-18 19:32:40 -07:00
|
|
|
"fmt"
|
2009-12-15 16:41:46 -07:00
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"once"
|
|
|
|
"testing"
|
2009-11-29 15:22:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var serverAddr string
|
|
|
|
|
2009-12-15 16:41:46 -07:00
|
|
|
func echoServer(ws *Conn) { io.Copy(ws, ws) }
|
2009-11-29 15:22:44 -07:00
|
|
|
|
|
|
|
func startServer() {
|
2010-06-21 14:05:38 -06:00
|
|
|
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
|
2009-11-29 15:22:44 -07:00
|
|
|
if e != nil {
|
|
|
|
log.Exitf("net.Listen tcp :0 %v", e)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
serverAddr = l.Addr().String()
|
|
|
|
log.Stderr("Test WebSocket server listening on ", serverAddr)
|
|
|
|
http.Handle("/echo", Handler(echoServer))
|
2010-03-23 19:09:24 -06:00
|
|
|
http.Handle("/echoDraft75", Draft75Handler(echoServer))
|
2009-12-15 16:41:46 -07:00
|
|
|
go http.Serve(l, nil)
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEcho(t *testing.T) {
|
2009-12-15 16:41:46 -07:00
|
|
|
once.Do(startServer)
|
2009-11-29 15:22:44 -07:00
|
|
|
|
2010-03-23 19:09:24 -06:00
|
|
|
// websocket.Dial()
|
2009-12-15 16:41:46 -07:00
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
ws, err := newClient("/echo", "localhost", "http://localhost",
|
2010-03-23 19:09:24 -06:00
|
|
|
"ws://localhost/echo", "", client, handshake)
|
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake error: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := []byte("hello, world\n")
|
|
|
|
if _, err := ws.Write(msg); err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("Write: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
}
|
|
|
|
var actual_msg = make([]byte, 512)
|
|
|
|
n, err := ws.Read(actual_msg)
|
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("Read: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
}
|
|
|
|
actual_msg = actual_msg[0:n]
|
|
|
|
if !bytes.Equal(msg, actual_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg, actual_msg)
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoDraft75(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
// websocket.Dial()
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
ws, err := newClient("/echoDraft75", "localhost", "http://localhost",
|
|
|
|
"ws://localhost/echoDraft75", "", client, draft75handshake)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake: %v", err)
|
2009-12-15 16:41:46 -07:00
|
|
|
return
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2010-03-23 19:09:24 -06:00
|
|
|
|
2010-02-25 17:01:29 -07:00
|
|
|
msg := []byte("hello, world\n")
|
2009-11-29 15:22:44 -07:00
|
|
|
if _, err := ws.Write(msg); err != nil {
|
|
|
|
t.Errorf("Write: error %v", err)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
var actual_msg = make([]byte, 512)
|
|
|
|
n, err := ws.Read(actual_msg)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: error %v", err)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
actual_msg = actual_msg[0:n]
|
2009-11-29 15:22:44 -07:00
|
|
|
if !bytes.Equal(msg, actual_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg, actual_msg)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
ws.Close()
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2010-02-18 19:32:40 -07:00
|
|
|
|
2010-03-19 15:18:02 -06:00
|
|
|
func TestWithQuery(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := newClient("/echo?q=v", "localhost", "http://localhost",
|
2010-03-23 19:09:24 -06:00
|
|
|
"ws://localhost/echo?q=v", "", client, handshake)
|
2010-03-19 15:18:02 -06:00
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake: %v", err)
|
2010-03-19 15:18:02 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
|
2010-02-18 19:32:40 -07:00
|
|
|
func TestHTTP(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
2010-03-23 19:09:24 -06:00
|
|
|
// If the client did not send a handshake that matches the protocol
|
|
|
|
// specification, the server should abort the WebSocket connection.
|
|
|
|
_, _, err := http.Get(fmt.Sprintf("http://%s/echo", serverAddr))
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Get: unexpected success")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
urlerr, ok := err.(*http.URLError)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Get: not URLError %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if urlerr.Error != io.ErrUnexpectedEOF {
|
|
|
|
t.Errorf("Get: error %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPDraft75(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
r, _, err := http.Get(fmt.Sprintf("http://%s/echoDraft75", serverAddr))
|
2010-02-18 19:32:40 -07:00
|
|
|
if err != nil {
|
2010-03-23 19:09:24 -06:00
|
|
|
t.Errorf("Get: error %#v", err)
|
2010-02-18 19:32:40 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.StatusCode != http.StatusBadRequest {
|
|
|
|
t.Errorf("Get: got status %d", r.StatusCode)
|
|
|
|
}
|
|
|
|
}
|