OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
13 #include "net/websockets/websocket_stream.h" | 13 #include "net/websockets/websocket_stream.h" |
14 | 14 |
15 class GURL; | 15 class GURL; |
16 | 16 |
17 namespace url { | 17 namespace url { |
18 class Origin; | 18 class Origin; |
19 } // namespace url | 19 } // namespace url |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 class BoundNetLog; | 23 class BoundNetLog; |
| 24 class DeterministicMockClientSocketFactory; |
24 class DeterministicSocketData; | 25 class DeterministicSocketData; |
25 class URLRequestContext; | 26 class URLRequestContext; |
26 class WebSocketHandshakeStreamCreateHelper; | 27 class WebSocketHandshakeStreamCreateHelper; |
27 class DeterministicMockClientSocketFactory; | 28 struct SSLSocketDataProvider; |
28 | 29 |
29 class LinearCongruentialGenerator { | 30 class LinearCongruentialGenerator { |
30 public: | 31 public: |
31 explicit LinearCongruentialGenerator(uint32 seed); | 32 explicit LinearCongruentialGenerator(uint32 seed); |
32 uint32 Generate(); | 33 uint32 Generate(); |
33 | 34 |
34 private: | 35 private: |
35 uint64 current_; | 36 uint64 current_; |
36 }; | 37 }; |
37 | 38 |
(...skipping 20 matching lines...) Expand all Loading... |
58 // key. Each header in |extra_headers| must be terminated with "\r\n". | 59 // key. Each header in |extra_headers| must be terminated with "\r\n". |
59 extern std::string WebSocketStandardResponse(const std::string& extra_headers); | 60 extern std::string WebSocketStandardResponse(const std::string& extra_headers); |
60 | 61 |
61 // This class provides a convenient way to construct a | 62 // This class provides a convenient way to construct a |
62 // DeterministicMockClientSocketFactory for WebSocket tests. | 63 // DeterministicMockClientSocketFactory for WebSocket tests. |
63 class WebSocketDeterministicMockClientSocketFactoryMaker { | 64 class WebSocketDeterministicMockClientSocketFactoryMaker { |
64 public: | 65 public: |
65 WebSocketDeterministicMockClientSocketFactoryMaker(); | 66 WebSocketDeterministicMockClientSocketFactoryMaker(); |
66 ~WebSocketDeterministicMockClientSocketFactoryMaker(); | 67 ~WebSocketDeterministicMockClientSocketFactoryMaker(); |
67 | 68 |
68 // The socket created by the factory will expect |expect_written| to be | 69 // Tell the factory to create a socket which expects |expect_written| to be |
69 // written to the socket, and will respond with |return_to_read|. The test | 70 // written, and responds with |return_to_read|. The test will fail if the |
70 // will fail if the expected text is not written, or all the bytes are not | 71 // expected text is not written, or all the bytes are not read. This adds data |
71 // read. | 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. |
72 void SetExpectations(const std::string& expect_written, | 75 void SetExpectations(const std::string& expect_written, |
73 const std::string& return_to_read); | 76 const std::string& return_to_read); |
74 | 77 |
75 // A low-level interface to permit arbitrary expectations to be set. | 78 // A low-level interface to permit arbitrary expectations to be added. The |
76 void SetRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); | 79 // mock sockets will be created in the same order that they were added. |
| 80 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); |
| 81 |
| 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); |
77 | 89 |
78 // Call to get a pointer to the factory, which remains owned by this object. | 90 // Call to get a pointer to the factory, which remains owned by this object. |
79 DeterministicMockClientSocketFactory* factory(); | 91 DeterministicMockClientSocketFactory* factory(); |
80 | 92 |
81 private: | 93 private: |
82 struct Detail; | 94 struct Detail; |
83 scoped_ptr<Detail> detail_; | 95 scoped_ptr<Detail> detail_; |
84 | 96 |
85 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker); | 97 DISALLOW_COPY_AND_ASSIGN(WebSocketDeterministicMockClientSocketFactoryMaker); |
86 }; | 98 }; |
87 | 99 |
88 // This class encapsulates the details of creating a | 100 // This class encapsulates the details of creating a |
89 // TestURLRequestContext that returns mock ClientSocketHandles that do what is | 101 // TestURLRequestContext that returns mock ClientSocketHandles that do what is |
90 // required by the tests. | 102 // required by the tests. |
91 struct WebSocketTestURLRequestContextHost { | 103 struct WebSocketTestURLRequestContextHost { |
92 public: | 104 public: |
93 WebSocketTestURLRequestContextHost(); | 105 WebSocketTestURLRequestContextHost(); |
94 ~WebSocketTestURLRequestContextHost(); | 106 ~WebSocketTestURLRequestContextHost(); |
95 | 107 |
96 void SetExpectations(const std::string& expect_written, | 108 void SetExpectations(const std::string& expect_written, |
97 const std::string& return_to_read) { | 109 const std::string& return_to_read) { |
98 maker_.SetExpectations(expect_written, return_to_read); | 110 maker_.SetExpectations(expect_written, return_to_read); |
99 } | 111 } |
100 | 112 |
101 void SetRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); | 113 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data); |
102 | 114 |
103 // Call after calling one of SetExpections() or SetRawExpectations(). The | 115 // Allow an SSL socket data provider to be added. |
| 116 void AddSSLSocketDataProvider( |
| 117 scoped_ptr<SSLSocketDataProvider> ssl_socket_data); |
| 118 |
| 119 // Call after calling one of SetExpections() or AddRawExpectations(). The |
104 // returned pointer remains owned by this object. This should only be called | 120 // returned pointer remains owned by this object. This should only be called |
105 // once. | 121 // once. |
106 TestURLRequestContext* GetURLRequestContext(); | 122 TestURLRequestContext* GetURLRequestContext(); |
107 | 123 |
108 private: | 124 private: |
109 WebSocketDeterministicMockClientSocketFactoryMaker maker_; | 125 WebSocketDeterministicMockClientSocketFactoryMaker maker_; |
110 TestURLRequestContext url_request_context_; | 126 TestURLRequestContext url_request_context_; |
111 TestNetworkDelegate network_delegate_; | 127 TestNetworkDelegate network_delegate_; |
112 | 128 |
113 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); | 129 DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); |
114 }; | 130 }; |
115 | 131 |
116 } // namespace net | 132 } // namespace net |
117 | 133 |
118 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ | 134 #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
OLD | NEW |