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 #include "net/http/http_transaction_unittest.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "net/base/load_flags.h"
14 #include "net/base/load_timing_info.h"
15 #include "net/base/net_errors.h"
16 #include "net/disk_cache/disk_cache.h"
17 #include "net/http/http_cache.h"
18 #include "net/http/http_request_info.h"
19 #include "net/http/http_response_info.h"
20 #include "net/http/http_transaction.h"
21 #include "testing/gtest/include/gtest/gtest.h"
24 typedef base::hash_map
<std::string
, const MockTransaction
*> MockTransactionMap
;
25 static MockTransactionMap mock_transactions
;
28 //-----------------------------------------------------------------------------
29 // mock transaction data
31 const MockTransaction kSimpleGET_Transaction
= {
32 "http://www.google.com/",
38 "Cache-Control: max-age=10000\n",
40 "<html><body>Google Blah Blah</body></html>",
47 const MockTransaction kSimplePOST_Transaction
= {
48 "http://bugdatabase.com/edit",
56 "<html><body>Google Blah Blah</body></html>",
63 const MockTransaction kTypicalGET_Transaction
= {
64 "http://www.example.com/~foo/bar.html",
70 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"
71 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n",
73 "<html><body>Google Blah Blah</body></html>",
80 const MockTransaction kETagGET_Transaction
= {
81 "http://www.google.com/foopy",
87 "Cache-Control: max-age=10000\n"
90 "<html><body>Google Blah Blah</body></html>",
97 const MockTransaction kRangeGET_Transaction
= {
98 "http://www.google.com/",
104 "Cache-Control: max-age=10000\n",
106 "<html><body>Google Blah Blah</body></html>",
113 static const MockTransaction
* const kBuiltinMockTransactions
[] = {
114 &kSimpleGET_Transaction
,
115 &kSimplePOST_Transaction
,
116 &kTypicalGET_Transaction
,
117 &kETagGET_Transaction
,
118 &kRangeGET_Transaction
121 const MockTransaction
* FindMockTransaction(const GURL
& url
) {
122 // look for overrides:
123 MockTransactionMap::const_iterator it
= mock_transactions
.find(url
.spec());
124 if (it
!= mock_transactions
.end())
127 // look for builtins:
128 for (size_t i
= 0; i
< arraysize(kBuiltinMockTransactions
); ++i
) {
129 if (url
== GURL(kBuiltinMockTransactions
[i
]->url
))
130 return kBuiltinMockTransactions
[i
];
135 void AddMockTransaction(const MockTransaction
* trans
) {
136 mock_transactions
[GURL(trans
->url
).spec()] = trans
;
139 void RemoveMockTransaction(const MockTransaction
* trans
) {
140 mock_transactions
.erase(GURL(trans
->url
).spec());
143 MockHttpRequest::MockHttpRequest(const MockTransaction
& t
) {
146 extra_headers
.AddHeadersFromString(t
.request_headers
);
147 load_flags
= t
.load_flags
;
150 //-----------------------------------------------------------------------------
153 int TestTransactionConsumer::quit_counter_
= 0;
155 TestTransactionConsumer::TestTransactionConsumer(
156 net::RequestPriority priority
,
157 net::HttpTransactionFactory
* factory
)
158 : state_(IDLE
), error_(net::OK
) {
159 // Disregard the error code.
160 factory
->CreateTransaction(priority
, &trans_
, NULL
);
164 TestTransactionConsumer::~TestTransactionConsumer() {
167 void TestTransactionConsumer::Start(const net::HttpRequestInfo
* request
,
168 const net::BoundNetLog
& net_log
) {
170 int result
= trans_
->Start(
171 request
, base::Bind(&TestTransactionConsumer::OnIOComplete
,
172 base::Unretained(this)), net_log
);
173 if (result
!= net::ERR_IO_PENDING
)
177 void TestTransactionConsumer::DidStart(int result
) {
178 if (result
!= net::OK
) {
185 void TestTransactionConsumer::DidRead(int result
) {
189 content_
.append(read_buf_
->data(), result
);
194 void TestTransactionConsumer::DidFinish(int result
) {
197 if (--quit_counter_
== 0)
198 base::MessageLoop::current()->Quit();
201 void TestTransactionConsumer::Read() {
203 read_buf_
= new net::IOBuffer(1024);
204 int result
= trans_
->Read(read_buf_
.get(),
206 base::Bind(&TestTransactionConsumer::OnIOComplete
,
207 base::Unretained(this)));
208 if (result
!= net::ERR_IO_PENDING
)
212 void TestTransactionConsumer::OnIOComplete(int result
) {
225 MockNetworkTransaction::MockNetworkTransaction(
226 net::RequestPriority priority
,
227 MockNetworkLayer
* factory
)
228 : weak_factory_(this),
231 transaction_factory_(factory
->AsWeakPtr()),
232 socket_log_id_(net::NetLog::Source::kInvalidId
) {
235 MockNetworkTransaction::~MockNetworkTransaction() {}
237 int MockNetworkTransaction::Start(const net::HttpRequestInfo
* request
,
238 const net::CompletionCallback
& callback
,
239 const net::BoundNetLog
& net_log
) {
240 const MockTransaction
* t
= FindMockTransaction(request
->url
);
242 return net::ERR_FAILED
;
244 test_mode_
= t
->test_mode
;
246 // Return immediately if we're returning an error.
247 if (net::OK
!= t
->return_code
) {
248 if (test_mode_
& TEST_MODE_SYNC_NET_START
)
249 return t
->return_code
;
250 CallbackLater(callback
, t
->return_code
);
251 return net::ERR_IO_PENDING
;
254 std::string resp_status
= t
->status
;
255 std::string resp_headers
= t
->response_headers
;
256 std::string resp_data
= t
->data
;
258 (t
->handler
)(request
, &resp_status
, &resp_headers
, &resp_data
);
260 std::string header_data
= base::StringPrintf(
261 "%s\n%s\n", resp_status
.c_str(), resp_headers
.c_str());
262 std::replace(header_data
.begin(), header_data
.end(), '\n', '\0');
264 response_
.request_time
= base::Time::Now();
265 if (!t
->request_time
.is_null())
266 response_
.request_time
= t
->request_time
;
268 response_
.was_cached
= false;
269 response_
.network_accessed
= true;
271 response_
.response_time
= base::Time::Now();
272 if (!t
->response_time
.is_null())
273 response_
.response_time
= t
->response_time
;
275 response_
.headers
= new net::HttpResponseHeaders(header_data
);
276 response_
.vary_data
.Init(*request
, *response_
.headers
.get());
277 response_
.ssl_info
.cert_status
= t
->cert_status
;
280 if (net_log
.net_log())
281 socket_log_id_
= net_log
.net_log()->NextID();
283 if (test_mode_
& TEST_MODE_SYNC_NET_START
)
286 CallbackLater(callback
, net::OK
);
287 return net::ERR_IO_PENDING
;
290 int MockNetworkTransaction::RestartIgnoringLastError(
291 const net::CompletionCallback
& callback
) {
292 return net::ERR_FAILED
;
295 int MockNetworkTransaction::RestartWithCertificate(
296 net::X509Certificate
* client_cert
,
297 const net::CompletionCallback
& callback
) {
298 return net::ERR_FAILED
;
301 int MockNetworkTransaction::RestartWithAuth(
302 const net::AuthCredentials
& credentials
,
303 const net::CompletionCallback
& callback
) {
304 return net::ERR_FAILED
;
307 bool MockNetworkTransaction::IsReadyToRestartForAuth() {
311 int MockNetworkTransaction::Read(net::IOBuffer
* buf
, int buf_len
,
312 const net::CompletionCallback
& callback
) {
313 int data_len
= static_cast<int>(data_
.size());
314 int num
= std::min(buf_len
, data_len
- data_cursor_
);
316 memcpy(buf
->data(), data_
.data() + data_cursor_
, num
);
319 if (test_mode_
& TEST_MODE_SYNC_NET_READ
)
322 CallbackLater(callback
, num
);
323 return net::ERR_IO_PENDING
;
326 void MockNetworkTransaction::StopCaching() {}
328 bool MockNetworkTransaction::GetFullRequestHeaders(
329 net::HttpRequestHeaders
* headers
) const {
333 void MockNetworkTransaction::DoneReading() {
334 if (transaction_factory_
.get())
335 transaction_factory_
->TransactionDoneReading();
338 const net::HttpResponseInfo
* MockNetworkTransaction::GetResponseInfo() const {
342 net::LoadState
MockNetworkTransaction::GetLoadState() const {
344 return net::LOAD_STATE_READING_RESPONSE
;
345 return net::LOAD_STATE_IDLE
;
348 net::UploadProgress
MockNetworkTransaction::GetUploadProgress() const {
349 return net::UploadProgress();
352 bool MockNetworkTransaction::GetLoadTimingInfo(
353 net::LoadTimingInfo
* load_timing_info
) const {
354 if (socket_log_id_
!= net::NetLog::Source::kInvalidId
) {
355 // The minimal set of times for a request that gets a response, assuming it
356 // gets a new socket.
357 load_timing_info
->socket_reused
= false;
358 load_timing_info
->socket_log_id
= socket_log_id_
;
359 load_timing_info
->connect_timing
.connect_start
= base::TimeTicks::Now();
360 load_timing_info
->connect_timing
.connect_end
= base::TimeTicks::Now();
361 load_timing_info
->send_start
= base::TimeTicks::Now();
362 load_timing_info
->send_end
= base::TimeTicks::Now();
364 // If there's no valid socket ID, just use the generic socket reused values.
365 // No tests currently depend on this, just should not match the values set
367 load_timing_info
->socket_reused
= true;
368 load_timing_info
->send_start
= base::TimeTicks::Now();
369 load_timing_info
->send_end
= base::TimeTicks::Now();
374 void MockNetworkTransaction::SetPriority(net::RequestPriority priority
) {
375 priority_
= priority
;
378 void MockNetworkTransaction::CallbackLater(
379 const net::CompletionCallback
& callback
, int result
) {
380 base::MessageLoop::current()->PostTask(
381 FROM_HERE
, base::Bind(&MockNetworkTransaction::RunCallback
,
382 weak_factory_
.GetWeakPtr(), callback
, result
));
385 void MockNetworkTransaction::RunCallback(
386 const net::CompletionCallback
& callback
, int result
) {
387 callback
.Run(result
);
390 MockNetworkLayer::MockNetworkLayer()
391 : transaction_count_(0),
392 done_reading_called_(false),
393 last_create_transaction_priority_(net::DEFAULT_PRIORITY
) {}
395 MockNetworkLayer::~MockNetworkLayer() {}
397 void MockNetworkLayer::TransactionDoneReading() {
398 done_reading_called_
= true;
401 int MockNetworkLayer::CreateTransaction(
402 net::RequestPriority priority
,
403 scoped_ptr
<net::HttpTransaction
>* trans
,
404 net::HttpTransactionDelegate
* delegate
) {
405 transaction_count_
++;
406 last_create_transaction_priority_
= priority
;
407 scoped_ptr
<MockNetworkTransaction
> mock_transaction(
408 new MockNetworkTransaction(priority
, this));
409 last_transaction_
= mock_transaction
->AsWeakPtr();
410 *trans
= mock_transaction
.Pass();
414 net::HttpCache
* MockNetworkLayer::GetCache() {
418 net::HttpNetworkSession
* MockNetworkLayer::GetSession() {
422 //-----------------------------------------------------------------------------
425 int ReadTransaction(net::HttpTransaction
* trans
, std::string
* result
) {
428 net::TestCompletionCallback callback
;
432 scoped_refptr
<net::IOBuffer
> buf(new net::IOBuffer(256));
433 rv
= trans
->Read(buf
.get(), 256, callback
.callback());
434 if (rv
== net::ERR_IO_PENDING
)
435 rv
= callback
.WaitForResult();
438 content
.append(buf
->data(), rv
);
443 result
->swap(content
);