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
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"
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/strings/string16.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/load_flags.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/request_priority.h"
22 #include "net/base/test_completion_callback.h"
23 #include "net/disk_cache/disk_cache.h"
24 #include "net/http/http_cache.h"
25 #include "net/http/http_request_info.h"
26 #include "net/http/http_response_headers.h"
27 #include "net/http/http_response_info.h"
28 #include "net/log/net_log.h"
29 #include "net/socket/connection_attempts.h"
33 class HttpRequestHeaders
;
35 class X509Certificate
;
36 struct HttpRequestInfo
;
38 //-----------------------------------------------------------------------------
39 // mock transaction data
41 // these flags may be combined to form the test_mode field
44 TEST_MODE_SYNC_NET_START
= 1 << 0,
45 TEST_MODE_SYNC_NET_READ
= 1 << 1,
46 TEST_MODE_SYNC_CACHE_START
= 1 << 2,
47 TEST_MODE_SYNC_CACHE_READ
= 1 << 3,
48 TEST_MODE_SYNC_CACHE_WRITE
= 1 << 4,
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
|
51 TEST_MODE_SYNC_CACHE_WRITE
),
52 TEST_MODE_SLOW_READ
= 1 << 5
55 typedef void (*MockTransactionHandler
)(const HttpRequestInfo
* request
,
56 std::string
* response_status
,
57 std::string
* response_headers
,
58 std::string
* response_data
);
60 struct MockTransaction
{
63 // If |request_time| is unspecified, the current time will be used.
64 base::Time request_time
;
65 const char* request_headers
;
68 const char* response_headers
;
69 // If |response_time| is unspecified, the current time will be used.
70 base::Time response_time
;
73 MockTransactionHandler handler
;
74 scoped_refptr
<X509Certificate
> cert
;
75 CertStatus cert_status
;
76 int ssl_connection_status
;
77 // Value returned by MockNetworkTransaction::Start (potentially
78 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
82 extern const MockTransaction kSimpleGET_Transaction
;
83 extern const MockTransaction kSimplePOST_Transaction
;
84 extern const MockTransaction kTypicalGET_Transaction
;
85 extern const MockTransaction kETagGET_Transaction
;
86 extern const MockTransaction kRangeGET_Transaction
;
88 // returns the mock transaction for the given URL
89 const MockTransaction
* FindMockTransaction(const GURL
& url
);
91 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
92 // There can be only one MockTransaction associated with a given URL.
93 void AddMockTransaction(const MockTransaction
* trans
);
94 void RemoveMockTransaction(const MockTransaction
* trans
);
96 struct ScopedMockTransaction
: MockTransaction
{
97 ScopedMockTransaction() {
98 AddMockTransaction(this);
100 explicit ScopedMockTransaction(const MockTransaction
& t
)
101 : MockTransaction(t
) {
102 AddMockTransaction(this);
104 ~ScopedMockTransaction() {
105 RemoveMockTransaction(this);
109 //-----------------------------------------------------------------------------
112 class MockHttpRequest
: public HttpRequestInfo
{
114 explicit MockHttpRequest(const MockTransaction
& t
);
117 //-----------------------------------------------------------------------------
118 // use this class to test completely consuming a transaction
120 class TestTransactionConsumer
{
122 TestTransactionConsumer(RequestPriority priority
,
123 HttpTransactionFactory
* factory
);
124 virtual ~TestTransactionConsumer();
126 void Start(const HttpRequestInfo
* request
, const BoundNetLog
& net_log
);
128 bool is_done() const { return state_
== DONE
; }
129 int error() const { return error_
; }
131 const HttpResponseInfo
* response_info() const {
132 return trans_
->GetResponseInfo();
134 const std::string
& content() const { return content_
; }
144 void DidStart(int result
);
145 void DidRead(int result
);
146 void DidFinish(int result
);
149 void OnIOComplete(int result
);
152 scoped_ptr
<HttpTransaction
> trans_
;
153 std::string content_
;
154 scoped_refptr
<IOBuffer
> read_buf_
;
157 static int quit_counter_
;
160 //-----------------------------------------------------------------------------
161 // mock network layer
163 class MockNetworkLayer
;
165 // This transaction class inspects the available set of mock transactions to
166 // find data for the request URL. It supports IO operations that complete
167 // synchronously or asynchronously to help exercise different code paths in the
168 // HttpCache implementation.
169 class MockNetworkTransaction
170 : public HttpTransaction
,
171 public base::SupportsWeakPtr
<MockNetworkTransaction
> {
172 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper
;
175 MockNetworkTransaction(RequestPriority priority
, MockNetworkLayer
* factory
);
176 ~MockNetworkTransaction() override
;
178 int Start(const HttpRequestInfo
* request
,
179 const CompletionCallback
& callback
,
180 const BoundNetLog
& net_log
) override
;
182 int RestartIgnoringLastError(const CompletionCallback
& callback
) override
;
184 int RestartWithCertificate(X509Certificate
* client_cert
,
185 const CompletionCallback
& callback
) override
;
187 int RestartWithAuth(const AuthCredentials
& credentials
,
188 const CompletionCallback
& callback
) override
;
190 bool IsReadyToRestartForAuth() override
;
192 int Read(IOBuffer
* buf
,
194 const CompletionCallback
& callback
) override
;
196 void StopCaching() override
;
198 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
200 int64
GetTotalReceivedBytes() const override
;
202 int64_t GetTotalSentBytes() const override
;
204 void DoneReading() override
;
206 const HttpResponseInfo
* GetResponseInfo() const override
;
208 LoadState
GetLoadState() const override
;
210 UploadProgress
GetUploadProgress() const override
;
212 void SetQuicServerInfo(QuicServerInfo
* quic_server_info
) override
;
214 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
216 void SetPriority(RequestPriority priority
) override
;
218 void SetWebSocketHandshakeStreamCreateHelper(
219 CreateHelper
* create_helper
) override
;
221 void SetBeforeNetworkStartCallback(
222 const BeforeNetworkStartCallback
& callback
) override
;
224 void SetBeforeProxyHeadersSentCallback(
225 const BeforeProxyHeadersSentCallback
& callback
) override
;
227 int ResumeNetworkStart() override
;
229 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
231 CreateHelper
* websocket_handshake_stream_create_helper() {
232 return websocket_handshake_stream_create_helper_
;
234 RequestPriority
priority() const { return priority_
; }
235 const HttpRequestInfo
* request() const { return request_
; }
237 // Bogus value that will be returned by GetTotalReceivedBytes() if the
238 // MockNetworkTransaction was started.
239 static const int64_t kTotalReceivedBytes
;
240 // Bogus value that will be returned by GetTotalSentBytes() if the
241 // MockNetworkTransaction was started.
242 static const int64_t kTotalSentBytes
;
245 int StartInternal(const HttpRequestInfo
* request
,
246 const CompletionCallback
& callback
,
247 const BoundNetLog
& net_log
);
248 void CallbackLater(const CompletionCallback
& callback
, int result
);
249 void RunCallback(const CompletionCallback
& callback
, int result
);
251 const HttpRequestInfo
* request_
;
252 HttpResponseInfo response_
;
256 RequestPriority priority_
;
257 CreateHelper
* websocket_handshake_stream_create_helper_
;
258 base::WeakPtr
<MockNetworkLayer
> transaction_factory_
;
259 int64 received_bytes_
;
262 // 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
265 unsigned int socket_log_id_
;
267 base::WeakPtrFactory
<MockNetworkTransaction
> weak_factory_
;
271 class MockNetworkLayer
: public HttpTransactionFactory
,
272 public base::SupportsWeakPtr
<MockNetworkLayer
> {
275 ~MockNetworkLayer() override
;
277 int transaction_count() const { return transaction_count_
; }
278 bool done_reading_called() const { return done_reading_called_
; }
279 bool stop_caching_called() const { return stop_caching_called_
; }
280 void TransactionDoneReading();
281 void TransactionStopCaching();
283 // Returns the last priority passed to CreateTransaction, or
284 // DEFAULT_PRIORITY if it hasn't been called yet.
285 RequestPriority
last_create_transaction_priority() const {
286 return last_create_transaction_priority_
;
289 // Returns the last transaction created by
290 // CreateTransaction. Returns a NULL WeakPtr if one has not been
291 // created yet, or the last transaction has been destroyed, or
292 // ClearLastTransaction() has been called and a new transaction
293 // hasn't been created yet.
294 base::WeakPtr
<MockNetworkTransaction
> last_transaction() {
295 return last_transaction_
;
298 // Makes last_transaction() return NULL until the next transaction
300 void ClearLastTransaction() {
301 last_transaction_
.reset();
304 // HttpTransactionFactory:
305 int CreateTransaction(RequestPriority priority
,
306 scoped_ptr
<HttpTransaction
>* trans
) override
;
307 HttpCache
* GetCache() override
;
308 HttpNetworkSession
* GetSession() override
;
310 // The caller must guarantee that |clock| will outlive this object.
311 void SetClock(base::Clock
* clock
);
312 base::Clock
* clock() const { return clock_
; }
314 // The current time (will use clock_ if it is non NULL).
318 int transaction_count_
;
319 bool done_reading_called_
;
320 bool stop_caching_called_
;
321 RequestPriority last_create_transaction_priority_
;
323 // By default clock_ is NULL but it can be set to a custom clock by test
324 // frameworks using SetClock.
327 base::WeakPtr
<MockNetworkTransaction
> last_transaction_
;
330 //-----------------------------------------------------------------------------
333 // read the transaction completely
334 int ReadTransaction(HttpTransaction
* trans
, std::string
* result
);
338 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_