blob: aab93c7641d8dc612cffe04ffcadfa74b8fa7abb [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;
yhirano4a593832016-10-24 18:58:2227class URLRequest;
yhirano01a5d662015-02-12 04:33:0628class WebSocketStream;
29class WebSocketStreamRequest;
30struct WebSocketHandshakeRequestInfo;
31struct WebSocketHandshakeResponseInfo;
32
33class 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.
tyoshino8572d572016-07-13 06:29:4842 void CreateAndConnectStream(const GURL& socket_url,
yhirano01a5d662015-02-12 04:33:0643 const std::vector<std::string>& sub_protocols,
mkwst4997ce82015-07-25 12:00:0544 const url::Origin& origin,
Mike Westb85da8ed2017-08-10 14:16:4645 const GURL& site_for_cookies,
alladacef397d2016-06-29 17:52:2346 const std::string& additional_headers,
danakj9c5cab52016-04-16 00:54:3347 std::unique_ptr<base::Timer> timer);
yhirano01a5d662015-02-12 04:33:0648
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_;
danakj9c5cab52016-04-16 00:54:3366 std::unique_ptr<WebSocketStreamRequest> stream_request_;
yhirano01a5d662015-02-12 04:33:0667 // Only set if the connection succeeded.
danakj9c5cab52016-04-16 00:54:3368 std::unique_ptr<WebSocketStream> stream_;
yhirano01a5d662015-02-12 04:33:0669 // Only set if the connection failed.
70 std::string failure_message_;
71 bool has_failed_;
danakj9c5cab52016-04-16 00:54:3372 std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_;
73 std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_;
74 std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
75 ssl_error_callbacks_;
yhirano01a5d662015-02-12 04:33:0676 SSLInfo ssl_info_;
77 bool ssl_fatal_;
danakj9c5cab52016-04-16 00:54:3378 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_data_;
yhirano4a593832016-10-24 18:58:2279 URLRequest* url_request_;
yhirano01a5d662015-02-12 04:33:0680
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_