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