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

Unified Diff: net/websockets/websocket_handshake_draft75_unittest.cc

Issue 1108002: Implement new websocket handshake based on draft-hixie-thewebsocketprotocol-76 (Closed)
Patch Set: fix for review comments Created 10 years, 9 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
« no previous file with comments | « net/websockets/websocket_handshake_draft75.cc ('k') | net/websockets/websocket_handshake_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/websockets/websocket_handshake_draft75_unittest.cc
diff --git a/net/websockets/websocket_handshake_draft75_unittest.cc b/net/websockets/websocket_handshake_draft75_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aff75addb08749913949c6746850f1c3b6091171
--- /dev/null
+++ b/net/websockets/websocket_handshake_draft75_unittest.cc
@@ -0,0 +1,217 @@
+// Copyright (c) 2010 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 <string>
+#include <vector>
+
+#include "base/scoped_ptr.h"
+#include "net/websockets/websocket_handshake_draft75.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/platform_test.h"
+
+namespace net {
+
+TEST(WebSocketHandshakeDraft75Test, Connect) {
+ const std::string kExpectedClientHandshakeMessage =
+ "GET /demo HTTP/1.1\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Host: example.com\r\n"
+ "Origin: http://example.com\r\n"
+ "WebSocket-Protocol: sample\r\n"
+ "\r\n";
+
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
+ EXPECT_EQ(kExpectedClientHandshakeMessage,
+ handshake->CreateClientHandshakeMessage());
+
+ const char kResponse[] = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "WebSocket-Origin: http://example.com\r\n"
+ "WebSocket-Location: ws://example.com/demo\r\n"
+ "WebSocket-Protocol: sample\r\n"
+ "\r\n";
+
+ EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
+ // too short
+ EXPECT_EQ(-1, handshake->ReadServerHandshake(kResponse, 16));
+ EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
+ // only status line
+ EXPECT_EQ(-1, handshake->ReadServerHandshake(
+ kResponse,
+ WebSocketHandshakeDraft75::kServerHandshakeHeaderLength));
+ EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
+ // by upgrade header
+ EXPECT_EQ(-1, handshake->ReadServerHandshake(
+ kResponse,
+ WebSocketHandshakeDraft75::kServerHandshakeHeaderLength +
+ WebSocketHandshakeDraft75::kUpgradeHeaderLength));
+ EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
+ // by connection header
+ EXPECT_EQ(-1, handshake->ReadServerHandshake(
+ kResponse,
+ WebSocketHandshakeDraft75::kServerHandshakeHeaderLength +
+ WebSocketHandshakeDraft75::kUpgradeHeaderLength +
+ WebSocketHandshakeDraft75::kConnectionHeaderLength));
+ EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
+
+ EXPECT_EQ(-1, handshake->ReadServerHandshake(
+ kResponse, sizeof(kResponse) - 2));
+ EXPECT_EQ(WebSocketHandshake::MODE_NORMAL, handshake->mode());
+
+ int handshake_length = strlen(kResponse);
+ EXPECT_EQ(handshake_length, handshake->ReadServerHandshake(
+ kResponse, sizeof(kResponse) - 1)); // -1 for terminating \0
+ EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode());
+}
+
+TEST(WebSocketHandshakeDraft75Test, ServerSentData) {
+ const std::string kExpectedClientHandshakeMessage =
+ "GET /demo HTTP/1.1\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Host: example.com\r\n"
+ "Origin: http://example.com\r\n"
+ "WebSocket-Protocol: sample\r\n"
+ "\r\n";
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ EXPECT_EQ(WebSocketHandshake::MODE_INCOMPLETE, handshake->mode());
+ EXPECT_EQ(kExpectedClientHandshakeMessage,
+ handshake->CreateClientHandshakeMessage());
+
+ const char kResponse[] ="HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "WebSocket-Origin: http://example.com\r\n"
+ "WebSocket-Location: ws://example.com/demo\r\n"
+ "WebSocket-Protocol: sample\r\n"
+ "\r\n"
+ "\0Hello\xff";
+
+ int handshake_length = strlen(kResponse);
+ EXPECT_EQ(handshake_length, handshake->ReadServerHandshake(
+ kResponse, sizeof(kResponse) - 1)); // -1 for terminating \0
+ EXPECT_EQ(WebSocketHandshake::MODE_CONNECTED, handshake->mode());
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_Simple) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com/demo"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ EXPECT_EQ("GET /demo HTTP/1.1\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Host: example.com\r\n"
+ "Origin: http://example.com\r\n"
+ "WebSocket-Protocol: sample\r\n"
+ "\r\n",
+ handshake->CreateClientHandshakeMessage());
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_PathAndQuery) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com/Test?q=xxx&p=%20"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ // Path and query should be preserved as-is.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("GET /Test?q=xxx&p=%20 HTTP/1.1\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_Host) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://Example.Com/demo"),
+ "http://Example.Com",
+ "ws://Example.Com/demo",
+ "sample"));
+ // Host should be lowercased
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com\r\n"));
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Origin: http://example.com\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_TrimPort80) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com:80/demo"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ // :80 should be trimmed as it's the default port for ws://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_TrimPort443) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("wss://example.com:443/demo"),
+ "http://example.com",
+ "wss://example.com/demo",
+ "sample"));
+ // :443 should be trimmed as it's the default port for wss://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test,
+ CreateClientHandshakeMessage_NonDefaultPortForWs) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com:8080/demo"),
+ "http://example.com",
+ "wss://example.com/demo",
+ "sample"));
+ // :8080 should be preserved as it's not the default port for ws://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com:8080\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test,
+ CreateClientHandshakeMessage_NonDefaultPortForWss) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("wss://example.com:4443/demo"),
+ "http://example.com",
+ "wss://example.com/demo",
+ "sample"));
+ // :4443 should be preserved as it's not the default port for wss://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com:4443\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_WsBut443) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("ws://example.com:443/demo"),
+ "http://example.com",
+ "ws://example.com/demo",
+ "sample"));
+ // :443 should be preserved as it's not the default port for ws://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com:443\r\n"));
+}
+
+TEST(WebSocketHandshakeDraft75Test, CreateClientHandshakeMessage_WssBut80) {
+ scoped_ptr<WebSocketHandshakeDraft75> handshake(
+ new WebSocketHandshakeDraft75(GURL("wss://example.com:80/demo"),
+ "http://example.com",
+ "wss://example.com/demo",
+ "sample"));
+ // :80 should be preserved as it's not the default port for wss://.
+ EXPECT_THAT(handshake->CreateClientHandshakeMessage(),
+ testing::HasSubstr("Host: example.com:80\r\n"));
+}
+
+} // namespace net
« no previous file with comments | « net/websockets/websocket_handshake_draft75.cc ('k') | net/websockets/websocket_handshake_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698