blob: 3d6a7cf448fb61524990936d5b1fb71f7d10c9b4 [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"
16#include "net/base/net_export.h"
17#include "net/socket/socket_test_util.h"
18#include "net/ssl/ssl_info.h"
19#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;
28class 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,
tyoshino8572d572016-07-13 06:29:4845 const GURL& first_party_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_;
yhirano01a5d662015-02-12 04:33:0679
80 // This temporarily sets WebSocketEndpointLockManager unlock delay to zero
81 // during tests.
82 ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_;
83 base::RunLoop connect_run_loop_;
84
85 private:
86 class TestConnectDelegate;
87 DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase);
88};
89
90} // namespace net
91
92#endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_