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