| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/websockets/websocket_handshake_stream_create_helper.h" | 5 #include "net/websockets/websocket_handshake_stream_create_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "net/base/completion_callback.h" | 10 #include "net/base/completion_callback.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks> | 71 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
| 72 ssl_error_callbacks, | 72 ssl_error_callbacks, |
| 73 const SSLInfo& ssl_info, | 73 const SSLInfo& ssl_info, |
| 74 bool fatal) override {} | 74 bool fatal) override {} |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 class WebSocketHandshakeStreamCreateHelperTest : public ::testing::Test { | 77 class WebSocketHandshakeStreamCreateHelperTest : public ::testing::Test { |
| 78 protected: | 78 protected: |
| 79 scoped_ptr<WebSocketStream> CreateAndInitializeStream( | 79 scoped_ptr<WebSocketStream> CreateAndInitializeStream( |
| 80 const std::string& socket_url, | 80 const std::string& socket_url, |
| 81 const std::string& socket_host, |
| 81 const std::string& socket_path, | 82 const std::string& socket_path, |
| 82 const std::vector<std::string>& sub_protocols, | 83 const std::vector<std::string>& sub_protocols, |
| 83 const std::string& origin, | 84 const std::string& origin, |
| 84 const std::string& extra_request_headers, | 85 const std::string& extra_request_headers, |
| 85 const std::string& extra_response_headers) { | 86 const std::string& extra_response_headers) { |
| 86 WebSocketHandshakeStreamCreateHelper create_helper(&connect_delegate_, | 87 WebSocketHandshakeStreamCreateHelper create_helper(&connect_delegate_, |
| 87 sub_protocols); | 88 sub_protocols); |
| 88 create_helper.set_failure_message(&failure_message_); | 89 create_helper.set_failure_message(&failure_message_); |
| 89 | 90 |
| 90 scoped_ptr<ClientSocketHandle> socket_handle = | 91 scoped_ptr<ClientSocketHandle> socket_handle = |
| 91 socket_handle_factory_.CreateClientSocketHandle( | 92 socket_handle_factory_.CreateClientSocketHandle( |
| 92 WebSocketStandardRequest( | 93 WebSocketStandardRequest(socket_path, socket_host, origin, |
| 93 socket_path, origin, extra_request_headers), | 94 extra_request_headers), |
| 94 WebSocketStandardResponse(extra_response_headers)); | 95 WebSocketStandardResponse(extra_response_headers)); |
| 95 | 96 |
| 96 scoped_ptr<WebSocketHandshakeStreamBase> handshake( | 97 scoped_ptr<WebSocketHandshakeStreamBase> handshake( |
| 97 create_helper.CreateBasicStream(socket_handle.Pass(), false)); | 98 create_helper.CreateBasicStream(socket_handle.Pass(), false)); |
| 98 | 99 |
| 99 // If in future the implementation type returned by CreateBasicStream() | 100 // If in future the implementation type returned by CreateBasicStream() |
| 100 // changes, this static_cast will be wrong. However, in that case the test | 101 // changes, this static_cast will be wrong. However, in that case the test |
| 101 // will fail and AddressSanitizer should identify the issue. | 102 // will fail and AddressSanitizer should identify the issue. |
| 102 static_cast<WebSocketBasicHandshakeStream*>(handshake.get()) | 103 static_cast<WebSocketBasicHandshakeStream*>(handshake.get()) |
| 103 ->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ=="); | 104 ->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ=="); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 return handshake->Upgrade(); | 138 return handshake->Upgrade(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 MockClientSocketHandleFactory socket_handle_factory_; | 141 MockClientSocketHandleFactory socket_handle_factory_; |
| 141 TestConnectDelegate connect_delegate_; | 142 TestConnectDelegate connect_delegate_; |
| 142 std::string failure_message_; | 143 std::string failure_message_; |
| 143 }; | 144 }; |
| 144 | 145 |
| 145 // Confirm that the basic case works as expected. | 146 // Confirm that the basic case works as expected. |
| 146 TEST_F(WebSocketHandshakeStreamCreateHelperTest, BasicStream) { | 147 TEST_F(WebSocketHandshakeStreamCreateHelperTest, BasicStream) { |
| 147 scoped_ptr<WebSocketStream> stream = | 148 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( |
| 148 CreateAndInitializeStream("ws://localhost/", "/", | 149 "ws://localhost/", "localhost", "/", std::vector<std::string>(), |
| 149 std::vector<std::string>(), "http://localhost/", | 150 "http://localhost/", "", ""); |
| 150 "", ""); | |
| 151 EXPECT_EQ("", stream->GetExtensions()); | 151 EXPECT_EQ("", stream->GetExtensions()); |
| 152 EXPECT_EQ("", stream->GetSubProtocol()); | 152 EXPECT_EQ("", stream->GetSubProtocol()); |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Verify that the sub-protocols are passed through. | 155 // Verify that the sub-protocols are passed through. |
| 156 TEST_F(WebSocketHandshakeStreamCreateHelperTest, SubProtocols) { | 156 TEST_F(WebSocketHandshakeStreamCreateHelperTest, SubProtocols) { |
| 157 std::vector<std::string> sub_protocols; | 157 std::vector<std::string> sub_protocols; |
| 158 sub_protocols.push_back("chat"); | 158 sub_protocols.push_back("chat"); |
| 159 sub_protocols.push_back("superchat"); | 159 sub_protocols.push_back("superchat"); |
| 160 scoped_ptr<WebSocketStream> stream = | 160 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( |
| 161 CreateAndInitializeStream("ws://localhost/", | 161 "ws://localhost/", "localhost", "/", sub_protocols, "http://localhost/", |
| 162 "/", | 162 "Sec-WebSocket-Protocol: chat, superchat\r\n", |
| 163 sub_protocols, | 163 "Sec-WebSocket-Protocol: superchat\r\n"); |
| 164 "http://localhost/", | |
| 165 "Sec-WebSocket-Protocol: chat, superchat\r\n", | |
| 166 "Sec-WebSocket-Protocol: superchat\r\n"); | |
| 167 EXPECT_EQ("superchat", stream->GetSubProtocol()); | 164 EXPECT_EQ("superchat", stream->GetSubProtocol()); |
| 168 } | 165 } |
| 169 | 166 |
| 170 // Verify that extension name is available. Bad extension names are tested in | 167 // Verify that extension name is available. Bad extension names are tested in |
| 171 // websocket_stream_test.cc. | 168 // websocket_stream_test.cc. |
| 172 TEST_F(WebSocketHandshakeStreamCreateHelperTest, Extensions) { | 169 TEST_F(WebSocketHandshakeStreamCreateHelperTest, Extensions) { |
| 173 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( | 170 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( |
| 174 "ws://localhost/", | 171 "ws://localhost/", "localhost", "/", std::vector<std::string>(), |
| 175 "/", | 172 "http://localhost/", "", |
| 176 std::vector<std::string>(), | |
| 177 "http://localhost/", | |
| 178 "", | |
| 179 "Sec-WebSocket-Extensions: permessage-deflate\r\n"); | 173 "Sec-WebSocket-Extensions: permessage-deflate\r\n"); |
| 180 EXPECT_EQ("permessage-deflate", stream->GetExtensions()); | 174 EXPECT_EQ("permessage-deflate", stream->GetExtensions()); |
| 181 } | 175 } |
| 182 | 176 |
| 183 // Verify that extension parameters are available. Bad parameters are tested in | 177 // Verify that extension parameters are available. Bad parameters are tested in |
| 184 // websocket_stream_test.cc. | 178 // websocket_stream_test.cc. |
| 185 TEST_F(WebSocketHandshakeStreamCreateHelperTest, ExtensionParameters) { | 179 TEST_F(WebSocketHandshakeStreamCreateHelperTest, ExtensionParameters) { |
| 186 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( | 180 scoped_ptr<WebSocketStream> stream = CreateAndInitializeStream( |
| 187 "ws://localhost/", | 181 "ws://localhost/", "localhost", "/", std::vector<std::string>(), |
| 188 "/", | 182 "http://localhost/", "", |
| 189 std::vector<std::string>(), | |
| 190 "http://localhost/", | |
| 191 "", | |
| 192 "Sec-WebSocket-Extensions: permessage-deflate;" | 183 "Sec-WebSocket-Extensions: permessage-deflate;" |
| 193 " client_max_window_bits=14; server_max_window_bits=14;" | 184 " client_max_window_bits=14; server_max_window_bits=14;" |
| 194 " server_no_context_takeover; client_no_context_takeover\r\n"); | 185 " server_no_context_takeover; client_no_context_takeover\r\n"); |
| 195 | 186 |
| 196 EXPECT_EQ( | 187 EXPECT_EQ( |
| 197 "permessage-deflate;" | 188 "permessage-deflate;" |
| 198 " client_max_window_bits=14; server_max_window_bits=14;" | 189 " client_max_window_bits=14; server_max_window_bits=14;" |
| 199 " server_no_context_takeover; client_no_context_takeover", | 190 " server_no_context_takeover; client_no_context_takeover", |
| 200 stream->GetExtensions()); | 191 stream->GetExtensions()); |
| 201 } | 192 } |
| 202 | 193 |
| 203 } // namespace | 194 } // namespace |
| 204 } // namespace net | 195 } // namespace net |
| OLD | NEW |