Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / net / websockets / websocket_test_util.h
blobf620f18d6b48577d96de403b4212abd5740e1c62
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 MockClientSocketFactory;
29 class ProxyService;
30 class SequencedSocketData;
31 struct SSLSocketDataProvider;
32 class URLRequestContext;
34 class LinearCongruentialGenerator {
35 public:
36 explicit LinearCongruentialGenerator(uint32 seed);
37 uint32 Generate();
39 private:
40 uint64 current_;
43 // Generates a standard WebSocket handshake request. The challenge key used is
44 // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated
45 // with "\r\n".
46 std::string WebSocketStandardRequest(const std::string& path,
47 const std::string& host,
48 const url::Origin& 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 url::Origin& 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 MockClientSocketFactory
66 // for WebSocket tests.
67 class WebSocketMockClientSocketFactoryMaker {
68 public:
69 WebSocketMockClientSocketFactoryMaker();
70 ~WebSocketMockClientSocketFactoryMaker();
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<SequencedSocketData> 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 MockClientSocketFactory* factory();
96 private:
97 struct Detail;
98 scoped_ptr<Detail> detail_;
100 DISALLOW_COPY_AND_ASSIGN(WebSocketMockClientSocketFactoryMaker);
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 {
107 public:
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<SequencedSocketData> 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();
132 private:
133 WebSocketMockClientSocketFactoryMaker 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);
142 } // namespace net
144 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_