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" |
| 18 | #include "net/websockets/websocket_event_interface.h" |
| 19 | #include "net/websockets/websocket_test_util.h" |
| 20 | |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 21 | class GURL; |
| 22 | |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 23 | namespace net { |
| 24 | |
| 25 | class HttpRequestHeaders; |
| 26 | class HttpResponseHeaders; |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 27 | class URLRequest; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 28 | class WebSocketStream; |
| 29 | class WebSocketStreamRequest; |
| 30 | struct WebSocketHandshakeRequestInfo; |
| 31 | struct WebSocketHandshakeResponseInfo; |
| 32 | |
| 33 | class WebSocketStreamCreateTestBase { |
| 34 | public: |
| 35 | using HeaderKeyValuePair = std::pair<std::string, std::string>; |
| 36 | |
| 37 | WebSocketStreamCreateTestBase(); |
| 38 | virtual ~WebSocketStreamCreateTestBase(); |
| 39 | |
| 40 | // A wrapper for CreateAndConnectStreamForTesting that knows about our default |
| 41 | // parameters. |
tyoshino | 8572d57 | 2016-07-13 06:29:48 | [diff] [blame] | 42 | void CreateAndConnectStream(const GURL& socket_url, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 43 | const std::vector<std::string>& sub_protocols, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 44 | const url::Origin& origin, |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame^] | 45 | const GURL& site_for_cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 46 | const std::string& additional_headers, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 47 | std::unique_ptr<base::Timer> timer); |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 48 | |
| 49 | static std::vector<HeaderKeyValuePair> RequestHeadersToVector( |
| 50 | const HttpRequestHeaders& headers); |
| 51 | static std::vector<HeaderKeyValuePair> ResponseHeadersToVector( |
| 52 | const HttpResponseHeaders& headers); |
| 53 | |
| 54 | const std::string& failure_message() const { return failure_message_; } |
| 55 | bool has_failed() const { return has_failed_; } |
| 56 | |
| 57 | // Runs |connect_run_loop_|. It will stop when the connection establishes or |
| 58 | // fails. |
| 59 | void WaitUntilConnectDone(); |
| 60 | |
| 61 | // A simple function to make the tests more readable. |
| 62 | std::vector<std::string> NoSubProtocols(); |
| 63 | |
| 64 | protected: |
| 65 | WebSocketTestURLRequestContextHost url_request_context_host_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 66 | std::unique_ptr<WebSocketStreamRequest> stream_request_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 67 | // Only set if the connection succeeded. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 68 | std::unique_ptr<WebSocketStream> stream_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 69 | // Only set if the connection failed. |
| 70 | std::string failure_message_; |
| 71 | bool has_failed_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 72 | std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_; |
| 73 | std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_; |
| 74 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
| 75 | ssl_error_callbacks_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 76 | SSLInfo ssl_info_; |
| 77 | bool ssl_fatal_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 78 | std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_data_; |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 79 | URLRequest* url_request_; |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 80 | |
| 81 | // This temporarily sets WebSocketEndpointLockManager unlock delay to zero |
| 82 | // during tests. |
| 83 | ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_; |
| 84 | base::RunLoop connect_run_loop_; |
| 85 | |
| 86 | private: |
| 87 | class TestConnectDelegate; |
| 88 | DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase); |
| 89 | }; |
| 90 | |
| 91 | } // namespace net |
| 92 | |
| 93 | #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_ |