Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: net/http/http_transaction_test_util.h

Issue 992733002: Remove //net (except for Android test stuff) and sdch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_transaction_factory.h ('k') | net/http/http_transaction_test_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
7
8 #include "net/http/http_transaction.h"
9
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_log.h"
20 #include "net/base/request_priority.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/disk_cache/disk_cache.h"
23 #include "net/http/http_cache.h"
24 #include "net/http/http_request_info.h"
25 #include "net/http/http_response_headers.h"
26 #include "net/http/http_response_info.h"
27
28 namespace net {
29 class HttpRequestHeaders;
30 class IOBuffer;
31 struct HttpRequestInfo;
32 }
33
34 //-----------------------------------------------------------------------------
35 // mock transaction data
36
37 // these flags may be combined to form the test_mode field
38 enum {
39 TEST_MODE_NORMAL = 0,
40 TEST_MODE_SYNC_NET_START = 1 << 0,
41 TEST_MODE_SYNC_NET_READ = 1 << 1,
42 TEST_MODE_SYNC_CACHE_START = 1 << 2,
43 TEST_MODE_SYNC_CACHE_READ = 1 << 3,
44 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
45 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ |
46 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ |
47 TEST_MODE_SYNC_CACHE_WRITE),
48 TEST_MODE_SLOW_READ = 1 << 5
49 };
50
51 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request,
52 std::string* response_status,
53 std::string* response_headers,
54 std::string* response_data);
55
56 struct MockTransaction {
57 const char* url;
58 const char* method;
59 // If |request_time| is unspecified, the current time will be used.
60 base::Time request_time;
61 const char* request_headers;
62 int load_flags;
63 const char* status;
64 const char* response_headers;
65 // If |response_time| is unspecified, the current time will be used.
66 base::Time response_time;
67 const char* data;
68 int test_mode;
69 MockTransactionHandler handler;
70 net::CertStatus cert_status;
71 // Value returned by MockNetworkTransaction::Start (potentially
72 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
73 net::Error return_code;
74 };
75
76 extern const MockTransaction kSimpleGET_Transaction;
77 extern const MockTransaction kSimplePOST_Transaction;
78 extern const MockTransaction kTypicalGET_Transaction;
79 extern const MockTransaction kETagGET_Transaction;
80 extern const MockTransaction kRangeGET_Transaction;
81
82 // returns the mock transaction for the given URL
83 const MockTransaction* FindMockTransaction(const GURL& url);
84
85 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
86 // There can be only one MockTransaction associated with a given URL.
87 void AddMockTransaction(const MockTransaction* trans);
88 void RemoveMockTransaction(const MockTransaction* trans);
89
90 struct ScopedMockTransaction : MockTransaction {
91 ScopedMockTransaction() {
92 AddMockTransaction(this);
93 }
94 explicit ScopedMockTransaction(const MockTransaction& t)
95 : MockTransaction(t) {
96 AddMockTransaction(this);
97 }
98 ~ScopedMockTransaction() {
99 RemoveMockTransaction(this);
100 }
101 };
102
103 //-----------------------------------------------------------------------------
104 // mock http request
105
106 class MockHttpRequest : public net::HttpRequestInfo {
107 public:
108 explicit MockHttpRequest(const MockTransaction& t);
109 };
110
111 //-----------------------------------------------------------------------------
112 // use this class to test completely consuming a transaction
113
114 class TestTransactionConsumer {
115 public:
116 TestTransactionConsumer(net::RequestPriority priority,
117 net::HttpTransactionFactory* factory);
118 virtual ~TestTransactionConsumer();
119
120 void Start(const net::HttpRequestInfo* request,
121 const net::BoundNetLog& net_log);
122
123 bool is_done() const { return state_ == DONE; }
124 int error() const { return error_; }
125
126 const net::HttpResponseInfo* response_info() const {
127 return trans_->GetResponseInfo();
128 }
129 const std::string& content() const { return content_; }
130
131 private:
132 enum State {
133 IDLE,
134 STARTING,
135 READING,
136 DONE
137 };
138
139 void DidStart(int result);
140 void DidRead(int result);
141 void DidFinish(int result);
142 void Read();
143
144 void OnIOComplete(int result);
145
146 State state_;
147 scoped_ptr<net::HttpTransaction> trans_;
148 std::string content_;
149 scoped_refptr<net::IOBuffer> read_buf_;
150 int error_;
151
152 static int quit_counter_;
153 };
154
155 //-----------------------------------------------------------------------------
156 // mock network layer
157
158 class MockNetworkLayer;
159
160 // This transaction class inspects the available set of mock transactions to
161 // find data for the request URL. It supports IO operations that complete
162 // synchronously or asynchronously to help exercise different code paths in the
163 // HttpCache implementation.
164 class MockNetworkTransaction
165 : public net::HttpTransaction,
166 public base::SupportsWeakPtr<MockNetworkTransaction> {
167 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
168 public:
169 MockNetworkTransaction(net::RequestPriority priority,
170 MockNetworkLayer* factory);
171 ~MockNetworkTransaction() override;
172
173 int Start(const net::HttpRequestInfo* request,
174 const net::CompletionCallback& callback,
175 const net::BoundNetLog& net_log) override;
176
177 int RestartIgnoringLastError(
178 const net::CompletionCallback& callback) override;
179
180 int RestartWithCertificate(net::X509Certificate* client_cert,
181 const net::CompletionCallback& callback) override;
182
183 int RestartWithAuth(const net::AuthCredentials& credentials,
184 const net::CompletionCallback& callback) override;
185
186 bool IsReadyToRestartForAuth() override;
187
188 int Read(net::IOBuffer* buf,
189 int buf_len,
190 const net::CompletionCallback& callback) override;
191
192 void StopCaching() override;
193
194 bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override;
195
196 int64 GetTotalReceivedBytes() const override;
197
198 void DoneReading() override;
199
200 const net::HttpResponseInfo* GetResponseInfo() const override;
201
202 net::LoadState GetLoadState() const override;
203
204 net::UploadProgress GetUploadProgress() const override;
205
206 void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override;
207
208 bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
209
210 void SetPriority(net::RequestPriority priority) override;
211
212 void SetWebSocketHandshakeStreamCreateHelper(
213 CreateHelper* create_helper) override;
214
215 void SetBeforeNetworkStartCallback(
216 const BeforeNetworkStartCallback& callback) override;
217
218 void SetBeforeProxyHeadersSentCallback(
219 const BeforeProxyHeadersSentCallback& callback) override;
220
221 int ResumeNetworkStart() override;
222
223 CreateHelper* websocket_handshake_stream_create_helper() {
224 return websocket_handshake_stream_create_helper_;
225 }
226 net::RequestPriority priority() const { return priority_; }
227 const net::HttpRequestInfo* request() const { return request_; }
228
229 private:
230 int StartInternal(const net::HttpRequestInfo* request,
231 const net::CompletionCallback& callback,
232 const net::BoundNetLog& net_log);
233 void CallbackLater(const net::CompletionCallback& callback, int result);
234 void RunCallback(const net::CompletionCallback& callback, int result);
235
236 const net::HttpRequestInfo* request_;
237 net::HttpResponseInfo response_;
238 std::string data_;
239 int data_cursor_;
240 int test_mode_;
241 net::RequestPriority priority_;
242 CreateHelper* websocket_handshake_stream_create_helper_;
243 base::WeakPtr<MockNetworkLayer> transaction_factory_;
244 int64 received_bytes_;
245
246 // NetLog ID of the fake / non-existent underlying socket used by the
247 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
248 // be initialized.
249 unsigned int socket_log_id_;
250
251 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
252
253 };
254
255 class MockNetworkLayer : public net::HttpTransactionFactory,
256 public base::SupportsWeakPtr<MockNetworkLayer> {
257 public:
258 MockNetworkLayer();
259 ~MockNetworkLayer() override;
260
261 int transaction_count() const { return transaction_count_; }
262 bool done_reading_called() const { return done_reading_called_; }
263 bool stop_caching_called() const { return stop_caching_called_; }
264 void TransactionDoneReading();
265 void TransactionStopCaching();
266
267 // Returns the last priority passed to CreateTransaction, or
268 // DEFAULT_PRIORITY if it hasn't been called yet.
269 net::RequestPriority last_create_transaction_priority() const {
270 return last_create_transaction_priority_;
271 }
272
273 // Returns the last transaction created by
274 // CreateTransaction. Returns a NULL WeakPtr if one has not been
275 // created yet, or the last transaction has been destroyed, or
276 // ClearLastTransaction() has been called and a new transaction
277 // hasn't been created yet.
278 base::WeakPtr<MockNetworkTransaction> last_transaction() {
279 return last_transaction_;
280 }
281
282 // Makes last_transaction() return NULL until the next transaction
283 // is created.
284 void ClearLastTransaction() {
285 last_transaction_.reset();
286 }
287
288 // net::HttpTransactionFactory:
289 int CreateTransaction(net::RequestPriority priority,
290 scoped_ptr<net::HttpTransaction>* trans) override;
291 net::HttpCache* GetCache() override;
292 net::HttpNetworkSession* GetSession() override;
293
294 // The caller must guarantee that |clock| will outlive this object.
295 void SetClock(base::Clock* clock);
296 base::Clock* clock() const { return clock_; }
297
298 // The current time (will use clock_ if it is non NULL).
299 base::Time Now();
300
301 private:
302 int transaction_count_;
303 bool done_reading_called_;
304 bool stop_caching_called_;
305 net::RequestPriority last_create_transaction_priority_;
306
307 // By default clock_ is NULL but it can be set to a custom clock by test
308 // frameworks using SetClock.
309 base::Clock* clock_;
310
311 base::WeakPtr<MockNetworkTransaction> last_transaction_;
312 };
313
314 //-----------------------------------------------------------------------------
315 // helpers
316
317 // read the transaction completely
318 int ReadTransaction(net::HttpTransaction* trans, std::string* result);
319
320 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
OLDNEW
« no previous file with comments | « net/http/http_transaction_factory.h ('k') | net/http/http_transaction_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698