diff options
author | ricea@chromium.org <ricea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-05 08:30:55 +0000 |
---|---|---|
committer | ricea@chromium.org <ricea@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-05 08:30:55 +0000 |
commit | a31ecc0d319f9490e67319e6bcabb78341e361a4 (patch) | |
tree | 2139216ee00d8a70ddd3791e75d71f7b08febdb3 /net/websockets/websocket_test_util.cc | |
parent | 24d468c358e7579f0a4e905a2fbc1de5a04b08b8 (diff) | |
download | chromium_src-a31ecc0d319f9490e67319e6bcabb78341e361a4.zip chromium_src-a31ecc0d319f9490e67319e6bcabb78341e361a4.tar.gz chromium_src-a31ecc0d319f9490e67319e6bcabb78341e361a4.tar.bz2 |
Add tests for WebSocketHandshakeStreamCreateHelper and WebSocketStream::CreateAndConnectStream().
BUG=265329
TEST=net_unittests --gtest_filter=WebSocket*
Review URL: https://codereview.chromium.org/64133006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238920 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/websockets/websocket_test_util.cc')
-rw-r--r-- | net/websockets/websocket_test_util.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/net/websockets/websocket_test_util.cc b/net/websockets/websocket_test_util.cc index 350a696..55113c6 100644 --- a/net/websockets/websocket_test_util.cc +++ b/net/websockets/websocket_test_util.cc @@ -5,6 +5,8 @@ #include "net/websockets/websocket_test_util.h" #include "base/basictypes.h" +#include "base/strings/stringprintf.h" +#include "net/socket/socket_test_util.h" namespace net { @@ -25,4 +27,99 @@ uint32 LinearCongruentialGenerator::Generate() { return static_cast<uint32>(result >> 16); } +std::string WebSocketStandardRequest(const std::string& path, + const std::string& origin, + const std::string& extra_headers) { + // Unrelated changes in net/http may change the order and default-values of + // HTTP headers, causing WebSocket tests to fail. It is safe to update this + // string in that case. + return base::StringPrintf( + "GET %s HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Origin: %s\r\n" + "Sec-WebSocket-Version: 13\r\n" + "User-Agent:\r\n" + "Accept-Encoding: gzip,deflate\r\n" + "Accept-Language: en-us,fr\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "%s\r\n", + path.c_str(), + origin.c_str(), + extra_headers.c_str()); +} + +std::string WebSocketStandardResponse(const std::string& extra_headers) { + return base::StringPrintf( + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" + "%s\r\n", + extra_headers.c_str()); +} + +struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail { + std::string expect_written; + std::string return_to_read; + MockRead read; + MockWrite write; + scoped_ptr<DeterministicSocketData> data; + DeterministicMockClientSocketFactory factory; +}; + +WebSocketDeterministicMockClientSocketFactoryMaker:: + WebSocketDeterministicMockClientSocketFactoryMaker() + : detail_(new Detail) {} + +WebSocketDeterministicMockClientSocketFactoryMaker:: + ~WebSocketDeterministicMockClientSocketFactoryMaker() {} + +DeterministicMockClientSocketFactory* +WebSocketDeterministicMockClientSocketFactoryMaker::factory() { + return &detail_->factory; +} + +void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations( + const std::string& expect_written, + const std::string& return_to_read) { + // We need to extend the lifetime of these strings. + detail_->expect_written = expect_written; + detail_->return_to_read = return_to_read; + detail_->write = MockWrite(SYNCHRONOUS, 0, detail_->expect_written.c_str()); + detail_->read = MockRead(SYNCHRONOUS, 1, detail_->return_to_read.c_str()); + scoped_ptr<DeterministicSocketData> socket_data( + new DeterministicSocketData(&detail_->read, 1, &detail_->write, 1)); + socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); + socket_data->SetStop(2); + SetRawExpectations(socket_data.Pass()); +} + +void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations( + scoped_ptr<DeterministicSocketData> socket_data) { + detail_->data = socket_data.Pass(); + detail_->factory.AddSocketDataProvider(detail_->data.get()); +} + +WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() + : url_request_context_(true) { + url_request_context_.set_client_socket_factory(maker_.factory()); +} + +WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {} + +void WebSocketTestURLRequestContextHost::SetRawExpectations( + scoped_ptr<DeterministicSocketData> socket_data) { + maker_.SetRawExpectations(socket_data.Pass()); +} + +TestURLRequestContext* +WebSocketTestURLRequestContextHost::GetURLRequestContext() { + url_request_context_.Init(); + // A Network Delegate is required to make the URLRequest::Delegate work. + url_request_context_.set_network_delegate(&network_delegate_); + return &url_request_context_; +} + } // namespace net |