Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / websockets / websocket_stream_create_test_base.cc
blob387ce8a95313bbbedb38308b4034b38465af97d2
1 // Copyright 2015 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_stream_create_test_base.h"
7 #include "base/callback.h"
8 #include "net/http/http_request_headers.h"
9 #include "net/http/http_response_headers.h"
10 #include "net/websockets/websocket_basic_handshake_stream.h"
11 #include "net/websockets/websocket_handshake_request_info.h"
12 #include "net/websockets/websocket_handshake_response_info.h"
13 #include "net/websockets/websocket_handshake_stream_create_helper.h"
14 #include "net/websockets/websocket_stream.h"
15 #include "url/gurl.h"
16 #include "url/origin.h"
18 namespace net {
20 using HeaderKeyValuePair = WebSocketStreamCreateTestBase::HeaderKeyValuePair;
22 // A sub-class of WebSocketHandshakeStreamCreateHelper which always sets a
23 // deterministic key to use in the WebSocket handshake.
24 class DeterministicKeyWebSocketHandshakeStreamCreateHelper
25 : public WebSocketHandshakeStreamCreateHelper {
26 public:
27 DeterministicKeyWebSocketHandshakeStreamCreateHelper(
28 WebSocketStream::ConnectDelegate* connect_delegate,
29 const std::vector<std::string>& requested_subprotocols)
30 : WebSocketHandshakeStreamCreateHelper(connect_delegate,
31 requested_subprotocols) {}
33 void OnStreamCreated(WebSocketBasicHandshakeStream* stream) override {
34 stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
37 private:
38 DISALLOW_COPY_AND_ASSIGN(
39 DeterministicKeyWebSocketHandshakeStreamCreateHelper);
42 class WebSocketStreamCreateTestBase::TestConnectDelegate
43 : public WebSocketStream::ConnectDelegate {
44 public:
45 TestConnectDelegate(WebSocketStreamCreateTestBase* owner,
46 const base::Closure& done_callback)
47 : owner_(owner), done_callback_(done_callback) {}
49 void OnSuccess(scoped_ptr<WebSocketStream> stream) override {
50 stream.swap(owner_->stream_);
51 done_callback_.Run();
54 void OnFailure(const std::string& message) override {
55 owner_->has_failed_ = true;
56 owner_->failure_message_ = message;
57 done_callback_.Run();
60 void OnStartOpeningHandshake(
61 scoped_ptr<WebSocketHandshakeRequestInfo> request) override {
62 // Can be called multiple times (in the case of HTTP auth). Last call
63 // wins.
64 owner_->request_info_ = request.Pass();
67 void OnFinishOpeningHandshake(
68 scoped_ptr<WebSocketHandshakeResponseInfo> response) override {
69 if (owner_->response_info_)
70 ADD_FAILURE();
71 owner_->response_info_ = response.Pass();
74 void OnSSLCertificateError(
75 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks>
76 ssl_error_callbacks,
77 const SSLInfo& ssl_info,
78 bool fatal) override {
79 owner_->ssl_error_callbacks_ = ssl_error_callbacks.Pass();
80 owner_->ssl_info_ = ssl_info;
81 owner_->ssl_fatal_ = fatal;
84 private:
85 WebSocketStreamCreateTestBase* owner_;
86 base::Closure done_callback_;
87 DISALLOW_COPY_AND_ASSIGN(TestConnectDelegate);
90 WebSocketStreamCreateTestBase::WebSocketStreamCreateTestBase()
91 : has_failed_(false), ssl_fatal_(false) {
94 WebSocketStreamCreateTestBase::~WebSocketStreamCreateTestBase() {
97 void WebSocketStreamCreateTestBase::CreateAndConnectStream(
98 const std::string& socket_url,
99 const std::vector<std::string>& sub_protocols,
100 const url::Origin& origin,
101 scoped_ptr<base::Timer> timer) {
102 for (size_t i = 0; i < ssl_data_.size(); ++i) {
103 scoped_ptr<SSLSocketDataProvider> ssl_data(ssl_data_[i]);
104 url_request_context_host_.AddSSLSocketDataProvider(ssl_data.Pass());
106 ssl_data_.weak_clear();
107 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate(
108 new TestConnectDelegate(this, connect_run_loop_.QuitClosure()));
109 WebSocketStream::ConnectDelegate* delegate = connect_delegate.get();
110 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper(
111 new DeterministicKeyWebSocketHandshakeStreamCreateHelper(delegate,
112 sub_protocols));
113 stream_request_ = CreateAndConnectStreamForTesting(
114 GURL(socket_url), create_helper.Pass(), origin,
115 url_request_context_host_.GetURLRequestContext(), BoundNetLog(),
116 connect_delegate.Pass(),
117 timer ? timer.Pass()
118 : scoped_ptr<base::Timer>(new base::Timer(false, false)));
121 std::vector<HeaderKeyValuePair>
122 WebSocketStreamCreateTestBase::RequestHeadersToVector(
123 const HttpRequestHeaders& headers) {
124 HttpRequestHeaders::Iterator it(headers);
125 std::vector<HeaderKeyValuePair> result;
126 while (it.GetNext())
127 result.push_back(HeaderKeyValuePair(it.name(), it.value()));
128 return result;
131 std::vector<HeaderKeyValuePair>
132 WebSocketStreamCreateTestBase::ResponseHeadersToVector(
133 const HttpResponseHeaders& headers) {
134 void* iter = NULL;
135 std::string name, value;
136 std::vector<HeaderKeyValuePair> result;
137 while (headers.EnumerateHeaderLines(&iter, &name, &value))
138 result.push_back(HeaderKeyValuePair(name, value));
139 return result;
142 void WebSocketStreamCreateTestBase::WaitUntilConnectDone() {
143 connect_run_loop_.Run();
146 std::vector<std::string> WebSocketStreamCreateTestBase::NoSubProtocols() {
147 return std::vector<std::string>();
150 } // namespace net