Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / net / websockets / websocket_test_util.cc
blob76057805a73be54e8531b1ac0c535b523cb307a7
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/stl_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/socket/socket_test_util.h"
15 namespace net {
17 namespace {
18 const uint64 kA =
19 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
20 const uint64 kC = 12345;
21 const uint64 kM = static_cast<uint64>(1) << 48;
23 } // namespace
25 LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
26 : current_(seed) {}
28 uint32 LinearCongruentialGenerator::Generate() {
29 uint64 result = current_;
30 current_ = (current_ * kA + kC) % kM;
31 return static_cast<uint32>(result >> 16);
34 std::string WebSocketStandardRequest(const std::string& path,
35 const std::string& origin,
36 const std::string& extra_headers) {
37 // Unrelated changes in net/http may change the order and default-values of
38 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
39 // string in that case.
40 return base::StringPrintf(
41 "GET %s HTTP/1.1\r\n"
42 "Host: localhost\r\n"
43 "Connection: Upgrade\r\n"
44 "Pragma: no-cache\r\n"
45 "Cache-Control: no-cache\r\n"
46 "Upgrade: websocket\r\n"
47 "Origin: %s\r\n"
48 "Sec-WebSocket-Version: 13\r\n"
49 "User-Agent:\r\n"
50 "Accept-Encoding: gzip,deflate\r\n"
51 "Accept-Language: en-us,fr\r\n"
52 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
53 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
54 "%s\r\n",
55 path.c_str(),
56 origin.c_str(),
57 extra_headers.c_str());
60 std::string WebSocketStandardResponse(const std::string& extra_headers) {
61 return base::StringPrintf(
62 "HTTP/1.1 101 Switching Protocols\r\n"
63 "Upgrade: websocket\r\n"
64 "Connection: Upgrade\r\n"
65 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
66 "%s\r\n",
67 extra_headers.c_str());
70 struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail {
71 std::string expect_written;
72 std::string return_to_read;
73 std::vector<MockRead> reads;
74 MockWrite write;
75 scoped_ptr<DeterministicSocketData> data;
76 DeterministicMockClientSocketFactory factory;
79 WebSocketDeterministicMockClientSocketFactoryMaker::
80 WebSocketDeterministicMockClientSocketFactoryMaker()
81 : detail_(new Detail) {}
83 WebSocketDeterministicMockClientSocketFactoryMaker::
84 ~WebSocketDeterministicMockClientSocketFactoryMaker() {}
86 DeterministicMockClientSocketFactory*
87 WebSocketDeterministicMockClientSocketFactoryMaker::factory() {
88 return &detail_->factory;
91 void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations(
92 const std::string& expect_written,
93 const std::string& return_to_read) {
94 const size_t kHttpStreamParserBufferSize = 4096;
95 // We need to extend the lifetime of these strings.
96 detail_->expect_written = expect_written;
97 detail_->return_to_read = return_to_read;
98 int sequence = 0;
99 detail_->write = MockWrite(SYNCHRONOUS,
100 detail_->expect_written.data(),
101 detail_->expect_written.size(),
102 sequence++);
103 // HttpStreamParser reads 4KB at a time. We need to take this implementation
104 // detail into account if |return_to_read| is big enough.
105 for (size_t place = 0; place < detail_->return_to_read.size();
106 place += kHttpStreamParserBufferSize) {
107 detail_->reads.push_back(
108 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
109 std::min(detail_->return_to_read.size() - place,
110 kHttpStreamParserBufferSize),
111 sequence++));
113 scoped_ptr<DeterministicSocketData> socket_data(
114 new DeterministicSocketData(vector_as_array(&detail_->reads),
115 detail_->reads.size(),
116 &detail_->write,
117 1));
118 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
119 socket_data->SetStop(sequence);
120 SetRawExpectations(socket_data.Pass());
123 void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations(
124 scoped_ptr<DeterministicSocketData> socket_data) {
125 detail_->data = socket_data.Pass();
126 detail_->factory.AddSocketDataProvider(detail_->data.get());
129 WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
130 : url_request_context_(true) {
131 url_request_context_.set_client_socket_factory(maker_.factory());
134 WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
136 void WebSocketTestURLRequestContextHost::SetRawExpectations(
137 scoped_ptr<DeterministicSocketData> socket_data) {
138 maker_.SetRawExpectations(socket_data.Pass());
141 TestURLRequestContext*
142 WebSocketTestURLRequestContextHost::GetURLRequestContext() {
143 url_request_context_.Init();
144 // A Network Delegate is required to make the URLRequest::Delegate work.
145 url_request_context_.set_network_delegate(&network_delegate_);
146 return &url_request_context_;
149 } // namespace net