blob: 228789c2e91d4315a29c0ba0a0789c7c5e219784 [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#ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_
6#define NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_
7
danakj9c5cab52016-04-16 00:54:338#include <memory>
yhirano01a5d662015-02-12 04:33:069#include <string>
10#include <utility>
11#include <vector>
12
Avi Drissman13fc8932015-12-20 04:40:4613#include "base/macros.h"
yhirano01a5d662015-02-12 04:33:0614#include "base/run_loop.h"
15#include "base/timer/timer.h"
yhirano01a5d662015-02-12 04:33:0616#include "net/socket/socket_test_util.h"
17#include "net/ssl/ssl_info.h"
Bence Béky98447b12018-05-08 03:14:0118#include "net/test/test_with_scoped_task_environment.h"
yhirano01a5d662015-02-12 04:33:0619#include "net/websockets/websocket_event_interface.h"
20#include "net/websockets/websocket_test_util.h"
21
tyoshino8572d572016-07-13 06:29:4822class GURL;
23
yhirano01a5d662015-02-12 04:33:0624namespace net {
25
26class HttpRequestHeaders;
27class HttpResponseHeaders;
yhirano4a593832016-10-24 18:58:2228class URLRequest;
yhirano01a5d662015-02-12 04:33:0629class WebSocketStream;
30class WebSocketStreamRequest;
31struct WebSocketHandshakeRequestInfo;
32struct WebSocketHandshakeResponseInfo;
33
Bence Béky98447b12018-05-08 03:14:0134class WebSocketStreamCreateTestBase : public WithScopedTaskEnvironment {
yhirano01a5d662015-02-12 04:33:0635 public:
36 using HeaderKeyValuePair = std::pair<std::string, std::string>;
37
38 WebSocketStreamCreateTestBase();
39 virtual ~WebSocketStreamCreateTestBase();
40
41 // A wrapper for CreateAndConnectStreamForTesting that knows about our default
42 // parameters.
tyoshino8572d572016-07-13 06:29:4843 void CreateAndConnectStream(const GURL& socket_url,
yhirano01a5d662015-02-12 04:33:0644 const std::vector<std::string>& sub_protocols,
mkwst4997ce82015-07-25 12:00:0545 const url::Origin& origin,
Mike Westb85da8ed2017-08-10 14:16:4646 const GURL& site_for_cookies,
Yutaka Hirano2f65eec2018-05-23 01:58:2247 const HttpRequestHeaders& additional_headers,
tzik08d8d6e2018-07-09 04:11:4748 std::unique_ptr<base::OneShotTimer> timer);
yhirano01a5d662015-02-12 04:33:0649
50 static std::vector<HeaderKeyValuePair> RequestHeadersToVector(
51 const HttpRequestHeaders& headers);
52 static std::vector<HeaderKeyValuePair> ResponseHeadersToVector(
53 const HttpResponseHeaders& headers);
54
55 const std::string& failure_message() const { return failure_message_; }
56 bool has_failed() const { return has_failed_; }
57
58 // Runs |connect_run_loop_|. It will stop when the connection establishes or
59 // fails.
60 void WaitUntilConnectDone();
61
Yutaka Hirano70fa25912018-06-06 05:26:5462 // Runs |run_loop_waiting_for_on_auth_required_| until OnAuthRequired() is
63 // called.
64 void WaitUntilOnAuthRequired();
65
yhirano01a5d662015-02-12 04:33:0666 // A simple function to make the tests more readable.
67 std::vector<std::string> NoSubProtocols();
68
69 protected:
70 WebSocketTestURLRequestContextHost url_request_context_host_;
danakj9c5cab52016-04-16 00:54:3371 std::unique_ptr<WebSocketStreamRequest> stream_request_;
yhirano01a5d662015-02-12 04:33:0672 // Only set if the connection succeeded.
danakj9c5cab52016-04-16 00:54:3373 std::unique_ptr<WebSocketStream> stream_;
yhirano01a5d662015-02-12 04:33:0674 // Only set if the connection failed.
75 std::string failure_message_;
76 bool has_failed_;
danakj9c5cab52016-04-16 00:54:3377 std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_;
78 std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_;
79 std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
80 ssl_error_callbacks_;
yhirano01a5d662015-02-12 04:33:0681 SSLInfo ssl_info_;
82 bool ssl_fatal_;
yhirano4a593832016-10-24 18:58:2283 URLRequest* url_request_;
Yutaka Hirano70fa25912018-06-06 05:26:5484 scoped_refptr<AuthChallengeInfo> auth_challenge_info_;
85 base::OnceCallback<void(const AuthCredentials*)> on_auth_required_callback_;
86
87 // This value will be copied to |*credentials| on OnAuthRequired.
88 base::Optional<AuthCredentials> auth_credentials_;
89 // OnAuthRequired returns this value.
90 int on_auth_required_rv_ = OK;
yhirano01a5d662015-02-12 04:33:0691
yhirano01a5d662015-02-12 04:33:0692 base::RunLoop connect_run_loop_;
93
Yutaka Hirano70fa25912018-06-06 05:26:5494 base::RunLoop run_loop_waiting_for_on_auth_required_;
95
yhirano01a5d662015-02-12 04:33:0696 private:
97 class TestConnectDelegate;
98 DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase);
99};
100
101} // namespace net
102
103#endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_