1 // Copyright 2011 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.
18 // Test the getChallengeResponse function with values from section
19 // 5.1 of the specification steps 18, 26, and 43 from
20 // http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00
21 func TestHixie76Challenge(t *testing.T) {
22 var part1 uint32 = 777007543
23 var part2 uint32 = 114997259
24 key3 := []byte{0x47, 0x30, 0x22, 0x2D, 0x5A, 0x3F, 0x47, 0x58}
25 expected := []byte("0st3Rl&q-2ZU^weu")
27 response, err := getChallengeResponse(part1, part2, key3)
29 t.Errorf("getChallengeResponse: returned error %v", err)
32 if !bytes.Equal(expected, response) {
33 t.Errorf("getChallengeResponse: expected %q got %q", expected, response)
37 func TestHixie76ClientHandshake(t *testing.T) {
38 b := bytes.NewBuffer([]byte{})
39 bw := bufio.NewWriter(b)
40 br := bufio.NewReader(strings.NewReader(`HTTP/1.1 101 WebSocket Protocol Handshake
43 Sec-WebSocket-Origin: http://example.com
44 Sec-WebSocket-Location: ws://example.com/demo
45 Sec-WebSocket-Protocol: sample
51 config.Location, err = url.ParseRequest("ws://example.com/demo")
53 t.Fatal("location url", err)
55 config.Origin, err = url.ParseRequest("http://example.com")
57 t.Fatal("origin url", err)
59 config.Protocol = append(config.Protocol, "sample")
60 config.Version = ProtocolVersionHixie76
62 config.handshakeData = map[string]string{
63 "key1": "4 @1 46546xW%0l 1 5",
64 "number1": "829309203",
65 "key2": "12998 5 Y3 1 .P00",
66 "number2": "259970620",
69 err = hixie76ClientHandshake(config, br, bw)
71 t.Errorf("handshake failed: %v", err)
73 req, err := http.ReadRequest(bufio.NewReader(b))
75 t.Fatalf("read request: %v", err)
77 if req.Method != "GET" {
78 t.Errorf("request method expected GET, but got %q", req.Method)
80 if req.URL.Path != "/demo" {
81 t.Errorf("request path expected /demo, but got %q", req.URL.Path)
83 if req.Proto != "HTTP/1.1" {
84 t.Errorf("request proto expected HTTP/1.1, but got %q", req.Proto)
86 if req.Host != "example.com" {
87 t.Errorf("request Host expected example.com, but got %v", req.Host)
89 var expectedHeader = map[string]string{
90 "Connection": "Upgrade",
91 "Upgrade": "WebSocket",
92 "Origin": "http://example.com",
93 "Sec-Websocket-Key1": config.handshakeData["key1"],
94 "Sec-Websocket-Key2": config.handshakeData["key2"],
95 "Sec-WebSocket-Protocol": config.Protocol[0],
97 for k, v := range expectedHeader {
98 if req.Header.Get(k) != v {
99 t.Errorf(fmt.Sprintf("%s expected %q but got %q", k, v, req.Header.Get(k)))
104 func TestHixie76ServerHandshake(t *testing.T) {
105 config := new(Config)
106 handshaker := &hixie76ServerHandshaker{Config: config}
107 br := bufio.NewReader(strings.NewReader(`GET /demo HTTP/1.1
110 Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
111 Sec-WebSocket-Protocol: sample
113 Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
114 Origin: http://example.com
117 req, err := http.ReadRequest(br)
119 t.Fatal("request", err)
121 code, err := handshaker.ReadHandshake(br, req)
123 t.Errorf("handshake failed: %v", err)
125 if code != http.StatusSwitchingProtocols {
126 t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
128 b := bytes.NewBuffer([]byte{})
129 bw := bufio.NewWriter(b)
131 err = handshaker.AcceptHandshake(bw)
133 t.Errorf("handshake response failed: %v", err)
135 expectedResponse := strings.Join([]string{
136 "HTTP/1.1 101 WebSocket Protocol Handshake",
137 "Upgrade: WebSocket",
138 "Connection: Upgrade",
139 "Sec-WebSocket-Origin: http://example.com",
140 "Sec-WebSocket-Location: ws://example.com/demo",
141 "Sec-WebSocket-Protocol: sample",
142 "", ""}, "\r\n") + "8jKS'y:G*Co,Wxa-"
143 if b.String() != expectedResponse {
144 t.Errorf("handshake expected %q but got %q", expectedResponse, b.String())
148 func TestHixie76SkipLengthFrame(t *testing.T) {
149 b := []byte{'\x80', '\x01', 'x', 0, 'h', 'e', 'l', 'l', 'o', '\xff'}
150 buf := bytes.NewBuffer(b)
151 br := bufio.NewReader(buf)
152 bw := bufio.NewWriter(buf)
153 config := newConfig(t, "/")
154 ws := newHixieConn(config, bufio.NewReadWriter(br, bw), nil, nil)
155 msg := make([]byte, 5)
156 n, err := ws.Read(msg)
158 t.Errorf("Read: %v", err)
160 if !bytes.Equal(b[4:9], msg[0:n]) {
161 t.Errorf("Read: expected %q got %q", b[4:9], msg[0:n])
165 func TestHixie76SkipNoUTF8Frame(t *testing.T) {
166 b := []byte{'\x01', 'n', '\xff', 0, 'h', 'e', 'l', 'l', 'o', '\xff'}
167 buf := bytes.NewBuffer(b)
168 br := bufio.NewReader(buf)
169 bw := bufio.NewWriter(buf)
170 config := newConfig(t, "/")
171 ws := newHixieConn(config, bufio.NewReadWriter(br, bw), nil, nil)
172 msg := make([]byte, 5)
173 n, err := ws.Read(msg)
175 t.Errorf("Read: %v", err)
177 if !bytes.Equal(b[4:9], msg[0:n]) {
178 t.Errorf("Read: expected %q got %q", b[4:9], msg[0:n])
182 func TestHixie76ClosingFrame(t *testing.T) {
183 b := []byte{0, 'h', 'e', 'l', 'l', 'o', '\xff'}
184 buf := bytes.NewBuffer(b)
185 br := bufio.NewReader(buf)
186 bw := bufio.NewWriter(buf)
187 config := newConfig(t, "/")
188 ws := newHixieConn(config, bufio.NewReadWriter(br, bw), nil, nil)
189 msg := make([]byte, 5)
190 n, err := ws.Read(msg)
192 t.Errorf("read: %v", err)
194 if !bytes.Equal(b[1:6], msg[0:n]) {
195 t.Errorf("Read: expected %q got %q", b[1:6], msg[0:n])
197 n, err = ws.Read(msg)
199 t.Errorf("read: %v", err)