[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / net / websockets / websocket_test_util.cc
blob5396020b1375c6c445f00beb44fcec5729636335
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_test_util.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/stl_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "net/proxy/proxy_service.h"
15 #include "net/socket/socket_test_util.h"
17 namespace net {
19 namespace {
20 const uint64 kA =
21 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
22 const uint64 kC = 12345;
23 const uint64 kM = static_cast<uint64>(1) << 48;
25 } // namespace
27 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
28 : current_(seed) {}
30 uint32 LinearCongruentialGenerator::Generate() {
31 uint64 result = current_;
32 current_ = (current_ * kA + kC) % kM;
33 return static_cast<uint32>(result >> 16);
36 std::string WebSocketStandardRequest(const std::string& path,
37 const std::string& host,
38 const std::string& origin,
39 const std::string& extra_headers) {
40 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
41 extra_headers);
44 std::string WebSocketStandardRequestWithCookies(
45 const std::string& path,
46 const std::string& host,
47 const std::string& origin,
48 const std::string& cookies,
49 const std::string& extra_headers) {
50 // Unrelated changes in net/http may change the order and default-values of
51 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
52 // string in that case.
53 return base::StringPrintf(
54 "GET %s HTTP/1.1\r\n"
55 "Host: %s\r\n"
56 "Connection: Upgrade\r\n"
57 "Pragma: no-cache\r\n"
58 "Cache-Control: no-cache\r\n"
59 "Upgrade: websocket\r\n"
60 "Origin: %s\r\n"
61 "Sec-WebSocket-Version: 13\r\n"
62 "User-Agent:\r\n"
63 "Accept-Encoding: gzip, deflate\r\n"
64 "Accept-Language: en-us,fr\r\n"
65 "%s"
66 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
67 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
68 "%s\r\n",
69 path.c_str(), host.c_str(), origin.c_str(), cookies.c_str(),
70 extra_headers.c_str());
73 std::string WebSocketStandardResponse(const std::string& extra_headers) {
74 return base::StringPrintf(
75 "HTTP/1.1 101 Switching Protocols\r\n"
76 "Upgrade: websocket\r\n"
77 "Connection: Upgrade\r\n"
78 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
79 "%s\r\n",
80 extra_headers.c_str());
83 struct WebSocketMockClientSocketFactoryMaker::Detail {
84 std::string expect_written;
85 std::string return_to_read;
86 std::vector<MockRead> reads;
87 MockWrite write;
88 ScopedVector<SequencedSocketData> socket_data_vector;
89 ScopedVector<SSLSocketDataProvider> ssl_socket_data_vector;
90 MockClientSocketFactory factory;
93 WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
94 : detail_(new Detail) {
97 WebSocketMockClientSocketFactoryMaker::
98 ~WebSocketMockClientSocketFactoryMaker() {
101 MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
102 return &detail_->factory;
105 void WebSocketMockClientSocketFactoryMaker::SetExpectations(
106 const std::string& expect_written,
107 const std::string& return_to_read) {
108 const size_t kHttpStreamParserBufferSize = 4096;
109 // We need to extend the lifetime of these strings.
110 detail_->expect_written = expect_written;
111 detail_->return_to_read = return_to_read;
112 int sequence = 0;
113 detail_->write = MockWrite(SYNCHRONOUS,
114 detail_->expect_written.data(),
115 detail_->expect_written.size(),
116 sequence++);
117 // HttpStreamParser reads 4KB at a time. We need to take this implementation
118 // detail into account if |return_to_read| is big enough.
119 for (size_t place = 0; place < detail_->return_to_read.size();
120 place += kHttpStreamParserBufferSize) {
121 detail_->reads.push_back(
122 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
123 std::min(detail_->return_to_read.size() - place,
124 kHttpStreamParserBufferSize),
125 sequence++));
127 scoped_ptr<SequencedSocketData> socket_data(
128 new SequencedSocketData(vector_as_array(&detail_->reads),
129 detail_->reads.size(), &detail_->write, 1));
130 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
131 AddRawExpectations(socket_data.Pass());
134 void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
135 scoped_ptr<SequencedSocketData> socket_data) {
136 detail_->factory.AddSocketDataProvider(socket_data.get());
137 detail_->socket_data_vector.push_back(socket_data.Pass());
140 void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
141 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
142 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
143 detail_->ssl_socket_data_vector.push_back(ssl_socket_data.Pass());
146 WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
147 : url_request_context_(true), url_request_context_initialized_(false) {
148 url_request_context_.set_client_socket_factory(maker_.factory());
151 WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
153 void WebSocketTestURLRequestContextHost::AddRawExpectations(
154 scoped_ptr<SequencedSocketData> socket_data) {
155 maker_.AddRawExpectations(socket_data.Pass());
158 void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
159 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
160 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass());
163 void WebSocketTestURLRequestContextHost::SetProxyConfig(
164 const std::string& proxy_rules) {
165 DCHECK(!url_request_context_initialized_);
166 proxy_service_.reset(ProxyService::CreateFixed(proxy_rules));
167 url_request_context_.set_proxy_service(proxy_service_.get());
170 TestURLRequestContext*
171 WebSocketTestURLRequestContextHost::GetURLRequestContext() {
172 if (!url_request_context_initialized_) {
173 url_request_context_.Init();
174 // A Network Delegate is required to make the URLRequest::Delegate work.
175 url_request_context_.set_network_delegate(&network_delegate_);
176 url_request_context_initialized_ = true;
178 return &url_request_context_;
181 } // namespace net