Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / net / websockets / websocket_test_util.h
blob5c7db80eac748fe676394e5b221cf39866bc38ff
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_
8 #include <string>
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"
15 class GURL;
17 namespace base {
18 class Timer;
19 } // namespace base
21 namespace url {
22 class Origin;
23 } // namespace url
25 namespace net {
27 class BoundNetLog;
28 class DeterministicMockClientSocketFactory;
29 class DeterministicSocketData;
30 class URLRequestContext;
31 class WebSocketHandshakeStreamCreateHelper;
32 struct SSLSocketDataProvider;
34 class LinearCongruentialGenerator {
35 public:
36 explicit LinearCongruentialGenerator(uint32 seed);
37 uint32 Generate();
39 private:
40 uint64 current_;
43 // Alternate version of WebSocketStream::CreateAndConnectStream() for testing
44 // use only. The differences are the use of a |create_helper| argument in place
45 // of |requested_subprotocols| and taking |timer| as the handshake timeout
46 // timer. Implemented in websocket_stream.cc.
47 NET_EXPORT_PRIVATE extern scoped_ptr<WebSocketStreamRequest>
48 CreateAndConnectStreamForTesting(
49 const GURL& socket_url,
50 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper,
51 const url::Origin& origin,
52 URLRequestContext* url_request_context,
53 const BoundNetLog& net_log,
54 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate,
55 scoped_ptr<base::Timer> timer);
57 // Generates a standard WebSocket handshake request. The challenge key used is
58 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
59 // with "\r\n".
60 extern std::string WebSocketStandardRequest(const std::string& path,
61 const std::string& host,
62 const std::string& origin,
63 const std::string& extra_headers);
65 // A response with the appropriate accept header to match the above challenge
66 // key. Each header in |extra_headers| must be terminated with "\r\n".
67 extern std::string WebSocketStandardResponse(const std::string& extra_headers);
69 // This class provides a convenient way to construct a
70 // DeterministicMockClientSocketFactory for WebSocket tests.
71 class WebSocketDeterministicMockClientSocketFactoryMaker {
72 public:
73 WebSocketDeterministicMockClientSocketFactoryMaker();
74 ~WebSocketDeterministicMockClientSocketFactoryMaker();
76 // Tell the factory to create a socket which expects |expect_written| to be
77 // written, and responds with |return_to_read|. The test will fail if the
78 // expected text is not written, or all the bytes are not read. This adds data
79 // for a new mock-socket using AddRawExpections(), and so can be called
80 // multiple times to queue up multiple mock sockets, but usually in those
81 // cases the lower-level AddRawExpections() interface is more appropriate.
82 void SetExpectations(const std::string& expect_written,
83 const std::string& return_to_read);
85 // A low-level interface to permit arbitrary expectations to be added. The
86 // mock sockets will be created in the same order that they were added.
87 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data);
89 // Allow an SSL socket data provider to be added. You must also supply a mock
90 // transport socket for it to use. If the mock SSL handshake fails then the
91 // mock transport socket will connect but have nothing read or written. If the
92 // mock handshake succeeds then the data from the underlying transport socket
93 // will be passed through unchanged (without encryption).
94 void AddSSLSocketDataProvider(
95 scoped_ptr<SSLSocketDataProvider> ssl_socket_data);
97 // Call to get a pointer to the factory, which remains owned by this object.
98 DeterministicMockClientSocketFactory* factory();
100 private:
101 struct Detail;
102 scoped_ptr<Detail> detail_;
104 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker);
107 // This class encapsulates the details of creating a
108 // TestURLRequestContext that returns mock ClientSocketHandles that do what is
109 // required by the tests.
110 struct WebSocketTestURLRequestContextHost {
111 public:
112 WebSocketTestURLRequestContextHost();
113 ~WebSocketTestURLRequestContextHost();
115 void SetExpectations(const std::string& expect_written,
116 const std::string& return_to_read) {
117 maker_.SetExpectations(expect_written, return_to_read);
120 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data);
122 // Allow an SSL socket data provider to be added.
123 void AddSSLSocketDataProvider(
124 scoped_ptr<SSLSocketDataProvider> ssl_socket_data);
126 // Call after calling one of SetExpections() or AddRawExpectations(). The
127 // returned pointer remains owned by this object.
128 TestURLRequestContext* GetURLRequestContext();
130 private:
131 WebSocketDeterministicMockClientSocketFactoryMaker maker_;
132 TestURLRequestContext url_request_context_;
133 TestNetworkDelegate network_delegate_;
134 bool url_request_context_initialized_;
136 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost);
139 } // namespace net
141 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_