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 DeterministicSocketData
;
25 class URLRequestContext
;
26 class WebSocketHandshakeStreamCreateHelper
;
27 class DeterministicMockClientSocketFactory
;
29 class LinearCongruentialGenerator
{
31 explicit LinearCongruentialGenerator(uint32 seed
);
38 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing
39 // use only. The difference is the use of a |create_helper| argument in place of
40 // |requested_subprotocols|. Implemented in websocket_stream.cc.
41 NET_EXPORT_PRIVATE
extern scoped_ptr
<WebSocketStreamRequest
>
42 CreateAndConnectStreamForTesting(
43 const GURL
& socket_url
,
44 scoped_ptr
<WebSocketHandshakeStreamCreateHelper
> create_helper
,
45 const url::Origin
& origin
,
46 URLRequestContext
* url_request_context
,
47 const BoundNetLog
& net_log
,
48 scoped_ptr
<WebSocketStream::ConnectDelegate
> connect_delegate
);
50 // Generates a standard WebSocket handshake request. The challenge key used is
51 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
53 extern std::string
WebSocketStandardRequest(const std::string
& path
,
54 const std::string
& origin
,
55 const std::string
& extra_headers
);
57 // A response with the appropriate accept header to match the above challenge
58 // key. Each header in |extra_headers| must be terminated with "\r\n".
59 extern std::string
WebSocketStandardResponse(const std::string
& extra_headers
);
61 // This class provides a convenient way to construct a
62 // DeterministicMockClientSocketFactory for WebSocket tests.
63 class WebSocketDeterministicMockClientSocketFactoryMaker
{
65 WebSocketDeterministicMockClientSocketFactoryMaker();
66 ~WebSocketDeterministicMockClientSocketFactoryMaker();
68 // The socket created by the factory will expect |expect_written| to be
69 // written to the socket, and will respond with |return_to_read|. The test
70 // will fail if the expected text is not written, or all the bytes are not
72 void SetExpectations(const std::string
& expect_written
,
73 const std::string
& return_to_read
);
75 // A low-level interface to permit arbitrary expectations to be set.
76 void SetRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
78 // Call to get a pointer to the factory, which remains owned by this object.
79 DeterministicMockClientSocketFactory
* factory();
83 scoped_ptr
<Detail
> detail_
;
85 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker
);
88 // This class encapsulates the details of creating a
89 // TestURLRequestContext that returns mock ClientSocketHandles that do what is
90 // required by the tests.
91 struct WebSocketTestURLRequestContextHost
{
93 WebSocketTestURLRequestContextHost();
94 ~WebSocketTestURLRequestContextHost();
96 void SetExpectations(const std::string
& expect_written
,
97 const std::string
& return_to_read
) {
98 maker_
.SetExpectations(expect_written
, return_to_read
);
101 void SetRawExpectations(scoped_ptr
<DeterministicSocketData
> socket_data
);
103 // Call after calling one of SetExpections() or SetRawExpectations(). The
104 // returned pointer remains owned by this object. This should only be called
106 TestURLRequestContext
* GetURLRequestContext();
109 WebSocketDeterministicMockClientSocketFactoryMaker maker_
;
110 TestURLRequestContext url_request_context_
;
111 TestNetworkDelegate network_delegate_
;
113 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost
);
118 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_