1 // Copyright (c) 2012 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 } 32 33 //----------------------------------------------------------------------------- 34 // mock transaction data 35 36 // these flags may be combined to form the test_mode field 37 enum { 38 TEST_MODE_NORMAL = 0, 39 TEST_MODE_SYNC_NET_START = 1 << 0, 40 TEST_MODE_SYNC_NET_READ = 1 << 1, 41 TEST_MODE_SYNC_CACHE_START = 1 << 2, 42 TEST_MODE_SYNC_CACHE_READ = 1 << 3, 43 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4, 44 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ | 45 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ | 46 TEST_MODE_SYNC_CACHE_WRITE) 47 }; 48 49 typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request, 50 std::string* response_status, 51 std::string* response_headers, 52 std::string* response_data); 53 54 struct MockTransaction { 55 const char* url; 56 const char* method; 57 // If |request_time| is unspecified, the current time will be used. 58 base::Time request_time; 59 const char* request_headers; 60 int load_flags; 61 const char* status; 62 const char* response_headers; 63 // If |response_time| is unspecified, the current time will be used. 64 base::Time response_time; 65 const char* data; 66 int test_mode; 67 MockTransactionHandler handler; 68 net::CertStatus cert_status; 69 // Value returned by MockNetworkTransaction::Start (potentially 70 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.) 71 net::Error return_code; 72 }; 73 74 extern const MockTransaction kSimpleGET_Transaction; 75 extern const MockTransaction kSimplePOST_Transaction; 76 extern const MockTransaction kTypicalGET_Transaction; 77 extern const MockTransaction kETagGET_Transaction; 78 extern const MockTransaction kRangeGET_Transaction; 79 80 // returns the mock transaction for the given URL 81 const MockTransaction* FindMockTransaction(const GURL& url); 82 83 // Add/Remove a mock transaction that can be accessed via FindMockTransaction. 84 // There can be only one MockTransaction associated with a given URL. 85 void AddMockTransaction(const MockTransaction* trans); 86 void RemoveMockTransaction(const MockTransaction* trans); 87 88 struct ScopedMockTransaction : MockTransaction { ScopedMockTransactionScopedMockTransaction89 ScopedMockTransaction() { 90 AddMockTransaction(this); 91 } ScopedMockTransactionScopedMockTransaction92 explicit ScopedMockTransaction(const MockTransaction& t) 93 : MockTransaction(t) { 94 AddMockTransaction(this); 95 } ~ScopedMockTransactionScopedMockTransaction96 ~ScopedMockTransaction() { 97 RemoveMockTransaction(this); 98 } 99 }; 100 101 //----------------------------------------------------------------------------- 102 // mock http request 103 104 class MockHttpRequest : public net::HttpRequestInfo { 105 public: 106 explicit MockHttpRequest(const MockTransaction& t); 107 }; 108 109 //----------------------------------------------------------------------------- 110 // use this class to test completely consuming a transaction 111 112 class TestTransactionConsumer { 113 public: 114 TestTransactionConsumer(net::RequestPriority priority, 115 net::HttpTransactionFactory* factory); 116 virtual ~TestTransactionConsumer(); 117 118 void Start(const net::HttpRequestInfo* request, 119 const net::BoundNetLog& net_log); 120 is_done()121 bool is_done() const { return state_ == DONE; } error()122 int error() const { return error_; } 123 response_info()124 const net::HttpResponseInfo* response_info() const { 125 return trans_->GetResponseInfo(); 126 } content()127 const std::string& content() const { return content_; } 128 129 private: 130 enum State { 131 IDLE, 132 STARTING, 133 READING, 134 DONE 135 }; 136 137 void DidStart(int result); 138 void DidRead(int result); 139 void DidFinish(int result); 140 void Read(); 141 142 void OnIOComplete(int result); 143 144 State state_; 145 scoped_ptr<net::HttpTransaction> trans_; 146 std::string content_; 147 scoped_refptr<net::IOBuffer> read_buf_; 148 int error_; 149 150 static int quit_counter_; 151 }; 152 153 //----------------------------------------------------------------------------- 154 // mock network layer 155 156 class MockNetworkLayer; 157 158 // This transaction class inspects the available set of mock transactions to 159 // find data for the request URL. It supports IO operations that complete 160 // synchronously or asynchronously to help exercise different code paths in the 161 // HttpCache implementation. 162 class MockNetworkTransaction 163 : public net::HttpTransaction, 164 public base::SupportsWeakPtr<MockNetworkTransaction> { 165 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper; 166 public: 167 MockNetworkTransaction(net::RequestPriority priority, 168 MockNetworkLayer* factory); 169 virtual ~MockNetworkTransaction(); 170 171 virtual int Start(const net::HttpRequestInfo* request, 172 const net::CompletionCallback& callback, 173 const net::BoundNetLog& net_log) OVERRIDE; 174 175 virtual int RestartIgnoringLastError( 176 const net::CompletionCallback& callback) OVERRIDE; 177 178 virtual int RestartWithCertificate( 179 net::X509Certificate* client_cert, 180 const net::CompletionCallback& callback) OVERRIDE; 181 182 virtual int RestartWithAuth( 183 const net::AuthCredentials& credentials, 184 const net::CompletionCallback& callback) OVERRIDE; 185 186 virtual bool IsReadyToRestartForAuth() OVERRIDE; 187 188 virtual int Read(net::IOBuffer* buf, int buf_len, 189 const net::CompletionCallback& callback) OVERRIDE; 190 191 virtual void StopCaching() OVERRIDE; 192 193 virtual bool GetFullRequestHeaders( 194 net::HttpRequestHeaders* headers) const OVERRIDE; 195 196 virtual void DoneReading() OVERRIDE; 197 198 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE; 199 200 virtual net::LoadState GetLoadState() const OVERRIDE; 201 202 virtual net::UploadProgress GetUploadProgress() const OVERRIDE; 203 204 virtual bool GetLoadTimingInfo( 205 net::LoadTimingInfo* load_timing_info) const OVERRIDE; 206 207 virtual void SetPriority(net::RequestPriority priority) OVERRIDE; 208 209 virtual void SetWebSocketHandshakeStreamCreateHelper( 210 CreateHelper* create_helper) OVERRIDE; 211 websocket_handshake_stream_create_helper()212 CreateHelper* websocket_handshake_stream_create_helper() { 213 return websocket_handshake_stream_create_helper_; 214 } priority()215 net::RequestPriority priority() const { return priority_; } 216 217 private: 218 void CallbackLater(const net::CompletionCallback& callback, int result); 219 void RunCallback(const net::CompletionCallback& callback, int result); 220 221 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_; 222 net::HttpResponseInfo response_; 223 std::string data_; 224 int data_cursor_; 225 int test_mode_; 226 net::RequestPriority priority_; 227 CreateHelper* websocket_handshake_stream_create_helper_; 228 base::WeakPtr<MockNetworkLayer> transaction_factory_; 229 230 // NetLog ID of the fake / non-existent underlying socket used by the 231 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to 232 // be initialized. 233 unsigned int socket_log_id_; 234 }; 235 236 class MockNetworkLayer : public net::HttpTransactionFactory, 237 public base::SupportsWeakPtr<MockNetworkLayer> { 238 public: 239 MockNetworkLayer(); 240 virtual ~MockNetworkLayer(); 241 transaction_count()242 int transaction_count() const { return transaction_count_; } done_reading_called()243 bool done_reading_called() const { return done_reading_called_; } 244 void TransactionDoneReading(); 245 246 // Returns the last priority passed to CreateTransaction, or 247 // DEFAULT_PRIORITY if it hasn't been called yet. last_create_transaction_priority()248 net::RequestPriority last_create_transaction_priority() const { 249 return last_create_transaction_priority_; 250 } 251 252 // Returns the last transaction created by 253 // CreateTransaction. Returns a NULL WeakPtr if one has not been 254 // created yet, or the last transaction has been destroyed, or 255 // ClearLastTransaction() has been called and a new transaction 256 // hasn't been created yet. last_transaction()257 base::WeakPtr<MockNetworkTransaction> last_transaction() { 258 return last_transaction_; 259 } 260 261 // Makes last_transaction() return NULL until the next transaction 262 // is created. ClearLastTransaction()263 void ClearLastTransaction() { 264 last_transaction_.reset(); 265 } 266 267 // net::HttpTransactionFactory: 268 virtual int CreateTransaction( 269 net::RequestPriority priority, 270 scoped_ptr<net::HttpTransaction>* trans, 271 net::HttpTransactionDelegate* delegate) OVERRIDE; 272 virtual net::HttpCache* GetCache() OVERRIDE; 273 virtual net::HttpNetworkSession* GetSession() OVERRIDE; 274 275 private: 276 int transaction_count_; 277 bool done_reading_called_; 278 net::RequestPriority last_create_transaction_priority_; 279 base::WeakPtr<MockNetworkTransaction> last_transaction_; 280 }; 281 282 //----------------------------------------------------------------------------- 283 // helpers 284 285 // read the transaction completely 286 int ReadTransaction(net::HttpTransaction* trans, std::string* result); 287 288 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_ 289