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

Side by Side 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, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/websockets/websocket_handshake_handler.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/string_util.h"
12 #include "googleurl/src/gurl.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/http/http_util.h"
15
Yuta Kitamura 2012/08/03 07:31:51 nit: This blank line is not needed.
Takashi Toyoshima 2012/08/06 13:01:53 Done.
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/platform_test.h"
19
20 namespace net {
21
22 TEST(WebSocketHandshakeHandlerSpdy2Test, RequestResponse) {
23 WebSocketHandshakeRequestHandler request_handler;
24
25 static const char kHandshakeRequestMessage[] =
26 "GET /demo HTTP/1.1\r\n"
27 "Host: example.com\r\n"
28 "Upgrade: websocket\r\n"
29 "Connection: Upgrade\r\n"
30 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
31 "Origin: http://example.com\r\n"
32 "Sec-WebSocket-Protocol: sample\r\n"
33 "Sec-WebSocket-Extensions: foo\r\n"
34 "Sec-WebSocket-Version: 13\r\n"
35 "\r\n";
36
37 EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
38 strlen(kHandshakeRequestMessage)));
39 EXPECT_EQ(13, request_handler.protocol_version());
40
41 GURL url("ws://example.com/demo");
42 std::string challenge;
43 SpdyHeaderBlock headers;
44 ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url,
45 &headers,
46 &challenge,
47 kProtoSPDY2));
48
49 EXPECT_EQ(url.path(), headers["path"]);
50 EXPECT_TRUE(headers.find("upgrade") == headers.end());
51 EXPECT_TRUE(headers.find("Upgrade") == headers.end());
52 EXPECT_TRUE(headers.find("connection") == headers.end());
53 EXPECT_TRUE(headers.find("Connection") == headers.end());
54 EXPECT_TRUE(headers.find("Sec-WebSocket-Key") == headers.end());
55 EXPECT_TRUE(headers.find("sec-websocket-key") == headers.end());
56 EXPECT_TRUE(headers.find("Sec-WebSocket-Version") == headers.end());
57 EXPECT_TRUE(headers.find("sec-webSocket-version") == headers.end());
58 EXPECT_EQ("example.com", headers["host"]);
59 EXPECT_EQ("http://example.com", headers["origin"]);
60 EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
61 EXPECT_EQ("foo", headers["sec-websocket-extensions"]);
62 EXPECT_EQ("ws", headers["scheme"]);
63 EXPECT_EQ("WebSocket/13", headers["version"]);
64
65 const char expected_challenge[] = "dGhlIHNhbXBsZSBub25jZQ==";
Yuta Kitamura 2012/08/03 07:31:51 static
Takashi Toyoshima 2012/08/06 13:01:53 Done.
66
67 EXPECT_EQ(expected_challenge, challenge);
68
69 headers.clear();
70
71 headers["status"] = "101 Switching Protocols";
72 headers["sec-websocket-protocol"] = "sample";
73 headers["sec-websocket-extensions"] = "foo";
74
75 WebSocketHandshakeResponseHandler response_handler;
76 response_handler.set_protocol_version(13);
77 EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers,
78 challenge,
79 kProtoSPDY2));
80 EXPECT_TRUE(response_handler.HasResponse());
81
82 // Note that order of sec-websocket-* is sensitive with hash_map order.
83 static const char kHandshakeResponseExpectedMessage[] =
84 "HTTP/1.1 101 Switching Protocols\r\n"
85 "Upgrade: websocket\r\n"
86 "Connection: Upgrade\r\n"
87 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
88 "sec-websocket-extensions: foo\r\n"
89 "sec-websocket-protocol: sample\r\n"
90 "\r\n";
91
92 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
93 }
94
95 TEST(WebSocketHandshakeHandlerSpdy2Test, RequestResponseWithCookies) {
96 WebSocketHandshakeRequestHandler request_handler;
97
98 // Note that websocket won't use multiple headers in request now.
99 static const char kHandshakeRequestMessage[] =
100 "GET /demo HTTP/1.1\r\n"
101 "Host: example.com\r\n"
102 "Upgrade: websocket\r\n"
103 "Connection: Upgrade\r\n"
104 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
105 "Origin: http://example.com\r\n"
106 "Sec-WebSocket-Protocol: sample\r\n"
107 "Sec-WebSocket-Extensions: foo\r\n"
108 "Sec-WebSocket-Version: 13\r\n"
109 "Cookie: WK-websocket-test=1; WK-websocket-test-httponly=1\r\n"
110 "\r\n";
111
112 EXPECT_TRUE(request_handler.ParseRequest(kHandshakeRequestMessage,
113 strlen(kHandshakeRequestMessage)));
114 EXPECT_EQ(13, request_handler.protocol_version());
115
116 GURL url("ws://example.com/demo");
117 std::string challenge;
118 SpdyHeaderBlock headers;
119 ASSERT_TRUE(request_handler.GetRequestHeaderBlock(url,
120 &headers,
121 &challenge,
122 kProtoSPDY2));
123
124 EXPECT_EQ(url.path(), headers["path"]);
125 EXPECT_TRUE(headers.find("upgrade") == headers.end());
126 EXPECT_TRUE(headers.find("Upgrade") == headers.end());
127 EXPECT_TRUE(headers.find("connection") == headers.end());
128 EXPECT_TRUE(headers.find("Connection") == headers.end());
129 EXPECT_TRUE(headers.find("Sec-WebSocket-Key") == headers.end());
130 EXPECT_TRUE(headers.find("sec-websocket-key") == headers.end());
131 EXPECT_TRUE(headers.find("Sec-WebSocket-Version") == headers.end());
132 EXPECT_TRUE(headers.find("sec-webSocket-version") == headers.end());
133 EXPECT_EQ("example.com", headers["host"]);
134 EXPECT_EQ("http://example.com", headers["origin"]);
135 EXPECT_EQ("sample", headers["sec-websocket-protocol"]);
136 EXPECT_EQ("foo", headers["sec-websocket-extensions"]);
137 EXPECT_EQ("ws", headers["scheme"]);
138 EXPECT_EQ("WebSocket/13", headers["version"]);
139 EXPECT_EQ("WK-websocket-test=1; WK-websocket-test-httponly=1",
140 headers["cookie"]);
141
142 const char expected_challenge[] = "dGhlIHNhbXBsZSBub25jZQ==";
143
144 EXPECT_EQ(expected_challenge, challenge);
145
146 headers.clear();
147
148 headers["status"] = "101 Switching Protocols";
149 headers["sec-websocket-protocol"] = "sample";
150 headers["sec-websocket-extensions"] = "foo";
151 std::string cookie = "WK-websocket-test=1";
152 cookie.append(1, '\0');
153 cookie += "WK-websocket-test-httponly=1; HttpOnly";
154 headers["set-cookie"] = cookie;
155
156
157 WebSocketHandshakeResponseHandler response_handler;
158 response_handler.set_protocol_version(13);
159 EXPECT_TRUE(response_handler.ParseResponseHeaderBlock(headers,
160 challenge,
161 kProtoSPDY2));
162 EXPECT_TRUE(response_handler.HasResponse());
163
164 // Note that order of sec-websocket-* is sensitive with hash_map order.
165 static const char kHandshakeResponseExpectedMessage[] =
166 "HTTP/1.1 101 Switching Protocols\r\n"
167 "Upgrade: websocket\r\n"
168 "Connection: Upgrade\r\n"
169 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
170 "sec-websocket-extensions: foo\r\n"
171 "sec-websocket-protocol: sample\r\n"
172 "set-cookie: WK-websocket-test=1\r\n"
173 "set-cookie: WK-websocket-test-httponly=1; HttpOnly\r\n"
174 "\r\n";
175
176 EXPECT_EQ(kHandshakeResponseExpectedMessage, response_handler.GetResponse());
177 }
178
179 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698