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"
24 class DeterministicMockClientSocketFactory
;
25 class DeterministicSocketData
;
26 class URLRequestContext
;
27 class WebSocketHandshakeStreamCreateHelper
;
28 struct SSLSocketDataProvider
;
30 class LinearCongruentialGenerator
{
32 explicit LinearCongruentialGenerator(uint32 seed
);
39 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing
40 // use only. The difference is the use of a |create_helper| argument in place of
41 // |requested_subprotocols|. Implemented in websocket_stream.cc.
42 NET_EXPORT_PRIVATE
extern scoped_ptr
<WebSocketStreamRequest
>
43 CreateAndConnectStreamForTesting(
44 const GURL
& socket_url
,
45 scoped_ptr
<WebSocketHandshakeStreamCreateHelper
> create_helper
,
46 const url::Origin
& origin
,
47 URLRequestContext
* url_request_context
,
48 const BoundNetLog
& net_log
,
49 scoped_ptr
<WebSocketStream::ConnectDelegate
> connect_delegate
);
51 // Generates a standard WebSocket handshake request. The challenge key used is
52 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
54 extern std::string
WebSocketStandardRequest(const std::string
& path
,
55 const std::string
& origin
,
56 const std::string
& extra_headers
);
58 // A response with the appropriate accept header to match the above challenge
59 // key. Each header in |extra_headers| must be terminated with "\r\n".
60 extern std::string
WebSocketStandardResponse(const std::string
& extra_headers
);
62 // This class provides a convenient way to construct a
63 // DeterministicMockClientSocketFactory for WebSocket tests.
64 class WebSocketDeterministicMockClientSocketFactoryMaker
{
66 WebSocketDeterministicMockClientSocketFactoryMaker();
67 ~WebSocketDeterministicMockClientSocketFactoryMaker();
69 // Tell the factory to create a socket which expects |expect_written| to be
70 // written, and responds with |return_to_read|. The test will fail if the
71 // expected text is not written, or all the bytes are not read. This adds data
72 // for a new mock-socket using AddRawExpections(), and so can be called
73 // multiple times to queue up multiple mock sockets, but usually in those
74 // cases the lower-level AddRawExpections() interface is more appropriate.
75 void SetExpectations(const std::string
& expect_written
,
76 const std::string
& return_to_read
);
78 // A low-level interface to permit arbitrary expectations to be added. The
79 // mock sockets will be created in the same order that they were added.
80 void AddRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
82 // Allow an SSL socket data provider to be added. You must also supply a mock
83 // transport socket for it to use. If the mock SSL handshake fails then the
84 // mock transport socket will connect but have nothing read or written. If the
85 // mock handshake succeeds then the data from the underlying transport socket
86 // will be passed through unchanged (without encryption).
87 void AddSSLSocketDataProvider(
88 scoped_ptr
<SSLSocketDataProvider
> ssl_socket_data
);
90 // Call to get a pointer to the factory, which remains owned by this object.
91 DeterministicMockClientSocketFactory
* factory();
95 scoped_ptr
<Detail
> detail_
;
97 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker
);
100 // This class encapsulates the details of creating a
101 // TestURLRequestContext that returns mock ClientSocketHandles that do what is
102 // required by the tests.
103 struct WebSocketTestURLRequestContextHost
{
105 WebSocketTestURLRequestContextHost();
106 ~WebSocketTestURLRequestContextHost();
108 void SetExpectations(const std::string
& expect_written
,
109 const std::string
& return_to_read
) {
110 maker_
.SetExpectations(expect_written
, return_to_read
);
113 void AddRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
115 // Allow an SSL socket data provider to be added.
116 void AddSSLSocketDataProvider(
117 scoped_ptr
<SSLSocketDataProvider
> ssl_socket_data
);
119 // Call after calling one of SetExpections() or AddRawExpectations(). The
120 // returned pointer remains owned by this object.
121 TestURLRequestContext
* GetURLRequestContext();
124 WebSocketDeterministicMockClientSocketFactoryMaker maker_
;
125 TestURLRequestContext url_request_context_
;
126 TestNetworkDelegate network_delegate_
;
127 bool url_request_context_initialized_
;
129 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost
);
134 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_