OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | |
6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "net/url_request/url_request_test_util.h" | |
13 #include "net/websockets/websocket_stream.h" | |
14 | |
15 class GURL; | |
16 | |
17 namespace base { | |
18 class Timer; | |
19 } // namespace base | |
20 | |
21 namespace url { | |
22 class Origin; | |
23 } // namespace url | |
24 | |
25 namespace net { | |
26 | |
27 class BoundNetLog; | |
28 class DeterministicMockClientSocketFactory; | |
29 class DeterministicSocketData; | |
30 class ProxyService; | |
31 class URLRequestContext; | |
32 class WebSocketHandshakeStreamCreateHelper; | |
33 struct SSLSocketDataProvider; | |
34 | |
35 class LinearCongruentialGenerator { | |
36 public: | |
37 explicit LinearCongruentialGenerator(uint32 seed); | |
38 uint32 Generate(); | |
39 | |
40 private: | |
41 uint64 current_; | |
42 }; | |
43 | |
44 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing | |
45 // use only. The differences are the use of a |create_helper| argument in place | |
46 // of |requested_subprotocols| and taking |timer| as the handshake timeout | |
47 // timer. Implemented in websocket_stream.cc. | |
48 NET_EXPORT_PRIVATE scoped_ptr<WebSocketStreamRequest> | |
49 CreateAndConnectStreamForTesting( | |
50 const GURL& socket_url, | |
51 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, | |
52 const url::Origin& origin, | |
53 URLRequestContext* url_request_context, | |
54 const BoundNetLog& net_log, | |
55 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, | |
56 scoped_ptr<base::Timer> timer); | |
57 | |
58 // Generates a standard WebSocket handshake request. The challenge key used is | |
59 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated | |
60 // with "\r\n". | |
61 std::string WebSocketStandardRequest(const std::string& path, | |
62 const std::string& host, | |
63 const std::string& origin, | |
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); | |
75 | |
76 // A response with the appropriate accept header to match the above challenge | |
77 // key. Each header in |extra_headers| must be terminated with "\r\n". | |
78 std::string WebSocketStandardResponse(const std::string& extra_headers); | |
79 | |
80 // This class provides a convenient way to construct a | |
81 // DeterministicMockClientSocketFactory for WebSocket tests. | |
82 class WebSocketDeterministicMockClientSocketFactoryMaker { | |
83 public: | |
84 WebSocketDeterministicMockClientSocketFactoryMaker(); | |
85 ~WebSocketDeterministicMockClientSocketFactoryMaker(); | |
86 | |
87 // Tell the factory to create a socket which expects |expect_written| to be | |
88 // written, and responds with |return_to_read|. The test will fail if the | |
89 // expected text is not written, or all the bytes are not read. This adds data | |
90 // for a new mock-socket using AddRawExpections(), and so can be called | |
91 // multiple times to queue up multiple mock sockets, but usually in those | |
92 // cases the lower-level AddRawExpections() interface is more appropriate. | |
93 void SetExpectations(const std::string& expect_written, | |
94 const std::string& return_to_read); | |
95 | |
96 // A low-level interface to permit arbitrary expectations to be added. The | |
97 // mock sockets will be created in the same order that they were added. | |
98 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); | |
99 | |
100 // Allow an SSL socket data provider to be added. You must also supply a mock | |
101 // transport socket for it to use. If the mock SSL handshake fails then the | |
102 // mock transport socket will connect but have nothing read or written. If the | |
103 // mock handshake succeeds then the data from the underlying transport socket | |
104 // will be passed through unchanged (without encryption). | |
105 void AddSSLSocketDataProvider( | |
106 scoped_ptr<SSLSocketDataProvider> ssl_socket_data); | |
107 | |
108 // Call to get a pointer to the factory, which remains owned by this object. | |
109 DeterministicMockClientSocketFactory* factory(); | |
110 | |
111 private: | |
112 struct Detail; | |
113 scoped_ptr<Detail> detail_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker); | |
116 }; | |
117 | |
118 // This class encapsulates the details of creating a | |
119 // TestURLRequestContext that returns mock ClientSocketHandles that do what is | |
120 // required by the tests. | |
121 struct WebSocketTestURLRequestContextHost { | |
122 public: | |
123 WebSocketTestURLRequestContextHost(); | |
124 ~WebSocketTestURLRequestContextHost(); | |
125 | |
126 void SetExpectations(const std::string& expect_written, | |
127 const std::string& return_to_read) { | |
128 maker_.SetExpectations(expect_written, return_to_read); | |
129 } | |
130 | |
131 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); | |
132 | |
133 // Allow an SSL socket data provider to be added. | |
134 void AddSSLSocketDataProvider( | |
135 scoped_ptr<SSLSocketDataProvider> ssl_socket_data); | |
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 | |
143 // Call after calling one of SetExpections() or AddRawExpectations(). The | |
144 // returned pointer remains owned by this object. | |
145 TestURLRequestContext* GetURLRequestContext(); | |
146 | |
147 private: | |
148 WebSocketDeterministicMockClientSocketFactoryMaker maker_; | |
149 TestURLRequestContext url_request_context_; | |
150 TestNetworkDelegate network_delegate_; | |
151 scoped_ptr<ProxyService> proxy_service_; | |
152 bool url_request_context_initialized_; | |
153 | |
154 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); | |
155 }; | |
156 | |
157 } // namespace net | |
158 | |
159 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | |
OLD | NEW |