Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: net/websockets/websocket_handshake_handler_unittest.cc

Issue 2743003: Add Spdy support in WebSocketHandshake*Handler (Closed)
Patch Set: fix Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/websockets/websocket_handshake_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 "Connection: Upgrade\r\n" 144 "Connection: Upgrade\r\n"
145 "Sec-WebSocket-Origin: http://example.com\r\n" 145 "Sec-WebSocket-Origin: http://example.com\r\n"
146 "Sec-WebSocket-Location: ws://example.com/demo\r\n" 146 "Sec-WebSocket-Location: ws://example.com/demo\r\n"
147 "Sec-WebSocket-Protocol: sample\r\n" 147 "Sec-WebSocket-Protocol: sample\r\n"
148 "\r\n" 148 "\r\n"
149 "8jKS'y:G*Co,Wxa-"; 149 "8jKS'y:G*Co,Wxa-";
150 150
151 EXPECT_EQ(kHandshakeResponseExpectedMessage, handler.GetResponse()); 151 EXPECT_EQ(kHandshakeResponseExpectedMessage, handler.GetResponse());
152 } 152 }
153 153
154 TEST(WebSocketHandshakeHandlerTest, RequestResponse) { 154 TEST(WebSocketHandshakeHandlerTest, HttpRequestResponse) {
155 WebSocketHandshakeRequestHandler request_handler; 155 WebSocketHandshakeRequestHandler request_handler;
156 156
157 static const char* kHandshakeRequestMessage = 157 static const char* kHandshakeRequestMessage =
158 "GET /demo HTTP/1.1\r\n" 158 "GET /demo HTTP/1.1\r\n"
159 "Host: example.com\r\n" 159 "Host: example.com\r\n"
160 "Connection: Upgrade\r\n" 160 "Connection: Upgrade\r\n"
161 "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n" 161 "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
162 "Sec-WebSocket-Protocol: sample\r\n" 162 "Sec-WebSocket-Protocol: sample\r\n"
163 "Upgrade: WebSocket\r\n" 163 "Upgrade: WebSocket\r\n"
164 "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n" 164 "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 "Connection: Upgrade\r\n" 226 "Connection: Upgrade\r\n"
227 "Sec-WebSocket-Origin: http://example.com\r\n" 227 "Sec-WebSocket-Origin: http://example.com\r\n"
228 "Sec-WebSocket-Location: ws://example.com/demo\r\n" 228 "Sec-WebSocket-Location: ws://example.com/demo\r\n"
229 "Sec-WebSocket-Protocol: sample\r\n" 229 "Sec-WebSocket-Protocol: sample\r\n"
230 "\r\n" 230 "\r\n"
231 "8jKS'y:G*Co,Wxa-"; 231 "8jKS'y:G*Co,Wxa-";
232 232
233 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse()); 233 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
234 } 234 }
235 235
236 TEST(WebSocketHandshakeHandlerTest, SpdyRequestResponse) {
237 WebSocketHandshakeRequestHandler request_handler;
238
239 static const char* kHandshakeRequestMessage =
240 "GET /demo HTTP/1.1\r\n"
241 "Host: example.com\r\n"
242 "Connection: Upgrade\r\n"
243 "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
244 "Sec-WebSocket-Protocol: sample\r\n"
245 "Upgrade: WebSocket\r\n"
246 "X-bogus-header: X\r\n"
247 "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
248 "Origin: http://example.com\r\n"
249 "X-bogus-header: Y\r\n"
250 "\r\n"
251 "^n:ds[4U";
252
253 EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
254 strlen(kHandshakeRequestMessage)));
255
256 GURL url("ws://example.com/demo");
257 std::string challenge;
258 spdy::SpdyHeaderBlock headers;
259 ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url, &headers, &challenge));
260
261 EXPECT_EQ(url.spec(), headers["url"]);
262 EXPECT_TRUE(headers.find("upgrade") == headers.end());
263 EXPECT_TRUE(headers.find("Upgrade") == headers.end());
264 EXPECT_TRUE(headers.find("connection") == headers.end());
265 EXPECT_TRUE(headers.find("Connection") == headers.end());
266 EXPECT_TRUE(headers.find("Sec-WebSocket-Key1") == headers.end());
267 EXPECT_TRUE(headers.find("sec-websocket-key1") == headers.end());
268 EXPECT_TRUE(headers.find("Sec-WebSocket-Key2") == headers.end());
269 EXPECT_TRUE(headers.find("sec-websocket-key2") == headers.end());
270 EXPECT_EQ("example.com", headers["host"]);
271 EXPECT_EQ("http://example.com", headers["origin"]);
272 EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
273 const char bogus_header[] = "X\0Y";
274 std::string bogus_header_str(bogus_header, sizeof(bogus_header) - 1);
275 EXPECT_EQ(bogus_header_str, headers["x-bogus-header"]);
276
277 const char expected_challenge[] = "\x31\x6e\x41\x13\x0f\x7e\xd6\x3c^n:ds[4U";
278
279 EXPECT_EQ(expected_challenge, challenge);
280
281 headers.clear();
282
283 headers["sec-websocket-origin"] = "http://example.com";
284 headers["sec-websocket-location"] = "ws://example.com/demo";
285 headers["sec-websocket-protocol"] = "sample";
286
287 WebSocketHandshakeResponseHandler response_handler;
288 EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers, challenge));
289 EXPECT_TRUE(response_handler.HasResponse());
290
291 // Note that order of sec-websocket-* is sensitive with hash_map order.
292 static const char* kHandshakeResponseExpectedMessage =
293 "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
294 "Upgrade: WebSocket\r\n"
295 "Connection: Upgrade\r\n"
296 "sec-websocket-location: ws://example.com/demo\r\n"
Yuta Kitamura 2010/06/29 09:09:08 Why these header names are all lower-case? Is it r
ukai 2010/06/29 09:14:18 These comes from spdy and we'll use lower case fie
Yuta Kitamura 2010/06/29 09:20:33 I know it's valid, but I'm a bit worried about Web
297 "sec-websocket-origin: http://example.com\r\n"
298 "sec-websocket-protocol: sample\r\n"
299 "\r\n"
300 "8jKS'y:G*Co,Wxa-";
301
302 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
303 }
304
305
306 TEST(WebSocketHandshakeHandlerTest, SpdyRequestResponseWithCookies) {
307 WebSocketHandshakeRequestHandler request_handler;
308
309 // Note that websocket won't use multiple headers in request now.
310 static const char* kHandshakeRequestMessage =
311 "GET /demo HTTP/1.1\r\n"
312 "Host: example.com\r\n"
313 "Connection: Upgrade\r\n"
314 "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n"
315 "Sec-WebSocket-Protocol: sample\r\n"
316 "Upgrade: WebSocket\r\n"
317 "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n"
318 "Origin: http://example.com\r\n"
319 "Cookie: WK-websocket-test=1; WK-websocket-test-httponly=1\r\n"
320 "\r\n"
321 "^n:ds[4U";
322
323 EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
324 strlen(kHandshakeRequestMessage)));
325
326 GURL url("ws://example.com/demo");
327 std::string challenge;
328 spdy::SpdyHeaderBlock headers;
329 ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url, &headers, &challenge));
330
331 EXPECT_EQ(url.spec(), headers["url"]);
332 EXPECT_TRUE(headers.find("upgrade") == headers.end());
333 EXPECT_TRUE(headers.find("Upgrade") == headers.end());
334 EXPECT_TRUE(headers.find("connection") == headers.end());
335 EXPECT_TRUE(headers.find("Connection") == headers.end());
336 EXPECT_TRUE(headers.find("Sec-WebSocket-Key1") == headers.end());
337 EXPECT_TRUE(headers.find("sec-websocket-key1") == headers.end());
338 EXPECT_TRUE(headers.find("Sec-WebSocket-Key2") == headers.end());
339 EXPECT_TRUE(headers.find("sec-websocket-key2") == headers.end());
340 EXPECT_EQ("example.com", headers["host"]);
341 EXPECT_EQ("http://example.com", headers["origin"]);
342 EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
343 EXPECT_EQ("WK-websocket-test=1; WK-websocket-test-httponly=1",
344 headers["cookie"]);
345
346 const char expected_challenge[] = "\x31\x6e\x41\x13\x0f\x7e\xd6\x3c^n:ds[4U";
347
348 EXPECT_EQ(expected_challenge, challenge);
349
350 headers.clear();
351
352 headers["sec-websocket-origin"] = "http://example.com";
353 headers["sec-websocket-location"] = "ws://example.com/demo";
354 headers["sec-websocket-protocol"] = "sample";
355 std::string cookie = "WK-websocket-test=1";
356 cookie.append(1, '\0');
357 cookie += "WK-websocket-test-httponly=1; HttpOnly";
358 headers["set-cookie"] = cookie;
359
360 WebSocketHandshakeResponseHandler response_handler;
361 EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers, challenge));
362 EXPECT_TRUE(response_handler.HasResponse());
363
364 // Note that order of sec-websocket-* is sensitive with hash_map order.
365 static const char* kHandshakeResponseExpectedMessage =
366 "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
367 "Upgrade: WebSocket\r\n"
368 "Connection: Upgrade\r\n"
369 "sec-websocket-location: ws://example.com/demo\r\n"
370 "sec-websocket-origin: http://example.com\r\n"
371 "sec-websocket-protocol: sample\r\n"
372 "set-cookie: WK-websocket-test=1\r\n"
373 "set-cookie: WK-websocket-test-httponly=1; HttpOnly\r\n"
374 "\r\n"
375 "8jKS'y:G*Co,Wxa-";
376
377 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
378 }
379
236 } // namespace net 380 } // namespace net
OLDNEW
« no previous file with comments | « net/websockets/websocket_handshake_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698