yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_ |
| 7 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 8 | #include <memory> |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 13 | #include "base/macros.h" |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 14 | #include "base/run_loop.h" |
| 15 | #include "base/timer/timer.h" |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 16 | #include "net/socket/socket_test_util.h" |
| 17 | #include "net/ssl/ssl_info.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 18 | #include "net/test/test_with_scoped_task_environment.h" |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 19 | #include "net/websockets/websocket_event_interface.h" |
| 20 | #include "net/websockets/websocket_test_util.h" |
| 21 | |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 22 | class GURL; |
| 23 | |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 24 | namespace net { |
| 25 | |
| 26 | class HttpRequestHeaders; |
| 27 | class HttpResponseHeaders; |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 28 | class URLRequest; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 29 | class WebSocketStream; |
| 30 | class WebSocketStreamRequest; |
| 31 | struct WebSocketHandshakeRequestInfo; |
| 32 | struct WebSocketHandshakeResponseInfo; |
| 33 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 34 | class WebSocketStreamCreateTestBase : public WithScopedTaskEnvironment { |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 35 | 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. |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 43 | void CreateAndConnectStream(const GURL& socket_url, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 44 | const std::vector<std::string>& sub_protocols, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 45 | const url::Origin& origin, |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 46 | const GURL& site_for_cookies, |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame] | 47 | const HttpRequestHeaders& additional_headers, |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 48 | std::unique_ptr<base::OneShotTimer> timer); |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 49 | |
| 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 Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 62 | // Runs |run_loop_waiting_for_on_auth_required_| until OnAuthRequired() is |
| 63 | // called. |
| 64 | void WaitUntilOnAuthRequired(); |
| 65 | |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 66 | // A simple function to make the tests more readable. |
| 67 | std::vector<std::string> NoSubProtocols(); |
| 68 | |
| 69 | protected: |
| 70 | WebSocketTestURLRequestContextHost url_request_context_host_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 71 | std::unique_ptr<WebSocketStreamRequest> stream_request_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 72 | // Only set if the connection succeeded. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 73 | std::unique_ptr<WebSocketStream> stream_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 74 | // Only set if the connection failed. |
| 75 | std::string failure_message_; |
| 76 | bool has_failed_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 77 | std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_; |
| 78 | std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_; |
| 79 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
| 80 | ssl_error_callbacks_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 81 | SSLInfo ssl_info_; |
| 82 | bool ssl_fatal_; |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 83 | URLRequest* url_request_; |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 84 | 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; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 91 | |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 92 | base::RunLoop connect_run_loop_; |
| 93 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 94 | base::RunLoop run_loop_waiting_for_on_auth_required_; |
| 95 | |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 96 | 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_ |