blob: be7912f63c8c57670f76fb8d3694631b05ab2b13 [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"
18#include "net/websockets/websocket_event_interface.h"
19#include "net/websockets/websocket_test_util.h"
20
tyoshino8572d572016-07-13 06:29:4821class GURL;
22
yhirano01a5d662015-02-12 04:33:0623namespace net {
24
25class HttpRequestHeaders;
26class HttpResponseHeaders;
27class WebSocketStream;
28class WebSocketStreamRequest;
29struct WebSocketHandshakeRequestInfo;
30struct WebSocketHandshakeResponseInfo;
31
32class WebSocketStreamCreateTestBase {
33 public:
34 using HeaderKeyValuePair = std::pair<std::string, std::string>;
35
36 WebSocketStreamCreateTestBase();
37 virtual ~WebSocketStreamCreateTestBase();
38
39 // A wrapper for CreateAndConnectStreamForTesting that knows about our default
40 // parameters.
tyoshino8572d572016-07-13 06:29:4841 void CreateAndConnectStream(const GURL& socket_url,
yhirano01a5d662015-02-12 04:33:0642 const std::vector<std::string>& sub_protocols,
mkwst4997ce82015-07-25 12:00:0543 const url::Origin& origin,
tyoshino8572d572016-07-13 06:29:4844 const GURL& first_party_for_cookies,
alladacef397d2016-06-29 17:52:2345 const std::string& additional_headers,
danakj9c5cab52016-04-16 00:54:3346 std::unique_ptr<base::Timer> timer);
yhirano01a5d662015-02-12 04:33:0647
48 static std::vector<HeaderKeyValuePair> RequestHeadersToVector(
49 const HttpRequestHeaders& headers);
50 static std::vector<HeaderKeyValuePair> ResponseHeadersToVector(
51 const HttpResponseHeaders& headers);
52
53 const std::string& failure_message() const { return failure_message_; }
54 bool has_failed() const { return has_failed_; }
55
56 // Runs |connect_run_loop_|. It will stop when the connection establishes or
57 // fails.
58 void WaitUntilConnectDone();
59
60 // A simple function to make the tests more readable.
61 std::vector<std::string> NoSubProtocols();
62
63 protected:
64 WebSocketTestURLRequestContextHost url_request_context_host_;
danakj9c5cab52016-04-16 00:54:3365 std::unique_ptr<WebSocketStreamRequest> stream_request_;
yhirano01a5d662015-02-12 04:33:0666 // Only set if the connection succeeded.
danakj9c5cab52016-04-16 00:54:3367 std::unique_ptr<WebSocketStream> stream_;
yhirano01a5d662015-02-12 04:33:0668 // Only set if the connection failed.
69 std::string failure_message_;
70 bool has_failed_;
danakj9c5cab52016-04-16 00:54:3371 std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_;
72 std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_;
73 std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
74 ssl_error_callbacks_;
yhirano01a5d662015-02-12 04:33:0675 SSLInfo ssl_info_;
76 bool ssl_fatal_;
danakj9c5cab52016-04-16 00:54:3377 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_data_;
yhirano01a5d662015-02-12 04:33:0678
79 // This temporarily sets WebSocketEndpointLockManager unlock delay to zero
80 // during tests.
81 ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_;
82 base::RunLoop connect_run_loop_;
83
84 private:
85 class TestConnectDelegate;
86 DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase);
87};
88
89} // namespace net
90
91#endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_