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.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_
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"
28 class DeterministicMockClientSocketFactory
;
29 class DeterministicSocketData
;
31 class URLRequestContext
;
32 struct SSLSocketDataProvider
;
34 class LinearCongruentialGenerator
{
36 explicit LinearCongruentialGenerator(uint32 seed
);
43 // Generates a standard WebSocket handshake request. The challenge key used is
44 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
46 std::string
WebSocketStandardRequest(const std::string
& path
,
47 const std::string
& host
,
48 const std::string
& origin
,
49 const std::string
& extra_headers
);
51 // Generates a standard WebSocket handshake request. The challenge key used is
52 // "dGhlIHNhbXBsZSBub25jZQ==". |cookies| must be empty or terminated with
53 // "\r\n". Each header in |extra_headers| must be terminated with "\r\n".
54 std::string
WebSocketStandardRequestWithCookies(
55 const std::string
& path
,
56 const std::string
& host
,
57 const std::string
& origin
,
58 const std::string
& cookies
,
59 const std::string
& extra_headers
);
61 // A response with the appropriate accept header to match the above challenge
62 // key. Each header in |extra_headers| must be terminated with "\r\n".
63 std::string
WebSocketStandardResponse(const std::string
& extra_headers
);
65 // This class provides a convenient way to construct a
66 // DeterministicMockClientSocketFactory for WebSocket tests.
67 class WebSocketDeterministicMockClientSocketFactoryMaker
{
69 WebSocketDeterministicMockClientSocketFactoryMaker();
70 ~WebSocketDeterministicMockClientSocketFactoryMaker();
72 // Tell the factory to create a socket which expects |expect_written| to be
73 // written, and responds with |return_to_read|. The test will fail if the
74 // expected text is not written, or all the bytes are not read. This adds data
75 // for a new mock-socket using AddRawExpections(), and so can be called
76 // multiple times to queue up multiple mock sockets, but usually in those
77 // cases the lower-level AddRawExpections() interface is more appropriate.
78 void SetExpectations(const std::string
& expect_written
,
79 const std::string
& return_to_read
);
81 // A low-level interface to permit arbitrary expectations to be added. The
82 // mock sockets will be created in the same order that they were added.
83 void AddRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
85 // Allow an SSL socket data provider to be added. You must also supply a mock
86 // transport socket for it to use. If the mock SSL handshake fails then the
87 // mock transport socket will connect but have nothing read or written. If the
88 // mock handshake succeeds then the data from the underlying transport socket
89 // will be passed through unchanged (without encryption).
90 void AddSSLSocketDataProvider(
91 scoped_ptr
<SSLSocketDataProvider
> ssl_socket_data
);
93 // Call to get a pointer to the factory, which remains owned by this object.
94 DeterministicMockClientSocketFactory
* factory();
98 scoped_ptr
<Detail
> detail_
;
100 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker
);
103 // This class encapsulates the details of creating a
104 // TestURLRequestContext that returns mock ClientSocketHandles that do what is
105 // required by the tests.
106 struct WebSocketTestURLRequestContextHost
{
108 WebSocketTestURLRequestContextHost();
109 ~WebSocketTestURLRequestContextHost();
111 void SetExpectations(const std::string
& expect_written
,
112 const std::string
& return_to_read
) {
113 maker_
.SetExpectations(expect_written
, return_to_read
);
116 void AddRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
118 // Allow an SSL socket data provider to be added.
119 void AddSSLSocketDataProvider(
120 scoped_ptr
<SSLSocketDataProvider
> ssl_socket_data
);
122 // Allow a proxy to be set. Usage:
123 // SetProxyConfig("proxy1:8000");
124 // Any syntax accepted by net::ProxyConfig::ParseFromString() will work.
125 // Do not call after GetURLRequestContext() has been called.
126 void SetProxyConfig(const std::string
& proxy_rules
);
128 // Call after calling one of SetExpections() or AddRawExpectations(). The
129 // returned pointer remains owned by this object.
130 TestURLRequestContext
* GetURLRequestContext();
133 WebSocketDeterministicMockClientSocketFactoryMaker maker_
;
134 TestURLRequestContext url_request_context_
;
135 TestNetworkDelegate network_delegate_
;
136 scoped_ptr
<ProxyService
> proxy_service_
;
137 bool url_request_context_initialized_
;
139 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost
);
144 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_