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"
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/request_priority.h"
20 #include "net/base/test_completion_callback.h"
21 #include "net/disk_cache/disk_cache.h"
22 #include "net/http/http_cache.h"
23 #include "net/http/http_request_info.h"
24 #include "net/http/http_response_headers.h"
25 #include "net/http/http_response_info.h"
26 #include "net/log/net_log.h"
27 #include "net/socket/connection_attempts.h"
31 class HttpRequestHeaders
;
33 struct HttpRequestInfo
;
35 //-----------------------------------------------------------------------------
36 // mock transaction data
38 // these flags may be combined to form the test_mode field
41 TEST_MODE_SYNC_NET_START
= 1 << 0,
42 TEST_MODE_SYNC_NET_READ
= 1 << 1,
43 TEST_MODE_SYNC_CACHE_START
= 1 << 2,
44 TEST_MODE_SYNC_CACHE_READ
= 1 << 3,
45 TEST_MODE_SYNC_CACHE_WRITE
= 1 << 4,
46 TEST_MODE_SYNC_ALL
= (TEST_MODE_SYNC_NET_START
| TEST_MODE_SYNC_NET_READ
|
47 TEST_MODE_SYNC_CACHE_START
| TEST_MODE_SYNC_CACHE_READ
|
48 TEST_MODE_SYNC_CACHE_WRITE
),
49 TEST_MODE_SLOW_READ
= 1 << 5
52 typedef void (*MockTransactionHandler
)(const HttpRequestInfo
* request
,
53 std::string
* response_status
,
54 std::string
* response_headers
,
55 std::string
* response_data
);
57 struct MockTransaction
{
60 // If |request_time| is unspecified, the current time will be used.
61 base::Time request_time
;
62 const char* request_headers
;
65 const char* response_headers
;
66 // If |response_time| is unspecified, the current time will be used.
67 base::Time response_time
;
70 MockTransactionHandler handler
;
71 CertStatus cert_status
;
72 // Value returned by MockNetworkTransaction::Start (potentially
73 // asynchronously if |!(test_mode & TEST_MODE_SYNC_NET_START)|.)
77 extern const MockTransaction kSimpleGET_Transaction
;
78 extern const MockTransaction kSimplePOST_Transaction
;
79 extern const MockTransaction kTypicalGET_Transaction
;
80 extern const MockTransaction kETagGET_Transaction
;
81 extern const MockTransaction kRangeGET_Transaction
;
83 // returns the mock transaction for the given URL
84 const MockTransaction
* FindMockTransaction(const GURL
& url
);
86 // Add/Remove a mock transaction that can be accessed via FindMockTransaction.
87 // There can be only one MockTransaction associated with a given URL.
88 void AddMockTransaction(const MockTransaction
* trans
);
89 void RemoveMockTransaction(const MockTransaction
* trans
);
91 struct ScopedMockTransaction
: MockTransaction
{
92 ScopedMockTransaction() {
93 AddMockTransaction(this);
95 explicit ScopedMockTransaction(const MockTransaction
& t
)
96 : MockTransaction(t
) {
97 AddMockTransaction(this);
99 ~ScopedMockTransaction() {
100 RemoveMockTransaction(this);
104 //-----------------------------------------------------------------------------
107 class MockHttpRequest
: public HttpRequestInfo
{
109 explicit MockHttpRequest(const MockTransaction
& t
);
112 //-----------------------------------------------------------------------------
113 // use this class to test completely consuming a transaction
115 class TestTransactionConsumer
{
117 TestTransactionConsumer(RequestPriority priority
,
118 HttpTransactionFactory
* factory
);
119 virtual ~TestTransactionConsumer();
121 void Start(const HttpRequestInfo
* request
, const BoundNetLog
& net_log
);
123 bool is_done() const { return state_
== DONE
; }
124 int error() const { return error_
; }
126 const HttpResponseInfo
* response_info() const {
127 return trans_
->GetResponseInfo();
129 const std::string
& content() const { return content_
; }
139 void DidStart(int result
);
140 void DidRead(int result
);
141 void DidFinish(int result
);
144 void OnIOComplete(int result
);
147 scoped_ptr
<HttpTransaction
> trans_
;
148 std::string content_
;
149 scoped_refptr
<IOBuffer
> read_buf_
;
152 static int quit_counter_
;
155 //-----------------------------------------------------------------------------
156 // mock network layer
158 class MockNetworkLayer
;
160 // This transaction class inspects the available set of mock transactions to
161 // find data for the request URL. It supports IO operations that complete
162 // synchronously or asynchronously to help exercise different code paths in the
163 // HttpCache implementation.
164 class MockNetworkTransaction
165 : public HttpTransaction
,
166 public base::SupportsWeakPtr
<MockNetworkTransaction
> {
167 typedef WebSocketHandshakeStreamBase::CreateHelper CreateHelper
;
170 MockNetworkTransaction(RequestPriority priority
, MockNetworkLayer
* factory
);
171 ~MockNetworkTransaction() override
;
173 int Start(const HttpRequestInfo
* request
,
174 const CompletionCallback
& callback
,
175 const BoundNetLog
& net_log
) override
;
177 int RestartIgnoringLastError(const CompletionCallback
& callback
) override
;
179 int RestartWithCertificate(X509Certificate
* client_cert
,
180 const CompletionCallback
& callback
) override
;
182 int RestartWithAuth(const AuthCredentials
& credentials
,
183 const CompletionCallback
& callback
) override
;
185 bool IsReadyToRestartForAuth() override
;
187 int Read(IOBuffer
* buf
,
189 const CompletionCallback
& callback
) override
;
191 void StopCaching() override
;
193 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
195 int64
GetTotalReceivedBytes() const override
;
197 void DoneReading() override
;
199 const HttpResponseInfo
* GetResponseInfo() const override
;
201 LoadState
GetLoadState() const override
;
203 UploadProgress
GetUploadProgress() const override
;
205 void SetQuicServerInfo(QuicServerInfo
* quic_server_info
) override
;
207 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
209 void SetPriority(RequestPriority priority
) override
;
211 void SetWebSocketHandshakeStreamCreateHelper(
212 CreateHelper
* create_helper
) override
;
214 void SetBeforeNetworkStartCallback(
215 const BeforeNetworkStartCallback
& callback
) override
;
217 void SetBeforeProxyHeadersSentCallback(
218 const BeforeProxyHeadersSentCallback
& callback
) override
;
220 int ResumeNetworkStart() override
;
222 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
224 CreateHelper
* websocket_handshake_stream_create_helper() {
225 return websocket_handshake_stream_create_helper_
;
227 RequestPriority
priority() const { return priority_
; }
228 const HttpRequestInfo
* request() const { return request_
; }
231 int StartInternal(const HttpRequestInfo
* request
,
232 const CompletionCallback
& callback
,
233 const BoundNetLog
& net_log
);
234 void CallbackLater(const CompletionCallback
& callback
, int result
);
235 void RunCallback(const CompletionCallback
& callback
, int result
);
237 const HttpRequestInfo
* request_
;
238 HttpResponseInfo response_
;
242 RequestPriority priority_
;
243 CreateHelper
* websocket_handshake_stream_create_helper_
;
244 base::WeakPtr
<MockNetworkLayer
> transaction_factory_
;
245 int64 received_bytes_
;
247 // NetLog ID of the fake / non-existent underlying socket used by the
248 // connection. Requires Start() be passed a BoundNetLog with a real NetLog to
250 unsigned int socket_log_id_
;
252 base::WeakPtrFactory
<MockNetworkTransaction
> weak_factory_
;
256 class MockNetworkLayer
: public HttpTransactionFactory
,
257 public base::SupportsWeakPtr
<MockNetworkLayer
> {
260 ~MockNetworkLayer() override
;
262 int transaction_count() const { return transaction_count_
; }
263 bool done_reading_called() const { return done_reading_called_
; }
264 bool stop_caching_called() const { return stop_caching_called_
; }
265 void TransactionDoneReading();
266 void TransactionStopCaching();
268 // Returns the last priority passed to CreateTransaction, or
269 // DEFAULT_PRIORITY if it hasn't been called yet.
270 RequestPriority
last_create_transaction_priority() const {
271 return last_create_transaction_priority_
;
274 // Returns the last transaction created by
275 // CreateTransaction. Returns a NULL WeakPtr if one has not been
276 // created yet, or the last transaction has been destroyed, or
277 // ClearLastTransaction() has been called and a new transaction
278 // hasn't been created yet.
279 base::WeakPtr
<MockNetworkTransaction
> last_transaction() {
280 return last_transaction_
;
283 // Makes last_transaction() return NULL until the next transaction
285 void ClearLastTransaction() {
286 last_transaction_
.reset();
289 // HttpTransactionFactory:
290 int CreateTransaction(RequestPriority priority
,
291 scoped_ptr
<HttpTransaction
>* trans
) override
;
292 HttpCache
* GetCache() override
;
293 HttpNetworkSession
* GetSession() override
;
295 // The caller must guarantee that |clock| will outlive this object.
296 void SetClock(base::Clock
* clock
);
297 base::Clock
* clock() const { return clock_
; }
299 // The current time (will use clock_ if it is non NULL).
303 int transaction_count_
;
304 bool done_reading_called_
;
305 bool stop_caching_called_
;
306 RequestPriority last_create_transaction_priority_
;
308 // By default clock_ is NULL but it can be set to a custom clock by test
309 // frameworks using SetClock.
312 base::WeakPtr
<MockNetworkTransaction
> last_transaction_
;
315 //-----------------------------------------------------------------------------
318 // read the transaction completely
319 int ReadTransaction(HttpTransaction
* trans
, std::string
* result
);
323 #endif // NET_HTTP_HTTP_TRANSACTION_UNITTEST_H_