blob: f0ac6fa37dacb01b5c784e8d16d462cd71d71b09 [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"
Bence Béky98447b12018-05-08 03:14:0118#include "net/test/test_with_scoped_task_environment.h"
yhirano01a5d662015-02-12 04:33:0619#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;
yhirano4a593832016-10-24 18:58:2228class URLRequest;
yhirano01a5d662015-02-12 04:33:0629class WebSocketStream;
30class WebSocketStreamRequest;
31struct WebSocketHandshakeRequestInfo;
32struct WebSocketHandshakeResponseInfo;
33
Bence Béky98447b12018-05-08 03:14:0134class WebSocketStreamCreateTestBase : public WithScopedTaskEnvironment {
yhirano01a5d662015-02-12 04:33:0635 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.
tyoshino8572d572016-07-13 06:29:4843 void CreateAndConnectStream(const GURL& socket_url,
yhirano01a5d662015-02-12 04:33:0644 const std::vector<std::string>& sub_protocols,
mkwst4997ce82015-07-25 12:00:0545 const url::Origin& origin,
Mike Westb85da8ed2017-08-10 14:16:4646 const GURL& site_for_cookies,
alladacef397d2016-06-29 17:52:2347 const std::string& additional_headers,
danakj9c5cab52016-04-16 00:54:3348 std::unique_ptr<base::Timer> timer);
yhirano01a5d662015-02-12 04:33:0649
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
62 // A simple function to make the tests more readable.
63 std::vector<std::string> NoSubProtocols();
64
65 protected:
66 WebSocketTestURLRequestContextHost url_request_context_host_;
danakj9c5cab52016-04-16 00:54:3367 std::unique_ptr<WebSocketStreamRequest> stream_request_;
yhirano01a5d662015-02-12 04:33:0668 // Only set if the connection succeeded.
danakj9c5cab52016-04-16 00:54:3369 std::unique_ptr<WebSocketStream> stream_;
yhirano01a5d662015-02-12 04:33:0670 // Only set if the connection failed.
71 std::string failure_message_;
72 bool has_failed_;
danakj9c5cab52016-04-16 00:54:3373 std::unique_ptr<WebSocketHandshakeRequestInfo> request_info_;
74 std::unique_ptr<WebSocketHandshakeResponseInfo> response_info_;
75 std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks>
76 ssl_error_callbacks_;
yhirano01a5d662015-02-12 04:33:0677 SSLInfo ssl_info_;
78 bool ssl_fatal_;
yhirano4a593832016-10-24 18:58:2279 URLRequest* url_request_;
yhirano01a5d662015-02-12 04:33:0680
yhirano01a5d662015-02-12 04:33:0681 base::RunLoop connect_run_loop_;
82
83 private:
84 class TestConnectDelegate;
85 DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase);
86};
87
88} // namespace net
89
90#endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_