blob: 2572b1faad502ff515831deb9021f2c01644fcf7 [file] [log] [blame]
[email protected]5a07c192012-07-30 20:18:221// Copyright (c) 2012 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
initial.commit586acc5fe2008-07-26 22:42:5210#include <string>
11
[email protected]2041cf342010-02-19 03:15:5912#include "base/callback.h"
[email protected]cad155b2008-09-23 14:44:2713#include "base/compiler_specific.h"
[email protected]5033ab82013-03-22 20:17:4614#include "base/memory/weak_ptr.h"
[email protected]125ef482013-06-11 18:32:4715#include "base/strings/string16.h"
[email protected]74a85ce2009-02-12 00:03:1916#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5217#include "net/base/load_flags.h"
[email protected]d8eb84242010-09-25 02:25:0618#include "net/base/net_errors.h"
[email protected]3b23a222013-05-15 21:33:2519#include "net/base/net_log.h"
[email protected]262eec82013-03-19 21:01:3620#include "net/base/request_priority.h"
initial.commit586acc5fe2008-07-26 22:42:5221#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"
[email protected]319d9e6f2009-02-18 19:47:2125#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5226#include "net/http/http_response_info.h"
27
[email protected]d2db0292011-01-26 20:23:4428namespace net {
[email protected]79e1fd62013-06-20 06:50:0429class HttpRequestHeaders;
[email protected]d2db0292011-01-26 20:23:4430class IOBuffer;
31}
32
initial.commit586acc5fe2008-07-26 22:42:5233//-----------------------------------------------------------------------------
34// mock transaction data
35
36// these flags may be combined to form the test_mode field
37enum {
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,
[email protected]73cae572009-10-22 18:36:1943 TEST_MODE_SYNC_CACHE_WRITE = 1 << 4,
[email protected]2227c692010-05-04 15:36:1144 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)
initial.commit586acc5fe2008-07-26 22:42:5247};
48
49typedef void (*MockTransactionHandler)(const net::HttpRequestInfo* request,
50 std::string* response_status,
51 std::string* response_headers,
52 std::string* response_data);
53
54struct MockTransaction {
55 const char* url;
56 const char* method;
[email protected]ca2f19e2009-09-04 22:53:1657 // If |request_time| is unspecified, the current time will be used.
58 base::Time request_time;
initial.commit586acc5fe2008-07-26 22:42:5259 const char* request_headers;
60 int load_flags;
61 const char* status;
62 const char* response_headers;
[email protected]207d58c72009-09-04 18:59:2963 // If |response_time| is unspecified, the current time will be used.
64 base::Time response_time;
initial.commit586acc5fe2008-07-26 22:42:5265 const char* data;
66 int test_mode;
67 MockTransactionHandler handler;
[email protected]70d66502011-09-23 00:55:0868 net::CertStatus cert_status;
[email protected]2ef5d00e2013-03-23 16:17:2769 // Value returned by MockNetworkTransaction::Start (potentially
70 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
71 net::Error return_code;
initial.commit586acc5fe2008-07-26 22:42:5272};
73
74extern const MockTransaction kSimpleGET_Transaction;
75extern const MockTransaction kSimplePOST_Transaction;
76extern const MockTransaction kTypicalGET_Transaction;
77extern const MockTransaction kETagGET_Transaction;
78extern const MockTransaction kRangeGET_Transaction;
79
80// returns the mock transaction for the given URL
81const 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.
85void AddMockTransaction(const MockTransaction* trans);
86void RemoveMockTransaction(const MockTransaction* trans);
87
88struct ScopedMockTransaction : MockTransaction {
89 ScopedMockTransaction() {
90 AddMockTransaction(this);
91 }
92 explicit ScopedMockTransaction(const MockTransaction& t)
93 : MockTransaction(t) {
94 AddMockTransaction(this);
95 }
96 ~ScopedMockTransaction() {
97 RemoveMockTransaction(this);
98 }
99};
100
101//-----------------------------------------------------------------------------
102// mock http request
103
104class MockHttpRequest : public net::HttpRequestInfo {
105 public:
[email protected]d2db0292011-01-26 20:23:44106 explicit MockHttpRequest(const MockTransaction& t);
initial.commit586acc5fe2008-07-26 22:42:52107};
108
109//-----------------------------------------------------------------------------
110// use this class to test completely consuming a transaction
111
[email protected]b35f629f2011-12-23 02:53:32112class TestTransactionConsumer {
initial.commit586acc5fe2008-07-26 22:42:52113 public:
[email protected]262eec82013-03-19 21:01:36114 TestTransactionConsumer(net::RequestPriority priority,
115 net::HttpTransactionFactory* factory);
[email protected]d2db0292011-01-26 20:23:44116 virtual ~TestTransactionConsumer();
initial.commit586acc5fe2008-07-26 22:42:52117
[email protected]9e743cd2010-03-16 07:03:53118 void Start(const net::HttpRequestInfo* request,
[email protected]d2db0292011-01-26 20:23:44119 const net::BoundNetLog& net_log);
initial.commit586acc5fe2008-07-26 22:42:52120
121 bool is_done() const { return state_ == DONE; }
122 int error() const { return error_; }
123
124 const net::HttpResponseInfo* response_info() const {
125 return trans_->GetResponseInfo();
126 }
127 const std::string& content() const { return content_; }
128
129 private:
initial.commit586acc5fe2008-07-26 22:42:52130 enum State {
131 IDLE,
132 STARTING,
133 READING,
134 DONE
[email protected]d2db0292011-01-26 20:23:44135 };
initial.commit586acc5fe2008-07-26 22:42:52136
[email protected]d2db0292011-01-26 20:23:44137 void DidStart(int result);
138 void DidRead(int result);
139 void DidFinish(int result);
140 void Read();
141
[email protected]b35f629f2011-12-23 02:53:32142 void OnIOComplete(int result);
[email protected]d2db0292011-01-26 20:23:44143
144 State state_;
[email protected]af4876d2008-10-21 23:10:57145 scoped_ptr<net::HttpTransaction> trans_;
initial.commit586acc5fe2008-07-26 22:42:52146 std::string content_;
[email protected]9dea9e1f2009-01-29 00:30:47147 scoped_refptr<net::IOBuffer> read_buf_;
initial.commit586acc5fe2008-07-26 22:42:52148 int error_;
149
150 static int quit_counter_;
151};
152
153//-----------------------------------------------------------------------------
154// mock network layer
155
[email protected]5c04f722011-08-12 17:52:47156class MockNetworkLayer;
157
initial.commit586acc5fe2008-07-26 22:42:52158// 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.
[email protected]5033ab82013-03-22 20:17:46162class MockNetworkTransaction
163 : public net::HttpTransaction,
164 public base::SupportsWeakPtr<MockNetworkTransaction> {
[email protected]831e4a32013-11-14 02:14:44165 typedef net::WebSocketHandshakeStreamBase::CreateHelper CreateHelper;
initial.commit586acc5fe2008-07-26 22:42:52166 public:
[email protected]262eec82013-03-19 21:01:36167 MockNetworkTransaction(net::RequestPriority priority,
168 MockNetworkLayer* factory);
[email protected]d2db0292011-01-26 20:23:44169 virtual ~MockNetworkTransaction();
initial.commit586acc5fe2008-07-26 22:42:52170
[email protected]684970b2009-08-14 04:54:46171 virtual int Start(const net::HttpRequestInfo* request,
[email protected]49639fa2011-12-20 23:22:41172 const net::CompletionCallback& callback,
[email protected]f3cf9802011-10-28 18:44:58173 const net::BoundNetLog& net_log) OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52174
[email protected]f3cf9802011-10-28 18:44:58175 virtual int RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41176 const net::CompletionCallback& callback) OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52177
[email protected]f3cf9802011-10-28 18:44:58178 virtual int RestartWithCertificate(
179 net::X509Certificate* client_cert,
[email protected]49639fa2011-12-20 23:22:41180 const net::CompletionCallback& callback) OVERRIDE;
[email protected]0b45559b2009-06-12 21:45:11181
[email protected]f3cf9802011-10-28 18:44:58182 virtual int RestartWithAuth(
183 const net::AuthCredentials& credentials,
[email protected]49639fa2011-12-20 23:22:41184 const net::CompletionCallback& callback) OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52185
[email protected]f3cf9802011-10-28 18:44:58186 virtual bool IsReadyToRestartForAuth() OVERRIDE;
[email protected]0757e7702009-03-27 04:00:22187
[email protected]9dea9e1f2009-01-29 00:30:47188 virtual int Read(net::IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41189 const net::CompletionCallback& callback) OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52190
[email protected]f3cf9802011-10-28 18:44:58191 virtual void StopCaching() OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52192
[email protected]79e1fd62013-06-20 06:50:04193 virtual bool GetFullRequestHeaders(
194 net::HttpRequestHeaders* headers) const OVERRIDE;
195
[email protected]b8015c42013-12-24 15:18:19196 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
197
[email protected]f3cf9802011-10-28 18:44:58198 virtual void DoneReading() OVERRIDE;
[email protected]5c04f722011-08-12 17:52:47199
[email protected]f3cf9802011-10-28 18:44:58200 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE;
[email protected]9dd90e52010-02-23 19:15:01201
[email protected]f3cf9802011-10-28 18:44:58202 virtual net::LoadState GetLoadState() const OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52203
[email protected]196d18a2012-08-30 03:47:31204 virtual net::UploadProgress GetUploadProgress() const OVERRIDE;
initial.commit586acc5fe2008-07-26 22:42:52205
[email protected]8cd06c02014-01-25 07:50:14206 virtual void SetQuicServerInfo(
207 net::QuicServerInfo* quic_server_info) OVERRIDE;
208
[email protected]58e32bb2013-01-21 18:23:25209 virtual bool GetLoadTimingInfo(
210 net::LoadTimingInfo* load_timing_info) const OVERRIDE;
211
[email protected]5033ab82013-03-22 20:17:46212 virtual void SetPriority(net::RequestPriority priority) OVERRIDE;
213
[email protected]831e4a32013-11-14 02:14:44214 virtual void SetWebSocketHandshakeStreamCreateHelper(
215 CreateHelper* create_helper) OVERRIDE;
216
[email protected]1826a402014-01-08 15:40:48217 virtual void SetBeforeNetworkStartCallback(
218 const BeforeNetworkStartCallback& callback) OVERRIDE;
219
220 virtual int ResumeNetworkStart() OVERRIDE;
221
[email protected]831e4a32013-11-14 02:14:44222 CreateHelper* websocket_handshake_stream_create_helper() {
223 return websocket_handshake_stream_create_helper_;
224 }
[email protected]5033ab82013-03-22 20:17:46225 net::RequestPriority priority() const { return priority_; }
226
initial.commit586acc5fe2008-07-26 22:42:52227 private:
[email protected]d7358ba2014-01-04 01:39:49228 int StartInternal(const net::HttpRequestInfo* request,
229 const net::CompletionCallback& callback,
230 const net::BoundNetLog& net_log);
[email protected]49639fa2011-12-20 23:22:41231 void CallbackLater(const net::CompletionCallback& callback, int result);
232 void RunCallback(const net::CompletionCallback& callback, int result);
initial.commit586acc5fe2008-07-26 22:42:52233
[email protected]49639fa2011-12-20 23:22:41234 base::WeakPtrFactory<MockNetworkTransaction> weak_factory_;
[email protected]d7358ba2014-01-04 01:39:49235 const net::HttpRequestInfo* request_;
initial.commit586acc5fe2008-07-26 22:42:52236 net::HttpResponseInfo response_;
237 std::string data_;
238 int data_cursor_;
239 int test_mode_;
[email protected]5033ab82013-03-22 20:17:46240 net::RequestPriority priority_;
[email protected]831e4a32013-11-14 02:14:44241 CreateHelper* websocket_handshake_stream_create_helper_;
[email protected]5c04f722011-08-12 17:52:47242 base::WeakPtr<MockNetworkLayer> transaction_factory_;
[email protected]b8015c42013-12-24 15:18:19243 int64 received_bytes_;
[email protected]3b23a222013-05-15 21:33:25244
245 // NetLog ID of the fake / non-existent underlying socket used by the
246 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
247 // be initialized.
248 unsigned int socket_log_id_;
initial.commit586acc5fe2008-07-26 22:42:52249};
250
[email protected]5c04f722011-08-12 17:52:47251class MockNetworkLayer : public net::HttpTransactionFactory,
252 public base::SupportsWeakPtr<MockNetworkLayer> {
initial.commit586acc5fe2008-07-26 22:42:52253 public:
[email protected]d2db0292011-01-26 20:23:44254 MockNetworkLayer();
255 virtual ~MockNetworkLayer();
initial.commit586acc5fe2008-07-26 22:42:52256
257 int transaction_count() const { return transaction_count_; }
[email protected]5c04f722011-08-12 17:52:47258 bool done_reading_called() const { return done_reading_called_; }
259 void TransactionDoneReading();
initial.commit586acc5fe2008-07-26 22:42:52260
[email protected]5033ab82013-03-22 20:17:46261 // Returns the last priority passed to CreateTransaction, or
262 // DEFAULT_PRIORITY if it hasn't been called yet.
263 net::RequestPriority last_create_transaction_priority() const {
264 return last_create_transaction_priority_;
265 }
266
267 // Returns the last transaction created by
268 // CreateTransaction. Returns a NULL WeakPtr if one has not been
269 // created yet, or the last transaction has been destroyed, or
270 // ClearLastTransaction() has been called and a new transaction
271 // hasn't been created yet.
272 base::WeakPtr<MockNetworkTransaction> last_transaction() {
273 return last_transaction_;
274 }
275
276 // Makes last_transaction() return NULL until the next transaction
277 // is created.
278 void ClearLastTransaction() {
279 last_transaction_.reset();
280 }
281
[email protected]d2db0292011-01-26 20:23:44282 // net::HttpTransactionFactory:
[email protected]f2cbbc82011-11-16 01:10:29283 virtual int CreateTransaction(
[email protected]262eec82013-03-19 21:01:36284 net::RequestPriority priority,
[email protected]027bd85a2013-12-27 22:39:10285 scoped_ptr<net::HttpTransaction>* trans) OVERRIDE;
[email protected]f2cbbc82011-11-16 01:10:29286 virtual net::HttpCache* GetCache() OVERRIDE;
287 virtual net::HttpNetworkSession* GetSession() OVERRIDE;
[email protected]d2db0292011-01-26 20:23:44288
initial.commit586acc5fe2008-07-26 22:42:52289 private:
290 int transaction_count_;
[email protected]5c04f722011-08-12 17:52:47291 bool done_reading_called_;
[email protected]5033ab82013-03-22 20:17:46292 net::RequestPriority last_create_transaction_priority_;
293 base::WeakPtr<MockNetworkTransaction> last_transaction_;
initial.commit586acc5fe2008-07-26 22:42:52294};
295
initial.commit586acc5fe2008-07-26 22:42:52296//-----------------------------------------------------------------------------
297// helpers
298
299// read the transaction completely
300int ReadTransaction(net::HttpTransaction* trans, std::string* result);
301
302#endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_