1 // Copyright 2013 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.
5 #include "net/websockets/websocket_handshake_stream_create_helper.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/http/http_request_info.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_response_info.h"
16 #include "net/socket/client_socket_handle.h"
17 #include "net/socket/socket_test_util.h"
18 #include "net/websockets/websocket_basic_handshake_stream.h"
19 #include "net/websockets/websocket_stream.h"
20 #include "net/websockets/websocket_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
27 // This class encapsulates the details of creating a mock ClientSocketHandle.
28 class MockClientSocketHandleFactory
{
30 MockClientSocketHandleFactory()
32 pool_(1, 1, &histograms_
, socket_factory_maker_
.factory()) {}
34 // The created socket expects |expect_written| to be written to the socket,
35 // and will respond with |return_to_read|. The test will fail if the expected
36 // text is not written, or if all the bytes are not read.
37 scoped_ptr
<ClientSocketHandle
> CreateClientSocketHandle(
38 const std::string
& expect_written
,
39 const std::string
& return_to_read
) {
40 socket_factory_maker_
.SetExpectations(expect_written
, return_to_read
);
41 scoped_ptr
<ClientSocketHandle
> socket_handle(new ClientSocketHandle
);
44 scoped_refptr
<MockTransportSocketParams
>(),
49 return socket_handle
.Pass();
53 WebSocketDeterministicMockClientSocketFactoryMaker socket_factory_maker_
;
54 ClientSocketPoolHistograms histograms_
;
55 MockTransportClientSocketPool pool_
;
57 DISALLOW_COPY_AND_ASSIGN(MockClientSocketHandleFactory
);
60 class TestConnectDelegate
: public WebSocketStream::ConnectDelegate
{
62 virtual ~TestConnectDelegate() {}
64 virtual void OnSuccess(scoped_ptr
<WebSocketStream
> stream
) OVERRIDE
{}
65 virtual void OnFailure(const std::string
& failure_message
) OVERRIDE
{}
66 virtual void OnStartOpeningHandshake(
67 scoped_ptr
<WebSocketHandshakeRequestInfo
> request
) OVERRIDE
{}
68 virtual void OnFinishOpeningHandshake(
69 scoped_ptr
<WebSocketHandshakeResponseInfo
> response
) OVERRIDE
{}
72 class WebSocketHandshakeStreamCreateHelperTest
: public ::testing::Test
{
74 scoped_ptr
<WebSocketStream
> CreateAndInitializeStream(
75 const std::string
& socket_url
,
76 const std::string
& socket_path
,
77 const std::vector
<std::string
>& sub_protocols
,
78 const std::string
& origin
,
79 const std::string
& extra_request_headers
,
80 const std::string
& extra_response_headers
) {
81 WebSocketHandshakeStreamCreateHelper
create_helper(&connect_delegate_
,
84 scoped_ptr
<ClientSocketHandle
> socket_handle
=
85 socket_handle_factory_
.CreateClientSocketHandle(
86 WebSocketStandardRequest(
87 socket_path
, origin
, extra_request_headers
),
88 WebSocketStandardResponse(extra_response_headers
));
90 scoped_ptr
<WebSocketHandshakeStreamBase
> handshake(
91 create_helper
.CreateBasicStream(socket_handle
.Pass(), false));
93 // If in future the implementation type returned by CreateBasicStream()
94 // changes, this static_cast will be wrong. However, in that case the test
95 // will fail and AddressSanitizer should identify the issue.
96 static_cast<WebSocketBasicHandshakeStream
*>(handshake
.get())
97 ->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
99 HttpRequestInfo request_info
;
100 request_info
.url
= GURL(socket_url
);
101 request_info
.method
= "GET";
102 request_info
.load_flags
= LOAD_DISABLE_CACHE
| LOAD_DO_NOT_PROMPT_FOR_LOGIN
;
103 int rv
= handshake
->InitializeStream(
104 &request_info
, DEFAULT_PRIORITY
, BoundNetLog(), CompletionCallback());
107 HttpRequestHeaders headers
;
108 headers
.SetHeader("Host", "localhost");
109 headers
.SetHeader("Connection", "Upgrade");
110 headers
.SetHeader("Pragma", "no-cache");
111 headers
.SetHeader("Cache-Control", "no-cache");
112 headers
.SetHeader("Upgrade", "websocket");
113 headers
.SetHeader("Origin", origin
);
114 headers
.SetHeader("Sec-WebSocket-Version", "13");
115 headers
.SetHeader("User-Agent", "");
116 headers
.SetHeader("Accept-Encoding", "gzip,deflate");
117 headers
.SetHeader("Accept-Language", "en-us,fr");
119 HttpResponseInfo response
;
120 TestCompletionCallback dummy
;
122 rv
= handshake
->SendRequest(headers
, &response
, dummy
.callback());
126 rv
= handshake
->ReadResponseHeaders(dummy
.callback());
128 EXPECT_EQ(101, response
.headers
->response_code());
129 EXPECT_TRUE(response
.headers
->HasHeaderValue("Connection", "Upgrade"));
130 EXPECT_TRUE(response
.headers
->HasHeaderValue("Upgrade", "websocket"));
131 return handshake
->Upgrade();
134 MockClientSocketHandleFactory socket_handle_factory_
;
135 TestConnectDelegate connect_delegate_
;
138 // Confirm that the basic case works as expected.
139 TEST_F(WebSocketHandshakeStreamCreateHelperTest
, BasicStream
) {
140 scoped_ptr
<WebSocketStream
> stream
=
141 CreateAndInitializeStream("ws://localhost/", "/",
142 std::vector
<std::string
>(), "http://localhost/",
144 EXPECT_EQ("", stream
->GetExtensions());
145 EXPECT_EQ("", stream
->GetSubProtocol());
148 // Verify that the sub-protocols are passed through.
149 TEST_F(WebSocketHandshakeStreamCreateHelperTest
, SubProtocols
) {
150 std::vector
<std::string
> sub_protocols
;
151 sub_protocols
.push_back("chat");
152 sub_protocols
.push_back("superchat");
153 scoped_ptr
<WebSocketStream
> stream
=
154 CreateAndInitializeStream("ws://localhost/",
158 "Sec-WebSocket-Protocol: chat, superchat\r\n",
159 "Sec-WebSocket-Protocol: superchat\r\n");
160 EXPECT_EQ("superchat", stream
->GetSubProtocol());
163 // Verify that extension name is available. Bad extension names are tested in
164 // websocket_stream_test.cc.
165 TEST_F(WebSocketHandshakeStreamCreateHelperTest
, Extensions
) {
166 scoped_ptr
<WebSocketStream
> stream
= CreateAndInitializeStream(
169 std::vector
<std::string
>(),
172 "Sec-WebSocket-Extensions: permessage-deflate\r\n");
173 EXPECT_EQ("permessage-deflate", stream
->GetExtensions());
176 // Verify that extension parameters are available. Bad parameters are tested in
177 // websocket_stream_test.cc.
178 TEST_F(WebSocketHandshakeStreamCreateHelperTest
, ExtensionParameters
) {
179 scoped_ptr
<WebSocketStream
> stream
= CreateAndInitializeStream(
182 std::vector
<std::string
>(),
185 "Sec-WebSocket-Extensions: permessage-deflate;"
186 " client_max_window_bits=14; server_max_window_bits=14;"
187 " server_no_context_takeover; client_no_context_takeover\r\n");
190 "permessage-deflate;"
191 " client_max_window_bits=14; server_max_window_bits=14;"
192 " server_no_context_takeover; client_no_context_takeover",
193 stream
->GetExtensions());