blob: 08f27ae76cd9a6c0a672b9628c1c50239d5273c5 [file] [log] [blame]
yhirano01a5d662015-02-12 04:33:061// 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.
4
5#include "net/websockets/websocket_stream_create_test_base.h"
Keishi Hattori0e45c022021-11-27 09:25:526#include "base/memory/raw_ptr.h"
yhirano01a5d662015-02-12 04:33:067
dchengc7eeda422015-12-26 03:56:488#include <utility>
9
yhirano01a5d662015-02-12 04:33:0610#include "base/callback.h"
Tsuyoshi Horo01faed62019-02-20 22:11:3711#include "net/base/ip_endpoint.h"
yhirano01a5d662015-02-12 04:33:0612#include "net/http/http_request_headers.h"
13#include "net/http/http_response_headers.h"
mikecironef22f9812016-10-04 03:40:1914#include "net/log/net_log_with_source.h"
Adam Langleyacbad242020-08-18 15:14:5215#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
yhirano01a5d662015-02-12 04:33:0616#include "net/websockets/websocket_basic_handshake_stream.h"
17#include "net/websockets/websocket_handshake_request_info.h"
18#include "net/websockets/websocket_handshake_response_info.h"
yhirano01a5d662015-02-12 04:33:0619#include "net/websockets/websocket_stream.h"
20#include "url/gurl.h"
mkwst4997ce82015-07-25 12:00:0521#include "url/origin.h"
yhirano01a5d662015-02-12 04:33:0622
23namespace net {
24
25using HeaderKeyValuePair = WebSocketStreamCreateTestBase::HeaderKeyValuePair;
26
yhirano01a5d662015-02-12 04:33:0627class WebSocketStreamCreateTestBase::TestConnectDelegate
28 : public WebSocketStream::ConnectDelegate {
29 public:
30 TestConnectDelegate(WebSocketStreamCreateTestBase* owner,
Steve Kobes3ad84b52020-07-09 00:30:0231 base::OnceClosure done_callback)
32 : owner_(owner), done_callback_(std::move(done_callback)) {}
yhirano01a5d662015-02-12 04:33:0633
Peter Boström407869b2021-10-07 04:42:4834 TestConnectDelegate(const TestConnectDelegate&) = delete;
35 TestConnectDelegate& operator=(const TestConnectDelegate&) = delete;
36
yhirano4a593832016-10-24 18:58:2237 void OnCreateRequest(URLRequest* request) override {
38 owner_->url_request_ = request;
39 }
40
Yoichi Osato1ead61a2020-01-06 04:52:5741 void OnSuccess(
42 std::unique_ptr<WebSocketStream> stream,
43 std::unique_ptr<WebSocketHandshakeResponseInfo> response) override {
44 if (owner_->response_info_)
45 ADD_FAILURE();
46 owner_->response_info_ = std::move(response);
yhirano01a5d662015-02-12 04:33:0647 stream.swap(owner_->stream_);
Steve Kobes3ad84b52020-07-09 00:30:0248 std::move(done_callback_).Run();
yhirano01a5d662015-02-12 04:33:0649 }
50
Adam Langleya48b636a2020-11-12 23:42:5251 void OnFailure(const std::string& message,
52 int net_error,
Anton Bikineev068d2912021-05-15 20:43:5253 absl::optional<int> response_code) override {
yhirano01a5d662015-02-12 04:33:0654 owner_->has_failed_ = true;
55 owner_->failure_message_ = message;
Adam Langleya48b636a2020-11-12 23:42:5256 owner_->failure_response_code_ = response_code.value_or(-1);
Steve Kobes3ad84b52020-07-09 00:30:0257 std::move(done_callback_).Run();
yhirano01a5d662015-02-12 04:33:0658 }
59
60 void OnStartOpeningHandshake(
danakj9c5cab52016-04-16 00:54:3361 std::unique_ptr<WebSocketHandshakeRequestInfo> request) override {
yhirano01a5d662015-02-12 04:33:0662 // Can be called multiple times (in the case of HTTP auth). Last call
63 // wins.
dchengc7eeda422015-12-26 03:56:4864 owner_->request_info_ = std::move(request);
yhirano01a5d662015-02-12 04:33:0665 }
66
yhirano01a5d662015-02-12 04:33:0667 void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:3368 std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
yhirano01a5d662015-02-12 04:33:0669 ssl_error_callbacks,
Emily Stark79fba5842019-04-25 04:59:3670 int net_error,
yhirano01a5d662015-02-12 04:33:0671 const SSLInfo& ssl_info,
72 bool fatal) override {
dchengc7eeda422015-12-26 03:56:4873 owner_->ssl_error_callbacks_ = std::move(ssl_error_callbacks);
yhirano01a5d662015-02-12 04:33:0674 owner_->ssl_info_ = ssl_info;
75 owner_->ssl_fatal_ = fatal;
76 }
77
Emily Starkf2c9bbd2019-04-09 17:08:5878 int OnAuthRequired(const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:5479 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:3780 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:5481 base::OnceCallback<void(const AuthCredentials*)> callback,
Anton Bikineev068d2912021-05-15 20:43:5282 absl::optional<AuthCredentials>* credentials) override {
Yutaka Hirano70fa25912018-06-06 05:26:5483 owner_->run_loop_waiting_for_on_auth_required_.Quit();
Emily Starkf2c9bbd2019-04-09 17:08:5884 owner_->auth_challenge_info_ = auth_info;
Yutaka Hirano70fa25912018-06-06 05:26:5485 *credentials = owner_->auth_credentials_;
86 owner_->on_auth_required_callback_ = std::move(callback);
87 return owner_->on_auth_required_rv_;
88 }
89
yhirano01a5d662015-02-12 04:33:0690 private:
Keishi Hattori0e45c022021-11-27 09:25:5291 raw_ptr<WebSocketStreamCreateTestBase> owner_;
Steve Kobes3ad84b52020-07-09 00:30:0292 base::OnceClosure done_callback_;
yhirano01a5d662015-02-12 04:33:0693};
94
Tsuyoshi Horo432981d52022-06-09 09:50:1395WebSocketStreamCreateTestBase::WebSocketStreamCreateTestBase() = default;
yhirano01a5d662015-02-12 04:33:0696
Chris Watkins28c2fdd2017-11-30 06:06:5297WebSocketStreamCreateTestBase::~WebSocketStreamCreateTestBase() = default;
yhirano01a5d662015-02-12 04:33:0698
99void WebSocketStreamCreateTestBase::CreateAndConnectStream(
tyoshino8572d572016-07-13 06:29:48100 const GURL& socket_url,
yhirano01a5d662015-02-12 04:33:06101 const std::vector<std::string>& sub_protocols,
mkwst4997ce82015-07-25 12:00:05102 const url::Origin& origin,
Maks Orlovich8be0e252019-12-09 18:35:49103 const SiteForCookies& site_for_cookies,
Matt Menke29a538d2020-04-29 16:12:17104 const IsolationInfo& isolation_info,
Yutaka Hirano2f65eec2018-05-23 01:58:22105 const HttpRequestHeaders& additional_headers,
tzik08d8d6e2018-07-09 04:11:47106 std::unique_ptr<base::OneShotTimer> timer) {
Bence Béky65623972018-03-05 15:31:56107 auto connect_delegate = std::make_unique<TestConnectDelegate>(
108 this, connect_run_loop_.QuitClosure());
Adam Rice6f75c0f2018-06-04 08:00:05109 auto api_delegate = std::make_unique<TestWebSocketStreamRequestAPI>();
tyoshinoccfcfde2016-07-21 14:06:55110 stream_request_ = WebSocketStream::CreateAndConnectStreamForTesting(
Matt Menke29a538d2020-04-29 16:12:17111 socket_url, sub_protocols, origin, site_for_cookies, isolation_info,
112 additional_headers, url_request_context_host_.GetURLRequestContext(),
Adam Langleyacbad242020-08-18 15:14:52113 NetLogWithSource(), TRAFFIC_ANNOTATION_FOR_TESTS,
114 std::move(connect_delegate),
tzik08d8d6e2018-07-09 04:11:47115 timer ? std::move(timer) : std::make_unique<base::OneShotTimer>(),
Adam Rice6f75c0f2018-06-04 08:00:05116 std::move(api_delegate));
yhirano01a5d662015-02-12 04:33:06117}
118
119std::vector<HeaderKeyValuePair>
120WebSocketStreamCreateTestBase::RequestHeadersToVector(
121 const HttpRequestHeaders& headers) {
122 HttpRequestHeaders::Iterator it(headers);
123 std::vector<HeaderKeyValuePair> result;
124 while (it.GetNext())
Tsuyoshi Horoebc507882022-06-30 11:16:45125 result.emplace_back(it.name(), it.value());
yhirano01a5d662015-02-12 04:33:06126 return result;
127}
128
129std::vector<HeaderKeyValuePair>
130WebSocketStreamCreateTestBase::ResponseHeadersToVector(
131 const HttpResponseHeaders& headers) {
olli.raula33c282f2016-01-21 12:12:49132 size_t iter = 0;
yhirano01a5d662015-02-12 04:33:06133 std::string name, value;
134 std::vector<HeaderKeyValuePair> result;
135 while (headers.EnumerateHeaderLines(&iter, &name, &value))
Tsuyoshi Horoebc507882022-06-30 11:16:45136 result.emplace_back(name, value);
yhirano01a5d662015-02-12 04:33:06137 return result;
138}
139
140void WebSocketStreamCreateTestBase::WaitUntilConnectDone() {
141 connect_run_loop_.Run();
142}
143
Yutaka Hirano70fa25912018-06-06 05:26:54144void WebSocketStreamCreateTestBase::WaitUntilOnAuthRequired() {
145 run_loop_waiting_for_on_auth_required_.Run();
146}
147
yhirano01a5d662015-02-12 04:33:06148std::vector<std::string> WebSocketStreamCreateTestBase::NoSubProtocols() {
149 return std::vector<std::string>();
150}
151
152} // namespace net