OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / websocket / websocket_test.go
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.
4
5 package websocket
6
7 import (
8         "bytes"
9         "fmt"
10         "http"
11         "http/httptest"
12         "io"
13         "log"
14         "net"
15         "strings"
16         "sync"
17         "testing"
18         "url"
19 )
20
21 var serverAddr string
22 var once sync.Once
23
24 func echoServer(ws *Conn) { io.Copy(ws, ws) }
25
26 type Count struct {
27         S string
28         N int
29 }
30
31 func countServer(ws *Conn) {
32         for {
33                 var count Count
34                 err := JSON.Receive(ws, &count)
35                 if err != nil {
36                         return
37                 }
38                 count.N++
39                 count.S = strings.Repeat(count.S, count.N)
40                 err = JSON.Send(ws, count)
41                 if err != nil {
42                         return
43                 }
44         }
45 }
46
47 func startServer() {
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)
53 }
54
55 func newConfig(t *testing.T, path string) *Config {
56         config, _ := NewConfig(fmt.Sprintf("ws://%s%s", serverAddr, path), "http://localhost")
57         return config
58 }
59
60 func TestEcho(t *testing.T) {
61         once.Do(startServer)
62
63         // websocket.Dial()
64         client, err := net.Dial("tcp", serverAddr)
65         if err != nil {
66                 t.Fatal("dialing", err)
67         }
68         conn, err := NewClient(newConfig(t, "/echo"), client)
69         if err != nil {
70                 t.Errorf("WebSocket handshake error: %v", err)
71                 return
72         }
73
74         msg := []byte("hello, world\n")
75         if _, err := conn.Write(msg); err != nil {
76                 t.Errorf("Write: %v", err)
77         }
78         var actual_msg = make([]byte, 512)
79         n, err := conn.Read(actual_msg)
80         if err != nil {
81                 t.Errorf("Read: %v", err)
82         }
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)
86         }
87         conn.Close()
88 }
89
90 func TestAddr(t *testing.T) {
91         once.Do(startServer)
92
93         // websocket.Dial()
94         client, err := net.Dial("tcp", serverAddr)
95         if err != nil {
96                 t.Fatal("dialing", err)
97         }
98         conn, err := NewClient(newConfig(t, "/echo"), client)
99         if err != nil {
100                 t.Errorf("WebSocket handshake error: %v", err)
101                 return
102         }
103
104         ra := conn.RemoteAddr().String()
105         if !strings.HasPrefix(ra, "ws://") || !strings.HasSuffix(ra, "/echo") {
106                 t.Errorf("Bad remote addr: %v", ra)
107         }
108         la := conn.LocalAddr().String()
109         if !strings.HasPrefix(la, "http://") {
110                 t.Errorf("Bad local addr: %v", la)
111         }
112         conn.Close()
113 }
114
115 func TestCount(t *testing.T) {
116         once.Do(startServer)
117
118         // websocket.Dial()
119         client, err := net.Dial("tcp", serverAddr)
120         if err != nil {
121                 t.Fatal("dialing", err)
122         }
123         conn, err := NewClient(newConfig(t, "/count"), client)
124         if err != nil {
125                 t.Errorf("WebSocket handshake error: %v", err)
126                 return
127         }
128
129         var count Count
130         count.S = "hello"
131         if err := JSON.Send(conn, count); err != nil {
132                 t.Errorf("Write: %v", err)
133         }
134         if err := JSON.Receive(conn, &count); err != nil {
135                 t.Errorf("Read: %v", err)
136         }
137         if count.N != 1 {
138                 t.Errorf("count: expected %d got %d", 1, count.N)
139         }
140         if count.S != "hello" {
141                 t.Errorf("count: expected %q got %q", "hello", count.S)
142         }
143         if err := JSON.Send(conn, count); err != nil {
144                 t.Errorf("Write: %v", err)
145         }
146         if err := JSON.Receive(conn, &count); err != nil {
147                 t.Errorf("Read: %v", err)
148         }
149         if count.N != 2 {
150                 t.Errorf("count: expected %d got %d", 2, count.N)
151         }
152         if count.S != "hellohello" {
153                 t.Errorf("count: expected %q got %q", "hellohello", count.S)
154         }
155         conn.Close()
156 }
157
158 func TestWithQuery(t *testing.T) {
159         once.Do(startServer)
160
161         client, err := net.Dial("tcp", serverAddr)
162         if err != nil {
163                 t.Fatal("dialing", err)
164         }
165
166         config := newConfig(t, "/echo")
167         config.Location, err = url.ParseRequest(fmt.Sprintf("ws://%s/echo?q=v", serverAddr))
168         if err != nil {
169                 t.Fatal("location url", err)
170         }
171
172         ws, err := NewClient(config, client)
173         if err != nil {
174                 t.Errorf("WebSocket handshake: %v", err)
175                 return
176         }
177         ws.Close()
178 }
179
180 func TestWithProtocol(t *testing.T) {
181         once.Do(startServer)
182
183         client, err := net.Dial("tcp", serverAddr)
184         if err != nil {
185                 t.Fatal("dialing", err)
186         }
187
188         config := newConfig(t, "/echo")
189         config.Protocol = append(config.Protocol, "test")
190
191         ws, err := NewClient(config, client)
192         if err != nil {
193                 t.Errorf("WebSocket handshake: %v", err)
194                 return
195         }
196         ws.Close()
197 }
198
199 func TestHTTP(t *testing.T) {
200         once.Do(startServer)
201
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))
205         if err == nil {
206                 t.Error("Get: unexpected success")
207                 return
208         }
209         urlerr, ok := err.(*url.Error)
210         if !ok {
211                 t.Errorf("Get: not url.Error %#v", err)
212                 return
213         }
214         if urlerr.Error != io.ErrUnexpectedEOF {
215                 t.Errorf("Get: error %#v", err)
216                 return
217         }
218 }
219
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.
224         once.Do(startServer)
225         config := newConfig(t, "/echo")
226         for i := 0; i < 30; i++ {
227                 // body
228                 ws, err := DialConfig(config)
229                 if err != nil {
230                         t.Errorf("Dial #%d failed: %v", i, err)
231                         break
232                 }
233                 ws.Close()
234         }
235 }
236
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.
240         once.Do(startServer)
241
242         // websocket.Dial()
243         client, err := net.Dial("tcp", serverAddr)
244         if err != nil {
245                 t.Fatal("dialing", err)
246         }
247         conn, err := NewClient(newConfig(t, "/echo"), client)
248         if err != nil {
249                 t.Errorf("WebSocket handshake error: %v", err)
250                 return
251         }
252
253         msg := []byte("hello, world\n")
254         if _, err := conn.Write(msg); err != nil {
255                 t.Errorf("Write: %v", err)
256         }
257         var small_msg = make([]byte, 8)
258         n, err := conn.Read(small_msg)
259         if err != nil {
260                 t.Errorf("Read: %v", err)
261         }
262         if !bytes.Equal(msg[:len(small_msg)], small_msg) {
263                 t.Errorf("Echo: expected %q got %q", msg[:len(small_msg)], small_msg)
264         }
265         var second_msg = make([]byte, len(msg))
266         n, err = conn.Read(second_msg)
267         if err != nil {
268                 t.Errorf("Read: %v", err)
269         }
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)
273         }
274         conn.Close()
275 }