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