// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_ #define NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_ #include #include #include #include #include "base/macros.h" #include "base/run_loop.h" #include "base/timer/timer.h" #include "net/socket/socket_test_util.h" #include "net/ssl/ssl_info.h" #include "net/test/test_with_scoped_task_environment.h" #include "net/websockets/websocket_event_interface.h" #include "net/websockets/websocket_test_util.h" class GURL; namespace net { class HttpRequestHeaders; class HttpResponseHeaders; class URLRequest; class WebSocketStream; class WebSocketStreamRequest; struct WebSocketHandshakeRequestInfo; struct WebSocketHandshakeResponseInfo; class WebSocketStreamCreateTestBase : public WithScopedTaskEnvironment { public: using HeaderKeyValuePair = std::pair; WebSocketStreamCreateTestBase(); virtual ~WebSocketStreamCreateTestBase(); // A wrapper for CreateAndConnectStreamForTesting that knows about our default // parameters. void CreateAndConnectStream(const GURL& socket_url, const std::vector& sub_protocols, const url::Origin& origin, const GURL& site_for_cookies, const HttpRequestHeaders& additional_headers, std::unique_ptr timer); static std::vector RequestHeadersToVector( const HttpRequestHeaders& headers); static std::vector ResponseHeadersToVector( const HttpResponseHeaders& headers); const std::string& failure_message() const { return failure_message_; } bool has_failed() const { return has_failed_; } // Runs |connect_run_loop_|. It will stop when the connection establishes or // fails. void WaitUntilConnectDone(); // Runs |run_loop_waiting_for_on_auth_required_| until OnAuthRequired() is // called. void WaitUntilOnAuthRequired(); // A simple function to make the tests more readable. std::vector NoSubProtocols(); protected: WebSocketTestURLRequestContextHost url_request_context_host_; std::unique_ptr stream_request_; // Only set if the connection succeeded. std::unique_ptr stream_; // Only set if the connection failed. std::string failure_message_; bool has_failed_; std::unique_ptr request_info_; std::unique_ptr response_info_; std::unique_ptr ssl_error_callbacks_; SSLInfo ssl_info_; bool ssl_fatal_; URLRequest* url_request_; scoped_refptr auth_challenge_info_; base::OnceCallback on_auth_required_callback_; // This value will be copied to |*credentials| on OnAuthRequired. base::Optional auth_credentials_; // OnAuthRequired returns this value. int on_auth_required_rv_ = OK; base::RunLoop connect_run_loop_; base::RunLoop run_loop_waiting_for_on_auth_required_; private: class TestConnectDelegate; DISALLOW_COPY_AND_ASSIGN(WebSocketStreamCreateTestBase); }; } // namespace net #endif // NET_WEBSOCKETS_WEBSOCKET_STREAM_CREATE_TEST_BASE_H_