| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
| 13 #include "net/websockets/websocket_stream.h" | 13 #include "net/websockets/websocket_stream.h" |
| 14 | 14 |
| 15 class GURL; | 15 class GURL; |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class Timer; | 18 class Timer; |
| 19 } // namespace base | 19 } // namespace base |
| 20 | 20 |
| 21 namespace url { | 21 namespace url { |
| 22 class Origin; | 22 class Origin; |
| 23 } // namespace url | 23 } // namespace url |
| 24 | 24 |
| 25 namespace net { | 25 namespace net { |
| 26 | 26 |
| 27 class BoundNetLog; | 27 class BoundNetLog; |
| 28 class DeterministicMockClientSocketFactory; | 28 class DeterministicMockClientSocketFactory; |
| 29 class DeterministicSocketData; | 29 class DeterministicSocketData; |
| 30 class ProxyService; |
| 30 class URLRequestContext; | 31 class URLRequestContext; |
| 31 class WebSocketHandshakeStreamCreateHelper; | 32 class WebSocketHandshakeStreamCreateHelper; |
| 32 struct SSLSocketDataProvider; | 33 struct SSLSocketDataProvider; |
| 33 | 34 |
| 34 class LinearCongruentialGenerator { | 35 class LinearCongruentialGenerator { |
| 35 public: | 36 public: |
| 36 explicit LinearCongruentialGenerator(uint32 seed); | 37 explicit LinearCongruentialGenerator(uint32 seed); |
| 37 uint32 Generate(); | 38 uint32 Generate(); |
| 38 | 39 |
| 39 private: | 40 private: |
| 40 uint64 current_; | 41 uint64 current_; |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing | 44 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing |
| 44 // use only. The differences are the use of a |create_helper| argument in place | 45 // use only. The differences are the use of a |create_helper| argument in place |
| 45 // of |requested_subprotocols| and taking |timer| as the handshake timeout | 46 // of |requested_subprotocols| and taking |timer| as the handshake timeout |
| 46 // timer. Implemented in websocket_stream.cc. | 47 // timer. Implemented in websocket_stream.cc. |
| 47 NET_EXPORT_PRIVATE extern scoped_ptr<WebSocketStreamRequest> | 48 NET_EXPORT_PRIVATE scoped_ptr<WebSocketStreamRequest> |
| 48 CreateAndConnectStreamForTesting( | 49 CreateAndConnectStreamForTesting( |
| 49 const GURL& socket_url, | 50 const GURL& socket_url, |
| 50 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, | 51 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
| 51 const url::Origin& origin, | 52 const url::Origin& origin, |
| 52 URLRequestContext* url_request_context, | 53 URLRequestContext* url_request_context, |
| 53 const BoundNetLog& net_log, | 54 const BoundNetLog& net_log, |
| 54 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, | 55 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
| 55 scoped_ptr<base::Timer> timer); | 56 scoped_ptr<base::Timer> timer); |
| 56 | 57 |
| 57 // Generates a standard WebSocket handshake request. The challenge key used is | 58 // Generates a standard WebSocket handshake request. The challenge key used is |
| 58 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated | 59 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated |
| 59 // with "\r\n". | 60 // with "\r\n". |
| 60 extern std::string WebSocketStandardRequest(const std::string& path, | 61 std::string WebSocketStandardRequest(const std::string& path, |
| 61 const std::string& host, | 62 const std::string& host, |
| 62 const std::string& origin, | 63 const std::string& origin, |
| 63 const std::string& extra_headers); | 64 const std::string& extra_headers); |
| 65 |
| 66 // Generates a standard WebSocket handshake request. The challenge key used is |
| 67 // "dGhlIHNhbXBsZSBub25jZQ==". |cookies| must be empty or terminated with |
| 68 // "\r\n". Each header in |extra_headers| must be terminated with "\r\n". |
| 69 std::string WebSocketStandardRequestWithCookies( |
| 70 const std::string& path, |
| 71 const std::string& host, |
| 72 const std::string& origin, |
| 73 const std::string& cookies, |
| 74 const std::string& extra_headers); |
| 64 | 75 |
| 65 // A response with the appropriate accept header to match the above challenge | 76 // A response with the appropriate accept header to match the above challenge |
| 66 // key. Each header in |extra_headers| must be terminated with "\r\n". | 77 // key. Each header in |extra_headers| must be terminated with "\r\n". |
| 67 extern std::string WebSocketStandardResponse(const std::string& extra_headers); | 78 std::string WebSocketStandardResponse(const std::string& extra_headers); |
| 68 | 79 |
| 69 // This class provides a convenient way to construct a | 80 // This class provides a convenient way to construct a |
| 70 // DeterministicMockClientSocketFactory for WebSocket tests. | 81 // DeterministicMockClientSocketFactory for WebSocket tests. |
| 71 class WebSocketDeterministicMockClientSocketFactoryMaker { | 82 class WebSocketDeterministicMockClientSocketFactoryMaker { |
| 72 public: | 83 public: |
| 73 WebSocketDeterministicMockClientSocketFactoryMaker(); | 84 WebSocketDeterministicMockClientSocketFactoryMaker(); |
| 74 ~WebSocketDeterministicMockClientSocketFactoryMaker(); | 85 ~WebSocketDeterministicMockClientSocketFactoryMaker(); |
| 75 | 86 |
| 76 // Tell the factory to create a socket which expects |expect_written| to be | 87 // Tell the factory to create a socket which expects |expect_written| to be |
| 77 // written, and responds with |return_to_read|. The test will fail if the | 88 // written, and responds with |return_to_read|. The test will fail if the |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 const std::string& return_to_read) { | 127 const std::string& return_to_read) { |
| 117 maker_.SetExpectations(expect_written, return_to_read); | 128 maker_.SetExpectations(expect_written, return_to_read); |
| 118 } | 129 } |
| 119 | 130 |
| 120 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); | 131 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); |
| 121 | 132 |
| 122 // Allow an SSL socket data provider to be added. | 133 // Allow an SSL socket data provider to be added. |
| 123 void AddSSLSocketDataProvider( | 134 void AddSSLSocketDataProvider( |
| 124 scoped_ptr<SSLSocketDataProvider> ssl_socket_data); | 135 scoped_ptr<SSLSocketDataProvider> ssl_socket_data); |
| 125 | 136 |
| 137 // Allow a proxy to be set. Usage: |
| 138 // SetProxyConfig("proxy1:8000"); |
| 139 // Any syntax accepted by net::ProxyConfig::ParseFromString() will work. |
| 140 // Do not call after GetURLRequestContext() has been called. |
| 141 void SetProxyConfig(const std::string& proxy_rules); |
| 142 |
| 126 // Call after calling one of SetExpections() or AddRawExpectations(). The | 143 // Call after calling one of SetExpections() or AddRawExpectations(). The |
| 127 // returned pointer remains owned by this object. | 144 // returned pointer remains owned by this object. |
| 128 TestURLRequestContext* GetURLRequestContext(); | 145 TestURLRequestContext* GetURLRequestContext(); |
| 129 | 146 |
| 130 private: | 147 private: |
| 131 WebSocketDeterministicMockClientSocketFactoryMaker maker_; | 148 WebSocketDeterministicMockClientSocketFactoryMaker maker_; |
| 132 TestURLRequestContext url_request_context_; | 149 TestURLRequestContext url_request_context_; |
| 133 TestNetworkDelegate network_delegate_; | 150 TestNetworkDelegate network_delegate_; |
| 151 scoped_ptr<ProxyService> proxy_service_; |
| 134 bool url_request_context_initialized_; | 152 bool url_request_context_initialized_; |
| 135 | 153 |
| 136 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); | 154 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); |
| 137 }; | 155 }; |
| 138 | 156 |
| 139 } // namespace net | 157 } // namespace net |
| 140 | 158 |
| 141 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 159 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
| OLD | NEW |