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.
5 #ifndef NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
6 #define NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_
8 #include "net/http/http_transaction.h"
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"
29 class HttpRequestHeaders
;
33 //-----------------------------------------------------------------------------
34 // mock transaction data
36 // these flags may be combined to form the test_mode field
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
)
49 typedef void (*MockTransactionHandler
)(const net::HttpRequestInfo
* request
,
50 std::string
* response_status
,
51 std::string
* response_headers
,
52 std::string
* response_data
);
54 struct MockTransaction
{
57 // If |request_time| is unspecified, the current time will be used.
58 base::Time request_time
;
59 const char* request_headers
;
62 const char* response_headers
;
63 // If |response_time| is unspecified, the current time will be used.
64 base::Time response_time
;
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
;
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
;
80 // returns the mock transaction for the given URL
81 const MockTransaction
* FindMockTransaction(const GURL
& url
);
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
);
88 struct ScopedMockTransaction
: MockTransaction
{
89 ScopedMockTransaction() {
90 AddMockTransaction(this);
92 explicit ScopedMockTransaction(const MockTransaction
& t
)
93 : MockTransaction(t
) {
94 AddMockTransaction(this);
96 ~ScopedMockTransaction() {
97 RemoveMockTransaction(this);
101 //-----------------------------------------------------------------------------
104 class MockHttpRequest
: public net::HttpRequestInfo
{
106 explicit MockHttpRequest(const MockTransaction
& t
);
109 //-----------------------------------------------------------------------------
110 // use this class to test completely consuming a transaction
112 class TestTransactionConsumer
{
114 TestTransactionConsumer(net::RequestPriority priority
,
115 net::HttpTransactionFactory
* factory
);
116 virtual ~TestTransactionConsumer();
118 void Start(const net::HttpRequestInfo
* request
,
119 const net::BoundNetLog
& net_log
);
121 bool is_done() const { return state_
== DONE
; }
122 int error() const { return error_
; }
124 const net::HttpResponseInfo
* response_info() const {
125 return trans_
->GetResponseInfo();
127 const std::string
& content() const { return content_
; }
137 void DidStart(int result
);
138 void DidRead(int result
);
139 void DidFinish(int result
);
142 void OnIOComplete(int result
);
145 scoped_ptr
<net::HttpTransaction
> trans_
;
146 std::string content_
;
147 scoped_refptr
<net::IOBuffer
> read_buf_
;
150 static int quit_counter_
;
153 //-----------------------------------------------------------------------------
154 // mock network layer
156 class MockNetworkLayer
;
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
> {
166 MockNetworkTransaction(net::RequestPriority priority
,
167 MockNetworkLayer
* factory
);
168 virtual ~MockNetworkTransaction();
170 virtual int Start(const net::HttpRequestInfo
* request
,
171 const net::CompletionCallback
& callback
,
172 const net::BoundNetLog
& net_log
) OVERRIDE
;
174 virtual int RestartIgnoringLastError(
175 const net::CompletionCallback
& callback
) OVERRIDE
;
177 virtual int RestartWithCertificate(
178 net::X509Certificate
* client_cert
,
179 const net::CompletionCallback
& callback
) OVERRIDE
;
181 virtual int RestartWithAuth(
182 const net::AuthCredentials
& credentials
,
183 const net::CompletionCallback
& callback
) OVERRIDE
;
185 virtual bool IsReadyToRestartForAuth() OVERRIDE
;
187 virtual int Read(net::IOBuffer
* buf
, int buf_len
,
188 const net::CompletionCallback
& callback
) OVERRIDE
;
190 virtual void StopCaching() OVERRIDE
;
192 virtual bool GetFullRequestHeaders(
193 net::HttpRequestHeaders
* headers
) const OVERRIDE
;
195 virtual void DoneReading() OVERRIDE
;
197 virtual const net::HttpResponseInfo
* GetResponseInfo() const OVERRIDE
;
199 virtual net::LoadState
GetLoadState() const OVERRIDE
;
201 virtual net::UploadProgress
GetUploadProgress() const OVERRIDE
;
203 virtual bool GetLoadTimingInfo(
204 net::LoadTimingInfo
* load_timing_info
) const OVERRIDE
;
206 virtual void SetPriority(net::RequestPriority priority
) OVERRIDE
;
208 net::RequestPriority
priority() const { return priority_
; }
211 void CallbackLater(const net::CompletionCallback
& callback
, int result
);
212 void RunCallback(const net::CompletionCallback
& callback
, int result
);
214 base::WeakPtrFactory
<MockNetworkTransaction
> weak_factory_
;
215 net::HttpResponseInfo response_
;
219 net::RequestPriority priority_
;
220 base::WeakPtr
<MockNetworkLayer
> transaction_factory_
;
222 // NetLog ID of the fake / non-existent underlying socket used by the
223 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
225 unsigned int socket_log_id_
;
228 class MockNetworkLayer
: public net::HttpTransactionFactory
,
229 public base::SupportsWeakPtr
<MockNetworkLayer
> {
232 virtual ~MockNetworkLayer();
234 int transaction_count() const { return transaction_count_
; }
235 bool done_reading_called() const { return done_reading_called_
; }
236 void TransactionDoneReading();
238 // Returns the last priority passed to CreateTransaction, or
239 // DEFAULT_PRIORITY if it hasn't been called yet.
240 net::RequestPriority
last_create_transaction_priority() const {
241 return last_create_transaction_priority_
;
244 // Returns the last transaction created by
245 // CreateTransaction. Returns a NULL WeakPtr if one has not been
246 // created yet, or the last transaction has been destroyed, or
247 // ClearLastTransaction() has been called and a new transaction
248 // hasn't been created yet.
249 base::WeakPtr
<MockNetworkTransaction
> last_transaction() {
250 return last_transaction_
;
253 // Makes last_transaction() return NULL until the next transaction
255 void ClearLastTransaction() {
256 last_transaction_
.reset();
259 // net::HttpTransactionFactory:
260 virtual int CreateTransaction(
261 net::RequestPriority priority
,
262 scoped_ptr
<net::HttpTransaction
>* trans
,
263 net::HttpTransactionDelegate
* delegate
) OVERRIDE
;
264 virtual net::HttpCache
* GetCache() OVERRIDE
;
265 virtual net::HttpNetworkSession
* GetSession() OVERRIDE
;
268 int transaction_count_
;
269 bool done_reading_called_
;
270 net::RequestPriority last_create_transaction_priority_
;
271 base::WeakPtr
<MockNetworkTransaction
> last_transaction_
;
274 //-----------------------------------------------------------------------------
277 // read the transaction completely
278 int ReadTransaction(net::HttpTransaction
* trans
, std::string
* result
);
280 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_