OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 5 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
7 | 7 |
8 #include "net/http/http_transaction.h" | 8 #include "net/http/http_transaction.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 TEST_MODE_SYNC_NET_READ = 1 << 1, | 43 TEST_MODE_SYNC_NET_READ = 1 << 1, |
44 TEST_MODE_SYNC_CACHE_START = 1 << 2, | 44 TEST_MODE_SYNC_CACHE_START = 1 << 2, |
45 TEST_MODE_SYNC_CACHE_READ = 1 << 3, | 45 TEST_MODE_SYNC_CACHE_READ = 1 << 3, |
46 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, | 46 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, |
47 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | | 47 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | |
48 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | | 48 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | |
49 TEST_MODE_SYNC_CACHE_WRITE), | 49 TEST_MODE_SYNC_CACHE_WRITE), |
50 TEST_MODE_SLOW_READ = 1 << 5 | 50 TEST_MODE_SLOW_READ = 1 << 5 |
51 }; | 51 }; |
52 | 52 |
53 typedef void (*MockTransactionHandler)(const HttpRequestInfo* request, | 53 using MockTransactionReadHandler = int (*)(int64 content_length, |
54 std::string* response_status, | 54 int64 offset, |
55 std::string* response_headers, | 55 IOBuffer* buf, |
56 std::string* response_data); | 56 int buf_len); |
| 57 using MockTransactionHandler = void (*)(const HttpRequestInfo* request, |
| 58 std::string* response_status, |
| 59 std::string* response_headers, |
| 60 std::string* response_data); |
57 | 61 |
58 struct MockTransaction { | 62 struct MockTransaction { |
59 const char* url; | 63 const char* url; |
60 const char* method; | 64 const char* method; |
61 // If |request_time| is unspecified, the current time will be used. | 65 // If |request_time| is unspecified, the current time will be used. |
62 base::Time request_time; | 66 base::Time request_time; |
63 const char* request_headers; | 67 const char* request_headers; |
64 int load_flags; | 68 int load_flags; |
65 const char* status; | 69 const char* status; |
66 const char* response_headers; | 70 const char* response_headers; |
67 // If |response_time| is unspecified, the current time will be used. | 71 // If |response_time| is unspecified, the current time will be used. |
68 base::Time response_time; | 72 base::Time response_time; |
69 const char* data; | 73 const char* data; |
70 int test_mode; | 74 int test_mode; |
71 MockTransactionHandler handler; | 75 MockTransactionHandler handler; |
| 76 MockTransactionReadHandler read_handler; |
72 scoped_refptr<X509Certificate> cert; | 77 scoped_refptr<X509Certificate> cert; |
73 CertStatus cert_status; | 78 CertStatus cert_status; |
74 int ssl_connection_status; | 79 int ssl_connection_status; |
75 // Value returned by MockNetworkTransaction::Start (potentially | 80 // Value returned by MockNetworkTransaction::Start (potentially |
76 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.) | 81 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.) |
77 Error return_code; | 82 Error return_code; |
78 }; | 83 }; |
79 | 84 |
80 extern const MockTransaction kSimpleGET_Transaction; | 85 extern const MockTransaction kSimpleGET_Transaction; |
81 extern const MockTransaction kSimplePOST_Transaction; | 86 extern const MockTransaction kSimplePOST_Transaction; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 private: | 238 private: |
234 int StartInternal(const HttpRequestInfo* request, | 239 int StartInternal(const HttpRequestInfo* request, |
235 const CompletionCallback& callback, | 240 const CompletionCallback& callback, |
236 const BoundNetLog& net_log); | 241 const BoundNetLog& net_log); |
237 void CallbackLater(const CompletionCallback& callback, int result); | 242 void CallbackLater(const CompletionCallback& callback, int result); |
238 void RunCallback(const CompletionCallback& callback, int result); | 243 void RunCallback(const CompletionCallback& callback, int result); |
239 | 244 |
240 const HttpRequestInfo* request_; | 245 const HttpRequestInfo* request_; |
241 HttpResponseInfo response_; | 246 HttpResponseInfo response_; |
242 std::string data_; | 247 std::string data_; |
243 int data_cursor_; | 248 int64 data_cursor_; |
| 249 int64 content_length_; |
244 int test_mode_; | 250 int test_mode_; |
245 RequestPriority priority_; | 251 RequestPriority priority_; |
| 252 MockTransactionReadHandler read_handler_; |
246 CreateHelper* websocket_handshake_stream_create_helper_; | 253 CreateHelper* websocket_handshake_stream_create_helper_; |
| 254 BeforeNetworkStartCallback before_network_start_callback_; |
247 base::WeakPtr<MockNetworkLayer> transaction_factory_; | 255 base::WeakPtr<MockNetworkLayer> transaction_factory_; |
248 int64 received_bytes_; | 256 int64 received_bytes_; |
249 | 257 |
250 // NetLog ID of the fake / non-existent underlying socket used by the | 258 // NetLog ID of the fake / non-existent underlying socket used by the |
251 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to | 259 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to |
252 // be initialized. | 260 // be initialized. |
253 unsigned int socket_log_id_; | 261 unsigned int socket_log_id_; |
254 | 262 |
255 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; | 263 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; |
256 | 264 |
257 }; | 265 }; |
258 | 266 |
259 class MockNetworkLayer : public HttpTransactionFactory, | 267 class MockNetworkLayer : public HttpTransactionFactory, |
260 public base::SupportsWeakPtr<MockNetworkLayer> { | 268 public base::SupportsWeakPtr<MockNetworkLayer> { |
261 public: | 269 public: |
262 MockNetworkLayer(); | 270 MockNetworkLayer(); |
263 ~MockNetworkLayer() override; | 271 ~MockNetworkLayer() override; |
264 | 272 |
265 int transaction_count() const { return transaction_count_; } | 273 int transaction_count() const { return transaction_count_; } |
266 bool done_reading_called() const { return done_reading_called_; } | 274 bool done_reading_called() const { return done_reading_called_; } |
267 bool stop_caching_called() const { return stop_caching_called_; } | 275 bool stop_caching_called() const { return stop_caching_called_; } |
268 void TransactionDoneReading(); | 276 void TransactionDoneReading(); |
269 void TransactionStopCaching(); | 277 void TransactionStopCaching(); |
270 | 278 |
| 279 // Resets the transaction count. Can be called after test setup in order to |
| 280 // make test expectations independent of how test setup is performed. |
| 281 void ResetTransactionCount(); |
| 282 |
271 // Returns the last priority passed to CreateTransaction, or | 283 // Returns the last priority passed to CreateTransaction, or |
272 // DEFAULT_PRIORITY if it hasn't been called yet. | 284 // DEFAULT_PRIORITY if it hasn't been called yet. |
273 RequestPriority last_create_transaction_priority() const { | 285 RequestPriority last_create_transaction_priority() const { |
274 return last_create_transaction_priority_; | 286 return last_create_transaction_priority_; |
275 } | 287 } |
276 | 288 |
277 // Returns the last transaction created by | 289 // Returns the last transaction created by |
278 // CreateTransaction. Returns a NULL WeakPtr if one has not been | 290 // CreateTransaction. Returns a NULL WeakPtr if one has not been |
279 // created yet, or the last transaction has been destroyed, or | 291 // created yet, or the last transaction has been destroyed, or |
280 // ClearLastTransaction() has been called and a new transaction | 292 // ClearLastTransaction() has been called and a new transaction |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 329 |
318 //----------------------------------------------------------------------------- | 330 //----------------------------------------------------------------------------- |
319 // helpers | 331 // helpers |
320 | 332 |
321 // read the transaction completely | 333 // read the transaction completely |
322 int ReadTransaction(HttpTransaction* trans, std::string* result); | 334 int ReadTransaction(HttpTransaction* trans, std::string* result); |
323 | 335 |
324 } // namespace net | 336 } // namespace net |
325 | 337 |
326 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ | 338 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ |
OLD | NEW |