blob: b7027833dc5b1d7eac4b6b58e8ce1b5461de5bdc [file] [log] [blame]
[email protected]c41737d2014-05-14 07:47:191// Copyright 2014 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
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
sclittlefb249892015-09-10 21:33:2210#include <stdint.h>
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <string>
13
[email protected]2041cf342010-02-19 03:15:5914#include "base/callback.h"
[email protected]cad155b2008-09-23 14:44:2715#include "base/compiler_specific.h"
[email protected]5033ab82013-03-22 20:17:4616#include "base/memory/weak_ptr.h"
[email protected]125ef482013-06-11 18:32:4717#include "base/strings/string16.h"
[email protected]74a85ce2009-02-12 00:03:1918#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5219#include "net/base/load_flags.h"
zhongyi48704c182015-12-07 07:52:0220#include "net/base/net_error_details.h"
[email protected]d8eb84242010-09-25 02:25:0621#include "net/base/net_errors.h"
[email protected]262eec82013-03-19 21:01:3622#include "net/base/request_priority.h"
initial.commit586acc5fe2008-07-26 22:42:5223#include "net/base/test_completion_callback.h"
24#include "net/disk_cache/disk_cache.h"
25#include "net/http/http_cache.h"
26#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2127#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5228#include "net/http/http_response_info.h"
eroman87c53d62015-04-02 06:51:0729#include "net/log/net_log.h"
ttuttle1f2d7e92015-04-28 16:17:4730#include "net/socket/connection_attempts.h"
initial.commit586acc5fe2008-07-26 22:42:5231
[email protected]d2db0292011-01-26 20:23:4432namespace net {
ttuttle859dc7a2015-04-23 19:42:2933
[email protected]79e1fd62013-06-20 06:50:0434class HttpRequestHeaders;
[email protected]d2db0292011-01-26 20:23:4435class IOBuffer;
svaldez7872fd02015-11-19 21:10:5436class SSLPrivateKey;
davidben2ec0ed342015-06-08 21:17:2837class X509Certificate;
[email protected]33acd5b2014-08-19 19:56:2238struct HttpRequestInfo;
[email protected]d2db0292011-01-26 20:23:4439
initial.commit586acc5fe2008-07-26 22:42:5240//-----------------------------------------------------------------------------
41// mock transaction data
42
43// these flags may be combined to form the test_mode field
44enum {
45 TEST_MODE_NORMAL = 0,
46 TEST_MODE_SYNC_NET_START = 1 << 0,
47 TEST_MODE_SYNC_NET_READ = 1 << 1,
48 TEST_MODE_SYNC_CACHE_START = 1 << 2,
49 TEST_MODE_SYNC_CACHE_READ = 1 << 3,
[email protected]73cae572009-10-22 18:36:1950 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
[email protected]2227c692010-05-04 15:36:1151 TEST_MODE_SYNC_ALL = (TEST_MODE_SYNC_NET_START | TEST_MODE_SYNC_NET_READ |
52 TEST_MODE_SYNC_CACHE_START | TEST_MODE_SYNC_CACHE_READ |
[email protected]0ede7cc52014-05-03 03:30:0753 TEST_MODE_SYNC_CACHE_WRITE),
54 TEST_MODE_SLOW_READ = 1 << 5
initial.commit586acc5fe2008-07-26 22:42:5255};
56
asankab5fb4b42016-06-29 11:06:0857using MockTransactionReadHandler = int (*)(int64_t content_length,
58 int64_t offset,
59 IOBuffer* buf,
60 int buf_len);
61using MockTransactionHandler = void (*)(const HttpRequestInfo* request,
62 std::string* response_status,
63 std::string* response_headers,
64 std::string* response_data);
initial.commit586acc5fe2008-07-26 22:42:5265
66struct MockTransaction {
67 const char* url;
68 const char* method;
[email protected]ca2f19e2009-09-04 22:53:1669 // If |request_time| is unspecified, the current time will be used.
70 base::Time request_time;
initial.commit586acc5fe2008-07-26 22:42:5271 const char* request_headers;
72 int load_flags;
73 const char* status;
74 const char* response_headers;
[email protected]207d58c72009-09-04 18:59:2975 // If |response_time| is unspecified, the current time will be used.
76 base::Time response_time;
initial.commit586acc5fe2008-07-26 22:42:5277 const char* data;
78 int test_mode;
79 MockTransactionHandler handler;
asankab5fb4b42016-06-29 11:06:0880 MockTransactionReadHandler read_handler;
davidben2ec0ed342015-06-08 21:17:2881 scoped_refptr<X509Certificate> cert;
ttuttle859dc7a2015-04-23 19:42:2982 CertStatus cert_status;
davidben2ec0ed342015-06-08 21:17:2883 int ssl_connection_status;
[email protected]2ef5d00e2013-03-23 16:17:2784 // Value returned by MockNetworkTransaction::Start (potentially
85 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
ttuttle859dc7a2015-04-23 19:42:2986 Error return_code;
initial.commit586acc5fe2008-07-26 22:42:5287};
88
89extern const MockTransaction kSimpleGET_Transaction;
90extern const MockTransaction kSimplePOST_Transaction;
91extern const MockTransaction kTypicalGET_Transaction;
92extern const MockTransaction kETagGET_Transaction;
93extern const MockTransaction kRangeGET_Transaction;
94
95// returns the mock transaction for the given URL
96const MockTransaction* FindMockTransaction(const GURL& url);
97
98// Add/Remove a mock transaction that can be accessed via FindMockTransaction.
99// There can be only one MockTransaction associated with a given URL.
100void AddMockTransaction(const MockTransaction* trans);
101void RemoveMockTransaction(const MockTransaction* trans);
102
103struct ScopedMockTransaction : MockTransaction {
104 ScopedMockTransaction() {
105 AddMockTransaction(this);
106 }
107 explicit ScopedMockTransaction(const MockTransaction& t)
108 : MockTransaction(t) {
109 AddMockTransaction(this);
110 }
111 ~ScopedMockTransaction() {
112 RemoveMockTransaction(this);
113 }
114};
115
116//-----------------------------------------------------------------------------
117// mock http request
118
ttuttle859dc7a2015-04-23 19:42:29119class MockHttpRequest : public HttpRequestInfo {
initial.commit586acc5fe2008-07-26 22:42:52120 public:
[email protected]d2db0292011-01-26 20:23:44121 explicit MockHttpRequest(const MockTransaction& t);
initial.commit586acc5fe2008-07-26 22:42:52122};
123
124//-----------------------------------------------------------------------------
125// use this class to test completely consuming a transaction
126
[email protected]b35f629f2011-12-23 02:53:32127class TestTransactionConsumer {
initial.commit586acc5fe2008-07-26 22:42:52128 public:
ttuttle859dc7a2015-04-23 19:42:29129 TestTransactionConsumer(RequestPriority priority,
130 HttpTransactionFactory* factory);
[email protected]d2db0292011-01-26 20:23:44131 virtual ~TestTransactionConsumer();
initial.commit586acc5fe2008-07-26 22:42:52132
ttuttle859dc7a2015-04-23 19:42:29133 void Start(const HttpRequestInfo* request, const BoundNetLog& net_log);
initial.commit586acc5fe2008-07-26 22:42:52134
135 bool is_done() const { return state_ == DONE; }
136 int error() const { return error_; }
137
ttuttle859dc7a2015-04-23 19:42:29138 const HttpResponseInfo* response_info() const {
initial.commit586acc5fe2008-07-26 22:42:52139 return trans_->GetResponseInfo();
140 }
nharperd5cddca2016-02-27 03:37:52141 const HttpTransaction* transaction() const { return trans_.get(); }
initial.commit586acc5fe2008-07-26 22:42:52142 const std::string& content() const { return content_; }
143
144 private:
initial.commit586acc5fe2008-07-26 22:42:52145 enum State {
146 IDLE,
147 STARTING,
148 READING,
149 DONE
[email protected]d2db0292011-01-26 20:23:44150 };
initial.commit586acc5fe2008-07-26 22:42:52151
[email protected]d2db0292011-01-26 20:23:44152 void DidStart(int result);
153 void DidRead(int result);
154 void DidFinish(int result);
155 void Read();
156
[email protected]b35f629f2011-12-23 02:53:32157 void OnIOComplete(int result);
[email protected]d2db0292011-01-26 20:23:44158
159 State state_;
danakj1fd259a02016-04-16 03:17:09160 std::unique_ptr<HttpTransaction> trans_;
initial.commit586acc5fe2008-07-26 22:42:52161 std::string content_;
ttuttle859dc7a2015-04-23 19:42:29162 scoped_refptr<IOBuffer> read_buf_;
initial.commit586acc5fe2008-07-26 22:42:52163 int error_;
164
165 static int quit_counter_;
166};
167
168//-----------------------------------------------------------------------------
169// mock network layer
170
[email protected]5c04f722011-08-12 17:52:47171class MockNetworkLayer;
172
initial.commit586acc5fe2008-07-26 22:42:52173// This transaction class inspects the available set of mock transactions to
174// find data for the request URL. It supports IO operations that complete
175// synchronously or asynchronously to help exercise different code paths in the
176// HttpCache implementation.
[email protected]5033ab82013-03-22 20:17:46177class MockNetworkTransaction
ttuttle859dc7a2015-04-23 19:42:29178 : public HttpTransaction,
[email protected]5033ab82013-03-22 20:17:46179 public base::SupportsWeakPtr<MockNetworkTransaction> {
ttuttle859dc7a2015-04-23 19:42:29180 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
181
initial.commit586acc5fe2008-07-26 22:42:52182 public:
ttuttle859dc7a2015-04-23 19:42:29183 MockNetworkTransaction(RequestPriority priority, MockNetworkLayer* factory);
dchengb03027d2014-10-21 12:00:20184 ~MockNetworkTransaction() override;
initial.commit586acc5fe2008-07-26 22:42:52185
ttuttle859dc7a2015-04-23 19:42:29186 int Start(const HttpRequestInfo* request,
187 const CompletionCallback& callback,
188 const BoundNetLog& net_log) override;
initial.commit586acc5fe2008-07-26 22:42:52189
ttuttle859dc7a2015-04-23 19:42:29190 int RestartIgnoringLastError(const CompletionCallback& callback) override;
initial.commit586acc5fe2008-07-26 22:42:52191
ttuttle859dc7a2015-04-23 19:42:29192 int RestartWithCertificate(X509Certificate* client_cert,
svaldez7872fd02015-11-19 21:10:54193 SSLPrivateKey* client_private_key,
ttuttle859dc7a2015-04-23 19:42:29194 const CompletionCallback& callback) override;
[email protected]0b45559b2009-06-12 21:45:11195
ttuttle859dc7a2015-04-23 19:42:29196 int RestartWithAuth(const AuthCredentials& credentials,
197 const CompletionCallback& callback) override;
initial.commit586acc5fe2008-07-26 22:42:52198
dchengb03027d2014-10-21 12:00:20199 bool IsReadyToRestartForAuth() override;
[email protected]0757e7702009-03-27 04:00:22200
ttuttle859dc7a2015-04-23 19:42:29201 int Read(IOBuffer* buf,
dchengb03027d2014-10-21 12:00:20202 int buf_len,
ttuttle859dc7a2015-04-23 19:42:29203 const CompletionCallback& callback) override;
zhongyi48704c182015-12-07 07:52:02204 void PopulateNetErrorDetails(NetErrorDetails* details) const override;
initial.commit586acc5fe2008-07-26 22:42:52205
dchengb03027d2014-10-21 12:00:20206 void StopCaching() override;
initial.commit586acc5fe2008-07-26 22:42:52207
ttuttle859dc7a2015-04-23 19:42:29208 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override;
[email protected]79e1fd62013-06-20 06:50:04209
sclittle4de1bab92015-09-22 21:28:24210 int64_t GetTotalReceivedBytes() const override;
[email protected]b8015c42013-12-24 15:18:19211
sclittlefb249892015-09-10 21:33:22212 int64_t GetTotalSentBytes() const override;
213
dchengb03027d2014-10-21 12:00:20214 void DoneReading() override;
[email protected]5c04f722011-08-12 17:52:47215
ttuttle859dc7a2015-04-23 19:42:29216 const HttpResponseInfo* GetResponseInfo() const override;
[email protected]9dd90e52010-02-23 19:15:01217
ttuttle859dc7a2015-04-23 19:42:29218 LoadState GetLoadState() const override;
initial.commit586acc5fe2008-07-26 22:42:52219
ttuttle859dc7a2015-04-23 19:42:29220 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override;
[email protected]8cd06c02014-01-25 07:50:14221
ttuttle859dc7a2015-04-23 19:42:29222 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
[email protected]58e32bb2013-01-21 18:23:25223
ttuttled9dbc652015-09-29 20:00:59224 bool GetRemoteEndpoint(IPEndPoint* endpoint) const override;
225
ttuttle859dc7a2015-04-23 19:42:29226 void SetPriority(RequestPriority priority) override;
[email protected]5033ab82013-03-22 20:17:46227
dchengb03027d2014-10-21 12:00:20228 void SetWebSocketHandshakeStreamCreateHelper(
mostynbba063d6032014-10-09 11:01:13229 CreateHelper* create_helper) override;
[email protected]831e4a32013-11-14 02:14:44230
dchengb03027d2014-10-21 12:00:20231 void SetBeforeNetworkStartCallback(
mostynbba063d6032014-10-09 11:01:13232 const BeforeNetworkStartCallback& callback) override;
[email protected]1826a402014-01-08 15:40:48233
ryansturm49a8cb12016-06-15 16:51:09234 void SetBeforeHeadersSentCallback(
235 const BeforeHeadersSentCallback& callback) override;
[email protected]597a1ab2014-06-26 08:12:27236
dchengb03027d2014-10-21 12:00:20237 int ResumeNetworkStart() override;
[email protected]1826a402014-01-08 15:40:48238
ttuttle1f2d7e92015-04-28 16:17:47239 void GetConnectionAttempts(ConnectionAttempts* out) const override;
240
[email protected]831e4a32013-11-14 02:14:44241 CreateHelper* websocket_handshake_stream_create_helper() {
242 return websocket_handshake_stream_create_helper_;
243 }
ttuttle859dc7a2015-04-23 19:42:29244 RequestPriority priority() const { return priority_; }
245 const HttpRequestInfo* request() const { return request_; }
[email protected]5033ab82013-03-22 20:17:46246
sclittlefb249892015-09-10 21:33:22247 // Bogus value that will be returned by GetTotalReceivedBytes() if the
248 // MockNetworkTransaction was started.
249 static const int64_t kTotalReceivedBytes;
250 // Bogus value that will be returned by GetTotalSentBytes() if the
251 // MockNetworkTransaction was started.
252 static const int64_t kTotalSentBytes;
253
initial.commit586acc5fe2008-07-26 22:42:52254 private:
ttuttle859dc7a2015-04-23 19:42:29255 int StartInternal(const HttpRequestInfo* request,
256 const CompletionCallback& callback,
257 const BoundNetLog& net_log);
258 void CallbackLater(const CompletionCallback& callback, int result);
259 void RunCallback(const CompletionCallback& callback, int result);
initial.commit586acc5fe2008-07-26 22:42:52260
ttuttle859dc7a2015-04-23 19:42:29261 const HttpRequestInfo* request_;
262 HttpResponseInfo response_;
initial.commit586acc5fe2008-07-26 22:42:52263 std::string data_;
asankab5fb4b42016-06-29 11:06:08264 int64_t data_cursor_;
265 int64_t content_length_;
initial.commit586acc5fe2008-07-26 22:42:52266 int test_mode_;
ttuttle859dc7a2015-04-23 19:42:29267 RequestPriority priority_;
asankab5fb4b42016-06-29 11:06:08268 MockTransactionReadHandler read_handler_;
[email protected]831e4a32013-11-14 02:14:44269 CreateHelper* websocket_handshake_stream_create_helper_;
asankab5fb4b42016-06-29 11:06:08270 BeforeNetworkStartCallback before_network_start_callback_;
[email protected]5c04f722011-08-12 17:52:47271 base::WeakPtr<MockNetworkLayer> transaction_factory_;
sclittle4de1bab92015-09-22 21:28:24272 int64_t received_bytes_;
sclittlefb249892015-09-10 21:33:22273 int64_t sent_bytes_;
[email protected]3b23a222013-05-15 21:33:25274
275 // NetLog ID of the fake / non-existent underlying socket used by the
276 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
277 // be initialized.
278 unsigned int socket_log_id_;
kulkarni.acd7b4462014-08-28 07:41:34279
xunjieli26ede962015-11-23 19:39:13280 bool done_reading_called_;
281
kulkarni.acd7b4462014-08-28 07:41:34282 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
283
initial.commit586acc5fe2008-07-26 22:42:52284};
285
ttuttle859dc7a2015-04-23 19:42:29286class MockNetworkLayer : public HttpTransactionFactory,
[email protected]5c04f722011-08-12 17:52:47287 public base::SupportsWeakPtr<MockNetworkLayer> {
initial.commit586acc5fe2008-07-26 22:42:52288 public:
[email protected]d2db0292011-01-26 20:23:44289 MockNetworkLayer();
dchengb03027d2014-10-21 12:00:20290 ~MockNetworkLayer() override;
initial.commit586acc5fe2008-07-26 22:42:52291
292 int transaction_count() const { return transaction_count_; }
[email protected]5c04f722011-08-12 17:52:47293 bool done_reading_called() const { return done_reading_called_; }
[email protected]e50efea2014-03-24 18:41:00294 bool stop_caching_called() const { return stop_caching_called_; }
[email protected]5c04f722011-08-12 17:52:47295 void TransactionDoneReading();
[email protected]e50efea2014-03-24 18:41:00296 void TransactionStopCaching();
initial.commit586acc5fe2008-07-26 22:42:52297
asankab5fb4b42016-06-29 11:06:08298 // Resets the transaction count. Can be called after test setup in order to
299 // make test expectations independent of how test setup is performed.
300 void ResetTransactionCount();
301
[email protected]5033ab82013-03-22 20:17:46302 // Returns the last priority passed to CreateTransaction, or
303 // DEFAULT_PRIORITY if it hasn't been called yet.
ttuttle859dc7a2015-04-23 19:42:29304 RequestPriority last_create_transaction_priority() const {
[email protected]5033ab82013-03-22 20:17:46305 return last_create_transaction_priority_;
306 }
307
308 // Returns the last transaction created by
309 // CreateTransaction. Returns a NULL WeakPtr if one has not been
310 // created yet, or the last transaction has been destroyed, or
311 // ClearLastTransaction() has been called and a new transaction
312 // hasn't been created yet.
313 base::WeakPtr<MockNetworkTransaction> last_transaction() {
314 return last_transaction_;
315 }
316
317 // Makes last_transaction() return NULL until the next transaction
318 // is created.
319 void ClearLastTransaction() {
320 last_transaction_.reset();
321 }
322
ttuttle859dc7a2015-04-23 19:42:29323 // HttpTransactionFactory:
324 int CreateTransaction(RequestPriority priority,
danakj1fd259a02016-04-16 03:17:09325 std::unique_ptr<HttpTransaction>* trans) override;
ttuttle859dc7a2015-04-23 19:42:29326 HttpCache* GetCache() override;
327 HttpNetworkSession* GetSession() override;
[email protected]d2db0292011-01-26 20:23:44328
jkarlinfb1d5172015-01-12 14:10:29329 // The caller must guarantee that |clock| will outlive this object.
330 void SetClock(base::Clock* clock);
331 base::Clock* clock() const { return clock_; }
332
333 // The current time (will use clock_ if it is non NULL).
334 base::Time Now();
335
initial.commit586acc5fe2008-07-26 22:42:52336 private:
337 int transaction_count_;
[email protected]5c04f722011-08-12 17:52:47338 bool done_reading_called_;
[email protected]e50efea2014-03-24 18:41:00339 bool stop_caching_called_;
ttuttle859dc7a2015-04-23 19:42:29340 RequestPriority last_create_transaction_priority_;
jkarlinfb1d5172015-01-12 14:10:29341
342 // By default clock_ is NULL but it can be set to a custom clock by test
343 // frameworks using SetClock.
344 base::Clock* clock_;
345
[email protected]5033ab82013-03-22 20:17:46346 base::WeakPtr<MockNetworkTransaction> last_transaction_;
initial.commit586acc5fe2008-07-26 22:42:52347};
348
initial.commit586acc5fe2008-07-26 22:42:52349//-----------------------------------------------------------------------------
350// helpers
351
352// read the transaction completely
ttuttle859dc7a2015-04-23 19:42:29353int ReadTransaction(HttpTransaction* trans, std::string* result);
354
355} // namespace net
initial.commit586acc5fe2008-07-26 22:42:52356
357#endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_