1 // Copyright 2009 The Go 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.
24 func echoServer(ws *Conn) { io.Copy(ws, ws) }
31 func countServer(ws *Conn) {
34 err := JSON.Receive(ws, &count)
39 count.S = strings.Repeat(count.S, count.N)
40 err = JSON.Send(ws, count)
48 http.Handle("/echo", Handler(echoServer))
49 http.Handle("/count", Handler(countServer))
50 server := httptest.NewServer(nil)
51 serverAddr = server.Listener.Addr().String()
52 log.Print("Test WebSocket server listening on ", serverAddr)
55 func newConfig(t *testing.T, path string) *Config {
56 config, _ := NewConfig(fmt.Sprintf("ws://%s%s", serverAddr, path), "http://localhost")
60 func TestEcho(t *testing.T) {
64 client, err := net.Dial("tcp", serverAddr)
66 t.Fatal("dialing", err)
68 conn, err := NewClient(newConfig(t, "/echo"), client)
70 t.Errorf("WebSocket handshake error: %v", err)
74 msg := []byte("hello, world\n")
75 if _, err := conn.Write(msg); err != nil {
76 t.Errorf("Write: %v", err)
78 var actual_msg = make([]byte, 512)
79 n, err := conn.Read(actual_msg)
81 t.Errorf("Read: %v", err)
83 actual_msg = actual_msg[0:n]
84 if !bytes.Equal(msg, actual_msg) {
85 t.Errorf("Echo: expected %q got %q", msg, actual_msg)
90 func TestAddr(t *testing.T) {
94 client, err := net.Dial("tcp", serverAddr)
96 t.Fatal("dialing", err)
98 conn, err := NewClient(newConfig(t, "/echo"), client)
100 t.Errorf("WebSocket handshake error: %v", err)
104 ra := conn.RemoteAddr().String()
105 if !strings.HasPrefix(ra, "ws://") || !strings.HasSuffix(ra, "/echo") {
106 t.Errorf("Bad remote addr: %v", ra)
108 la := conn.LocalAddr().String()
109 if !strings.HasPrefix(la, "http://") {
110 t.Errorf("Bad local addr: %v", la)
115 func TestCount(t *testing.T) {
119 client, err := net.Dial("tcp", serverAddr)
121 t.Fatal("dialing", err)
123 conn, err := NewClient(newConfig(t, "/count"), client)
125 t.Errorf("WebSocket handshake error: %v", err)
131 if err := JSON.Send(conn, count); err != nil {
132 t.Errorf("Write: %v", err)
134 if err := JSON.Receive(conn, &count); err != nil {
135 t.Errorf("Read: %v", err)
138 t.Errorf("count: expected %d got %d", 1, count.N)
140 if count.S != "hello" {
141 t.Errorf("count: expected %q got %q", "hello", count.S)
143 if err := JSON.Send(conn, count); err != nil {
144 t.Errorf("Write: %v", err)
146 if err := JSON.Receive(conn, &count); err != nil {
147 t.Errorf("Read: %v", err)
150 t.Errorf("count: expected %d got %d", 2, count.N)
152 if count.S != "hellohello" {
153 t.Errorf("count: expected %q got %q", "hellohello", count.S)
158 func TestWithQuery(t *testing.T) {
161 client, err := net.Dial("tcp", serverAddr)
163 t.Fatal("dialing", err)
166 config := newConfig(t, "/echo")
167 config.Location, err = url.ParseRequest(fmt.Sprintf("ws://%s/echo?q=v", serverAddr))
169 t.Fatal("location url", err)
172 ws, err := NewClient(config, client)
174 t.Errorf("WebSocket handshake: %v", err)
180 func TestWithProtocol(t *testing.T) {
183 client, err := net.Dial("tcp", serverAddr)
185 t.Fatal("dialing", err)
188 config := newConfig(t, "/echo")
189 config.Protocol = append(config.Protocol, "test")
191 ws, err := NewClient(config, client)
193 t.Errorf("WebSocket handshake: %v", err)
199 func TestHTTP(t *testing.T) {
202 // If the client did not send a handshake that matches the protocol
203 // specification, the server should abort the WebSocket connection.
204 _, err := http.Get(fmt.Sprintf("http://%s/echo", serverAddr))
206 t.Error("Get: unexpected success")
209 urlerr, ok := err.(*url.Error)
211 t.Errorf("Get: not url.Error %#v", err)
214 if urlerr.Error != io.ErrUnexpectedEOF {
215 t.Errorf("Get: error %#v", err)
220 func TestTrailingSpaces(t *testing.T) {
221 // http://code.google.com/p/go/issues/detail?id=955
222 // The last runs of this create keys with trailing spaces that should not be
223 // generated by the client.
225 config := newConfig(t, "/echo")
226 for i := 0; i < 30; i++ {
228 ws, err := DialConfig(config)
230 t.Errorf("Dial #%d failed: %v", i, err)
237 func TestSmallBuffer(t *testing.T) {
238 // http://code.google.com/p/go/issues/detail?id=1145
239 // Read should be able to handle reading a fragment of a frame.
243 client, err := net.Dial("tcp", serverAddr)
245 t.Fatal("dialing", err)
247 conn, err := NewClient(newConfig(t, "/echo"), client)
249 t.Errorf("WebSocket handshake error: %v", err)
253 msg := []byte("hello, world\n")
254 if _, err := conn.Write(msg); err != nil {
255 t.Errorf("Write: %v", err)
257 var small_msg = make([]byte, 8)
258 n, err := conn.Read(small_msg)
260 t.Errorf("Read: %v", err)
262 if !bytes.Equal(msg[:len(small_msg)], small_msg) {
263 t.Errorf("Echo: expected %q got %q", msg[:len(small_msg)], small_msg)
265 var second_msg = make([]byte, len(msg))
266 n, err = conn.Read(second_msg)
268 t.Errorf("Read: %v", err)
270 second_msg = second_msg[0:n]
271 if !bytes.Equal(msg[len(small_msg):], second_msg) {
272 t.Errorf("Echo: expected %q got %q", msg[len(small_msg):], second_msg)