Roll src/third_party/skia 7b05ff1:dab1f60
[chromium-blink-merge.git] / net / websockets / websocket_handshake_stream_create_helper_test.cc
blob458ea44a65c3c036d4626f143204acada9419cee
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"
7 #include <string>
8 #include <vector>
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"
22 #include "url/gurl.h"
24 namespace net {
25 namespace {
27 // This class encapsulates the details of creating a mock ClientSocketHandle.
28 class MockClientSocketHandleFactory {
29 public:
30 MockClientSocketHandleFactory()
31 : pool_(1, 1, socket_factory_maker_.factory()) {}
33 // The created socket expects |expect_written| to be written to the socket,
34 // and will respond with |return_to_read|. The test will fail if the expected
35 // text is not written, or if all the bytes are not read.
36 scoped_ptr<ClientSocketHandle> CreateClientSocketHandle(
37 const std::string& expect_written,
38 const std::string& return_to_read) {
39 socket_factory_maker_.SetExpectations(expect_written, return_to_read);
40 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
41 socket_handle->Init(
42 "a",
43 scoped_refptr<MockTransportSocketParams>(),
44 MEDIUM,
45 CompletionCallback(),
46 &pool_,
47 BoundNetLog());
48 return socket_handle.Pass();
51 private:
52 WebSocketMockClientSocketFactoryMaker socket_factory_maker_;
53 MockTransportClientSocketPool pool_;
55 DISALLOW_COPY_AND_ASSIGN(MockClientSocketHandleFactory);
58 class TestConnectDelegate : public WebSocketStream::ConnectDelegate {
59 public:
60 ~TestConnectDelegate() override {}
62 void OnSuccess(scoped_ptr<WebSocketStream> stream) override {}
63 void OnFailure(const std::string& failure_message) override {}
64 void OnStartOpeningHandshake(
65 scoped_ptr<WebSocketHandshakeRequestInfo> request) override {}
66 void OnFinishOpeningHandshake(
67 scoped_ptr<WebSocketHandshakeResponseInfo> response) override {}
68 void OnSSLCertificateError(
69 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks>
70 ssl_error_callbacks,
71 const SSLInfo& ssl_info,
72 bool fatal) override {}
75 class WebSocketHandshakeStreamCreateHelperTest : public ::testing::Test {
76 protected:
77 scoped_ptr<WebSocketStream> CreateAndInitializeStream(
78 const std::string& socket_url,
79 const std::string& socket_host,
80 const std::string& socket_path,
81 const std::vector<std::string>& sub_protocols,
82 const std::string& origin,
83 const std::string& extra_request_headers,
84 const std::string& extra_response_headers) {
85 WebSocketHandshakeStreamCreateHelper create_helper(&connect_delegate_,
86 sub_protocols);
87 create_helper.set_failure_message(&failure_message_);
89 scoped_ptr<ClientSocketHandle> socket_handle =
90 socket_handle_factory_.CreateClientSocketHandle(
91 WebSocketStandardRequest(socket_path, socket_host, origin,
92 extra_request_headers),
93 WebSocketStandardResponse(extra_response_headers));
95 scoped_ptr<WebSocketHandshakeStreamBase> handshake(
96 create_helper.CreateBasicStream(socket_handle.Pass(), false));
98 // If in future the implementation type returned by CreateBasicStream()
99 // changes, this static_cast will be wrong. However, in that case the test
100 // will fail and AddressSanitizer should identify the issue.
101 static_cast<WebSocketBasicHandshakeStream*>(handshake.get())
102 ->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
104 HttpRequestInfo request_info;
105 request_info.url = GURL(socket_url);
106 request_info.method = "GET";
107 request_info.load_flags = LOAD_DISABLE_CACHE;
108 int rv = handshake->InitializeStream(
109 &request_info, DEFAULT_PRIORITY, BoundNetLog(), CompletionCallback());
110 EXPECT_EQ(OK, rv);
112 HttpRequestHeaders headers;
113 headers.SetHeader("Host", "localhost");
114 headers.SetHeader("Connection", "Upgrade");
115 headers.SetHeader("Pragma", "no-cache");
116 headers.SetHeader("Cache-Control", "no-cache");
117 headers.SetHeader("Upgrade", "websocket");
118 headers.SetHeader("Origin", origin);
119 headers.SetHeader("Sec-WebSocket-Version", "13");
120 headers.SetHeader("User-Agent", "");
121 headers.SetHeader("Accept-Encoding", "gzip, deflate");
122 headers.SetHeader("Accept-Language", "en-us,fr");
124 HttpResponseInfo response;
125 TestCompletionCallback dummy;
127 rv = handshake->SendRequest(headers, &response, dummy.callback());
129 EXPECT_EQ(OK, rv);
131 rv = handshake->ReadResponseHeaders(dummy.callback());
132 EXPECT_EQ(OK, rv);
133 EXPECT_EQ(101, response.headers->response_code());
134 EXPECT_TRUE(response.headers->HasHeaderValue("Connection", "Upgrade"));
135 EXPECT_TRUE(response.headers->HasHeaderValue("Upgrade", "websocket"));
136 return handshake->Upgrade();
139 MockClientSocketHandleFactory socket_handle_factory_;
140 TestConnectDelegate connect_delegate_;
141 std::string failure_message_;
144 // Confirm that the basic case works as expected.
145 TEST_F(WebSocketHandshakeStreamCreateHelperTest, BasicStream) {
146 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
147 "ws://localhost/", "localhost", "/", std::vector<std::string>(),
148 "http://localhost/", "", "");
149 EXPECT_EQ("", stream->GetExtensions());
150 EXPECT_EQ("", stream->GetSubProtocol());
153 // Verify that the sub-protocols are passed through.
154 TEST_F(WebSocketHandshakeStreamCreateHelperTest, SubProtocols) {
155 std::vector<std::string> sub_protocols;
156 sub_protocols.push_back("chat");
157 sub_protocols.push_back("superchat");
158 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
159 "ws://localhost/", "localhost", "/", sub_protocols, "http://localhost/",
160 "Sec-WebSocket-Protocol: chat, superchat\r\n",
161 "Sec-WebSocket-Protocol: superchat\r\n");
162 EXPECT_EQ("superchat", stream->GetSubProtocol());
165 // Verify that extension name is available. Bad extension names are tested in
166 // websocket_stream_test.cc.
167 TEST_F(WebSocketHandshakeStreamCreateHelperTest, Extensions) {
168 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
169 "ws://localhost/", "localhost", "/", std::vector<std::string>(),
170 "http://localhost/", "",
171 "Sec-WebSocket-Extensions: permessage-deflate\r\n");
172 EXPECT_EQ("permessage-deflate", stream->GetExtensions());
175 // Verify that extension parameters are available. Bad parameters are tested in
176 // websocket_stream_test.cc.
177 TEST_F(WebSocketHandshakeStreamCreateHelperTest, ExtensionParameters) {
178 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream(
179 "ws://localhost/", "localhost", "/", std::vector<std::string>(),
180 "http://localhost/", "",
181 "Sec-WebSocket-Extensions: permessage-deflate;"
182 " client_max_window_bits=14; server_max_window_bits=14;"
183 " server_no_context_takeover; client_no_context_takeover\r\n");
185 EXPECT_EQ(
186 "permessage-deflate;"
187 " client_max_window_bits=14; server_max_window_bits=14;"
188 " server_no_context_takeover; client_no_context_takeover",
189 stream->GetExtensions());
192 } // namespace
193 } // namespace net