OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_transaction_unittest.h" | 5 #include "net/http/http_transaction_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/time.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
| 15 #include "net/base/load_timing_info.h" |
14 #include "net/disk_cache/disk_cache.h" | 16 #include "net/disk_cache/disk_cache.h" |
15 #include "net/http/http_cache.h" | 17 #include "net/http/http_cache.h" |
16 #include "net/http/http_request_info.h" | 18 #include "net/http/http_request_info.h" |
17 #include "net/http/http_response_info.h" | 19 #include "net/http/http_response_info.h" |
18 #include "net/http/http_transaction.h" | 20 #include "net/http/http_transaction.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; | 24 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; |
23 static MockTransactionMap mock_transactions; | 25 static MockTransactionMap mock_transactions; |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 NOTREACHED(); | 222 NOTREACHED(); |
221 } | 223 } |
222 } | 224 } |
223 | 225 |
224 MockNetworkTransaction::MockNetworkTransaction( | 226 MockNetworkTransaction::MockNetworkTransaction( |
225 net::RequestPriority priority, | 227 net::RequestPriority priority, |
226 MockNetworkLayer* factory) | 228 MockNetworkLayer* factory) |
227 : weak_factory_(this), | 229 : weak_factory_(this), |
228 data_cursor_(0), | 230 data_cursor_(0), |
229 priority_(priority), | 231 priority_(priority), |
230 transaction_factory_(factory->AsWeakPtr()) { | 232 transaction_factory_(factory->AsWeakPtr()), |
| 233 socket_log_id_(net::NetLog::Source::kInvalidId) { |
231 } | 234 } |
232 | 235 |
233 MockNetworkTransaction::~MockNetworkTransaction() {} | 236 MockNetworkTransaction::~MockNetworkTransaction() {} |
234 | 237 |
235 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | 238 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, |
236 const net::CompletionCallback& callback, | 239 const net::CompletionCallback& callback, |
237 const net::BoundNetLog& net_log) { | 240 const net::BoundNetLog& net_log) { |
238 const MockTransaction* t = FindMockTransaction(request->url); | 241 const MockTransaction* t = FindMockTransaction(request->url); |
239 if (!t) | 242 if (!t) |
240 return net::ERR_FAILED; | 243 return net::ERR_FAILED; |
241 | 244 |
242 test_mode_ = t->test_mode; | 245 test_mode_ = t->test_mode; |
243 | 246 |
244 // Return immediately if we're returning in error. | 247 // Return immediately if we're returning an error. |
245 if (net::OK != t->return_code) { | 248 if (net::OK != t->return_code) { |
246 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 249 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
247 return t->return_code; | 250 return t->return_code; |
248 CallbackLater(callback, t->return_code); | 251 CallbackLater(callback, t->return_code); |
249 return net::ERR_IO_PENDING; | 252 return net::ERR_IO_PENDING; |
250 } | 253 } |
251 | 254 |
252 std::string resp_status = t->status; | 255 std::string resp_status = t->status; |
253 std::string resp_headers = t->response_headers; | 256 std::string resp_headers = t->response_headers; |
254 std::string resp_data = t->data; | 257 std::string resp_data = t->data; |
(...skipping 13 matching lines...) Expand all Loading... |
268 | 271 |
269 response_.response_time = base::Time::Now(); | 272 response_.response_time = base::Time::Now(); |
270 if (!t->response_time.is_null()) | 273 if (!t->response_time.is_null()) |
271 response_.response_time = t->response_time; | 274 response_.response_time = t->response_time; |
272 | 275 |
273 response_.headers = new net::HttpResponseHeaders(header_data); | 276 response_.headers = new net::HttpResponseHeaders(header_data); |
274 response_.vary_data.Init(*request, *response_.headers); | 277 response_.vary_data.Init(*request, *response_.headers); |
275 response_.ssl_info.cert_status = t->cert_status; | 278 response_.ssl_info.cert_status = t->cert_status; |
276 data_ = resp_data; | 279 data_ = resp_data; |
277 | 280 |
| 281 if (net_log.net_log()) |
| 282 socket_log_id_ = net_log.net_log()->NextID(); |
| 283 |
278 if (test_mode_ & TEST_MODE_SYNC_NET_START) | 284 if (test_mode_ & TEST_MODE_SYNC_NET_START) |
279 return net::OK; | 285 return net::OK; |
280 | 286 |
281 CallbackLater(callback, net::OK); | 287 CallbackLater(callback, net::OK); |
282 return net::ERR_IO_PENDING; | 288 return net::ERR_IO_PENDING; |
283 } | 289 } |
284 | 290 |
285 int MockNetworkTransaction::RestartIgnoringLastError( | 291 int MockNetworkTransaction::RestartIgnoringLastError( |
286 const net::CompletionCallback& callback) { | 292 const net::CompletionCallback& callback) { |
287 return net::ERR_FAILED; | 293 return net::ERR_FAILED; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 return net::LOAD_STATE_READING_RESPONSE; | 340 return net::LOAD_STATE_READING_RESPONSE; |
335 return net::LOAD_STATE_IDLE; | 341 return net::LOAD_STATE_IDLE; |
336 } | 342 } |
337 | 343 |
338 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { | 344 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { |
339 return net::UploadProgress(); | 345 return net::UploadProgress(); |
340 } | 346 } |
341 | 347 |
342 bool MockNetworkTransaction::GetLoadTimingInfo( | 348 bool MockNetworkTransaction::GetLoadTimingInfo( |
343 net::LoadTimingInfo* load_timing_info) const { | 349 net::LoadTimingInfo* load_timing_info) const { |
344 return false; | 350 if (socket_log_id_ != net::NetLog::Source::kInvalidId) { |
| 351 // The minimal set of times for a request that gets a response, assuming it |
| 352 // gets a new socket. |
| 353 load_timing_info->socket_reused = false; |
| 354 load_timing_info->socket_log_id = socket_log_id_; |
| 355 load_timing_info->connect_timing.connect_start = base::TimeTicks::Now(); |
| 356 load_timing_info->connect_timing.connect_end = base::TimeTicks::Now(); |
| 357 load_timing_info->send_start = base::TimeTicks::Now(); |
| 358 load_timing_info->send_end = base::TimeTicks::Now(); |
| 359 } else { |
| 360 // If there's no valid socket ID, just use the generic socket reused values. |
| 361 // No tests currently depend on this, just should not match the values set |
| 362 // by a cache hit. |
| 363 load_timing_info->socket_reused = true; |
| 364 load_timing_info->send_start = base::TimeTicks::Now(); |
| 365 load_timing_info->send_end = base::TimeTicks::Now(); |
| 366 } |
| 367 return true; |
345 } | 368 } |
346 | 369 |
347 void MockNetworkTransaction::SetPriority(net::RequestPriority priority) { | 370 void MockNetworkTransaction::SetPriority(net::RequestPriority priority) { |
348 priority_ = priority; | 371 priority_ = priority; |
349 } | 372 } |
350 | 373 |
351 void MockNetworkTransaction::CallbackLater( | 374 void MockNetworkTransaction::CallbackLater( |
352 const net::CompletionCallback& callback, int result) { | 375 const net::CompletionCallback& callback, int result) { |
353 MessageLoop::current()->PostTask( | 376 MessageLoop::current()->PostTask( |
354 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | 377 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 | 432 |
410 if (rv > 0) | 433 if (rv > 0) |
411 content.append(buf->data(), rv); | 434 content.append(buf->data(), rv); |
412 else if (rv < 0) | 435 else if (rv < 0) |
413 return rv; | 436 return rv; |
414 } while (rv > 0); | 437 } while (rv > 0); |
415 | 438 |
416 result->swap(content); | 439 result->swap(content); |
417 return net::OK; | 440 return net::OK; |
418 } | 441 } |
OLD | NEW |