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

Unified Diff: net/websockets/websocket_handshake_handler_spdy2_unittest.cc

Issue 10843050: WebSocket over SPDY: handshake support for both of SPDY/2 and SPDY/3 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move spdy related tests to new files Created 8 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 side-by-side diff with in-line comments
Download patch
Index: net/websockets/websocket_handshake_handler_spdy2_unittest.cc
diff --git a/net/websockets/websocket_handshake_handler_spdy2_unittest.cc b/net/websockets/websocket_handshake_handler_spdy2_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..586e3d55c36830a74b91d8421d86f63e3d3fa894
--- /dev/null
+++ b/net/websockets/websocket_handshake_handler_spdy2_unittest.cc
@@ -0,0 +1,179 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/websockets/websocket_handshake_handler.h"
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/string_util.h"
+#include "googleurl/src/gurl.h"
+#include "net/http/http_response_headers.h"
+#include "net/http/http_util.h"
+
Yuta Kitamura 2012/08/03 07:31:51 nit: This blank line is not needed.
Takashi Toyoshima 2012/08/06 13:01:53 Done.
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace net {
+
+TEST(WebSocketHandshakeHandlerSpdy2Test, RequestResponse) {
+ WebSocketHandshakeRequestHandler request_handler;
+
+ static const char kHandshakeRequestMessage[] =
+ "GET /demo HTTP/1.1\r\n"
+ "Host: example.com\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "Origin: http://example.com\r\n"
+ "Sec-WebSocket-Protocol: sample\r\n"
+ "Sec-WebSocket-Extensions: foo\r\n"
+ "Sec-WebSocket-Version: 13\r\n"
+ "\r\n";
+
+ EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
+ strlen(kHandshakeRequestMessage)));
+ EXPECT_EQ(13, request_handler.protocol_version());
+
+ GURL url("ws://example.com/demo");
+ std::string challenge;
+ SpdyHeaderBlock headers;
+ ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url,
+ &headers,
+ &challenge,
+ kProtoSPDY2));
+
+ EXPECT_EQ(url.path(), headers["path"]);
+ EXPECT_TRUE(headers.find("upgrade") == headers.end());
+ EXPECT_TRUE(headers.find("Upgrade") == headers.end());
+ EXPECT_TRUE(headers.find("connection") == headers.end());
+ EXPECT_TRUE(headers.find("Connection") == headers.end());
+ EXPECT_TRUE(headers.find("Sec-WebSocket-Key") == headers.end());
+ EXPECT_TRUE(headers.find("sec-websocket-key") == headers.end());
+ EXPECT_TRUE(headers.find("Sec-WebSocket-Version") == headers.end());
+ EXPECT_TRUE(headers.find("sec-webSocket-version") == headers.end());
+ EXPECT_EQ("example.com", headers["host"]);
+ EXPECT_EQ("http://example.com", headers["origin"]);
+ EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
+ EXPECT_EQ("foo", headers["sec-websocket-extensions"]);
+ EXPECT_EQ("ws", headers["scheme"]);
+ EXPECT_EQ("WebSocket/13", headers["version"]);
+
+ const char expected_challenge[] = "dGhlIHNhbXBsZSBub25jZQ==";
Yuta Kitamura 2012/08/03 07:31:51 static
Takashi Toyoshima 2012/08/06 13:01:53 Done.
+
+ EXPECT_EQ(expected_challenge, challenge);
+
+ headers.clear();
+
+ headers["status"] = "101 Switching Protocols";
+ headers["sec-websocket-protocol"] = "sample";
+ headers["sec-websocket-extensions"] = "foo";
+
+ WebSocketHandshakeResponseHandler response_handler;
+ response_handler.set_protocol_version(13);
+ EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers,
+ challenge,
+ kProtoSPDY2));
+ EXPECT_TRUE(response_handler.HasResponse());
+
+ // Note that order of sec-websocket-* is sensitive with hash_map order.
+ static const char kHandshakeResponseExpectedMessage[] =
+ "HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
+ "sec-websocket-extensions: foo\r\n"
+ "sec-websocket-protocol: sample\r\n"
+ "\r\n";
+
+ EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
+}
+
+TEST(WebSocketHandshakeHandlerSpdy2Test, RequestResponseWithCookies) {
+ WebSocketHandshakeRequestHandler request_handler;
+
+ // Note that websocket won't use multiple headers in request now.
+ static const char kHandshakeRequestMessage[] =
+ "GET /demo HTTP/1.1\r\n"
+ "Host: example.com\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "Origin: http://example.com\r\n"
+ "Sec-WebSocket-Protocol: sample\r\n"
+ "Sec-WebSocket-Extensions: foo\r\n"
+ "Sec-WebSocket-Version: 13\r\n"
+ "Cookie: WK-websocket-test=1; WK-websocket-test-httponly=1\r\n"
+ "\r\n";
+
+ EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
+ strlen(kHandshakeRequestMessage)));
+ EXPECT_EQ(13, request_handler.protocol_version());
+
+ GURL url("ws://example.com/demo");
+ std::string challenge;
+ SpdyHeaderBlock headers;
+ ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url,
+ &headers,
+ &challenge,
+ kProtoSPDY2));
+
+ EXPECT_EQ(url.path(), headers["path"]);
+ EXPECT_TRUE(headers.find("upgrade") == headers.end());
+ EXPECT_TRUE(headers.find("Upgrade") == headers.end());
+ EXPECT_TRUE(headers.find("connection") == headers.end());
+ EXPECT_TRUE(headers.find("Connection") == headers.end());
+ EXPECT_TRUE(headers.find("Sec-WebSocket-Key") == headers.end());
+ EXPECT_TRUE(headers.find("sec-websocket-key") == headers.end());
+ EXPECT_TRUE(headers.find("Sec-WebSocket-Version") == headers.end());
+ EXPECT_TRUE(headers.find("sec-webSocket-version") == headers.end());
+ EXPECT_EQ("example.com", headers["host"]);
+ EXPECT_EQ("http://example.com", headers["origin"]);
+ EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
+ EXPECT_EQ("foo", headers["sec-websocket-extensions"]);
+ EXPECT_EQ("ws", headers["scheme"]);
+ EXPECT_EQ("WebSocket/13", headers["version"]);
+ EXPECT_EQ("WK-websocket-test=1; WK-websocket-test-httponly=1",
+ headers["cookie"]);
+
+ const char expected_challenge[] = "dGhlIHNhbXBsZSBub25jZQ==";
+
+ EXPECT_EQ(expected_challenge, challenge);
+
+ headers.clear();
+
+ headers["status"] = "101 Switching Protocols";
+ headers["sec-websocket-protocol"] = "sample";
+ headers["sec-websocket-extensions"] = "foo";
+ std::string cookie = "WK-websocket-test=1";
+ cookie.append(1, '\0');
+ cookie += "WK-websocket-test-httponly=1; HttpOnly";
+ headers["set-cookie"] = cookie;
+
+
+ WebSocketHandshakeResponseHandler response_handler;
+ response_handler.set_protocol_version(13);
+ EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers,
+ challenge,
+ kProtoSPDY2));
+ EXPECT_TRUE(response_handler.HasResponse());
+
+ // Note that order of sec-websocket-* is sensitive with hash_map order.
+ static const char kHandshakeResponseExpectedMessage[] =
+ "HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
+ "sec-websocket-extensions: foo\r\n"
+ "sec-websocket-protocol: sample\r\n"
+ "set-cookie: WK-websocket-test=1\r\n"
+ "set-cookie: WK-websocket-test-httponly=1; HttpOnly\r\n"
+ "\r\n";
+
+ EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698