Chromium Code Reviews| 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 "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/http/http_cache.h" | 15 #include "net/http/http_cache.h" |
| 16 #include "net/http/http_request_info.h" | 16 #include "net/http/http_request_info.h" |
| 17 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 18 #include "net/http/http_transaction.h" | 18 #include "net/http/http_transaction.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; | 22 typedef base::hash_map<std::string, const MockTransaction*> MockTransactionMap; |
| 23 static MockTransactionMap mock_transactions; | 23 static MockTransactionMap mock_transactions; |
| 24 | |
| 25 // Utility class that delegates to another HttpTransaction. Useful to | |
| 26 // pass to classes that take ownership of the HttpTransaction. | |
|
mmenke
2013/03/21 15:39:23
I think this comment should be much more specific.
akalin
2013/03/21 23:43:48
moot.
| |
| 27 class DelegatingTransaction : public net::HttpTransaction { | |
| 28 public: | |
| 29 // Assumes |delegate| will outlive this object. | |
| 30 explicit DelegatingTransaction(net::HttpTransaction* delegate) | |
|
mmenke
2013/03/21 15:39:23
Think this should be a MockHttpTransaction.
The u
akalin
2013/03/21 23:43:48
moot.
| |
| 31 : delegate_(delegate) {} | |
| 32 | |
| 33 virtual ~DelegatingTransaction() {} | |
| 34 | |
| 35 // net::HttpTransaction implementation. | |
| 36 virtual int Start(const net::HttpRequestInfo* request_info, | |
| 37 const net::CompletionCallback& callback, | |
| 38 const net::BoundNetLog& net_log) OVERRIDE { | |
| 39 return delegate_->Start(request_info, callback, net_log); | |
| 40 } | |
| 41 | |
| 42 virtual int RestartIgnoringLastError( | |
| 43 const net::CompletionCallback& callback) OVERRIDE { | |
| 44 return delegate_->RestartIgnoringLastError(callback); | |
| 45 } | |
| 46 | |
| 47 virtual int RestartWithCertificate( | |
| 48 net::X509Certificate* client_cert, | |
| 49 const net::CompletionCallback& callback) OVERRIDE { | |
| 50 return delegate_->RestartWithCertificate(client_cert, callback); | |
| 51 } | |
| 52 | |
| 53 virtual int RestartWithAuth( | |
| 54 const net::AuthCredentials& credentials, | |
| 55 const net::CompletionCallback& callback) OVERRIDE { | |
| 56 return delegate_->RestartWithAuth(credentials, callback); | |
| 57 } | |
| 58 | |
| 59 virtual bool IsReadyToRestartForAuth() OVERRIDE { | |
| 60 return delegate_->IsReadyToRestartForAuth(); | |
| 61 } | |
| 62 | |
| 63 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 64 const net::CompletionCallback& callback) OVERRIDE { | |
| 65 return delegate_->Read(buf, buf_len, callback); | |
| 66 } | |
| 67 | |
| 68 virtual void StopCaching() OVERRIDE { | |
| 69 delegate_->StopCaching(); | |
| 70 } | |
| 71 | |
| 72 virtual void DoneReading() OVERRIDE { | |
| 73 delegate_->DoneReading(); | |
| 74 } | |
| 75 | |
| 76 virtual const net::HttpResponseInfo* GetResponseInfo() const OVERRIDE { | |
| 77 return delegate_->GetResponseInfo(); | |
| 78 } | |
| 79 | |
| 80 virtual net::LoadState GetLoadState() const OVERRIDE { | |
| 81 return delegate_->GetLoadState(); | |
| 82 } | |
| 83 | |
| 84 virtual net::UploadProgress GetUploadProgress() const OVERRIDE { | |
| 85 return delegate_->GetUploadProgress(); | |
| 86 } | |
| 87 | |
| 88 virtual bool GetLoadTimingInfo( | |
| 89 net::LoadTimingInfo* load_timing_info) const OVERRIDE { | |
| 90 return delegate_->GetLoadTimingInfo(load_timing_info); | |
| 91 } | |
| 92 | |
| 93 virtual void SetPriority(net::RequestPriority priority) OVERRIDE { | |
| 94 return delegate_->SetPriority(priority); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 net::HttpTransaction* const delegate_; | |
| 99 }; | |
| 100 | |
| 24 } // namespace | 101 } // namespace |
| 25 | 102 |
| 26 //----------------------------------------------------------------------------- | 103 //----------------------------------------------------------------------------- |
| 27 // mock transaction data | 104 // mock transaction data |
| 28 | 105 |
| 29 const MockTransaction kSimpleGET_Transaction = { | 106 const MockTransaction kSimpleGET_Transaction = { |
| 30 "http://www.google.com/", | 107 "http://www.google.com/", |
| 31 "GET", | 108 "GET", |
| 32 base::Time(), | 109 base::Time(), |
| 33 "", | 110 "", |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 default: | 291 default: |
| 215 NOTREACHED(); | 292 NOTREACHED(); |
| 216 } | 293 } |
| 217 } | 294 } |
| 218 | 295 |
| 219 MockNetworkTransaction::MockNetworkTransaction( | 296 MockNetworkTransaction::MockNetworkTransaction( |
| 220 net::RequestPriority priority, | 297 net::RequestPriority priority, |
| 221 MockNetworkLayer* factory) | 298 MockNetworkLayer* factory) |
| 222 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 299 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 223 data_cursor_(0), | 300 data_cursor_(0), |
| 301 priority_(priority), | |
| 224 transaction_factory_(factory->AsWeakPtr()) { | 302 transaction_factory_(factory->AsWeakPtr()) { |
| 225 } | 303 } |
| 226 | 304 |
| 227 MockNetworkTransaction::~MockNetworkTransaction() {} | 305 MockNetworkTransaction::~MockNetworkTransaction() {} |
| 228 | 306 |
| 229 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, | 307 int MockNetworkTransaction::Start(const net::HttpRequestInfo* request, |
| 230 const net::CompletionCallback& callback, | 308 const net::CompletionCallback& callback, |
| 231 const net::BoundNetLog& net_log) { | 309 const net::BoundNetLog& net_log) { |
| 232 const MockTransaction* t = FindMockTransaction(request->url); | 310 const MockTransaction* t = FindMockTransaction(request->url); |
| 233 if (!t) | 311 if (!t) |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 | 399 |
| 322 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { | 400 net::UploadProgress MockNetworkTransaction::GetUploadProgress() const { |
| 323 return net::UploadProgress(); | 401 return net::UploadProgress(); |
| 324 } | 402 } |
| 325 | 403 |
| 326 bool MockNetworkTransaction::GetLoadTimingInfo( | 404 bool MockNetworkTransaction::GetLoadTimingInfo( |
| 327 net::LoadTimingInfo* load_timing_info) const { | 405 net::LoadTimingInfo* load_timing_info) const { |
| 328 return false; | 406 return false; |
| 329 } | 407 } |
| 330 | 408 |
| 409 void MockNetworkTransaction::SetPriority(net::RequestPriority priority) { | |
| 410 priority_ = priority; | |
| 411 } | |
| 412 | |
| 331 void MockNetworkTransaction::CallbackLater( | 413 void MockNetworkTransaction::CallbackLater( |
| 332 const net::CompletionCallback& callback, int result) { | 414 const net::CompletionCallback& callback, int result) { |
| 333 MessageLoop::current()->PostTask( | 415 MessageLoop::current()->PostTask( |
| 334 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | 416 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, |
| 335 weak_factory_.GetWeakPtr(), callback, result)); | 417 weak_factory_.GetWeakPtr(), callback, result)); |
| 336 } | 418 } |
| 337 | 419 |
| 338 void MockNetworkTransaction::RunCallback( | 420 void MockNetworkTransaction::RunCallback( |
| 339 const net::CompletionCallback& callback, int result) { | 421 const net::CompletionCallback& callback, int result) { |
| 340 callback.Run(result); | 422 callback.Run(result); |
| 341 } | 423 } |
| 342 | 424 |
| 343 MockNetworkLayer::MockNetworkLayer() | 425 MockNetworkLayer::MockNetworkLayer() |
| 344 : transaction_count_(0), done_reading_called_(false) {} | 426 : transaction_count_(0), |
| 427 done_reading_called_(false) {} | |
| 345 | 428 |
| 346 MockNetworkLayer::~MockNetworkLayer() {} | 429 MockNetworkLayer::~MockNetworkLayer() {} |
| 347 | 430 |
| 348 void MockNetworkLayer::TransactionDoneReading() { | 431 void MockNetworkLayer::TransactionDoneReading() { |
| 349 done_reading_called_ = true; | 432 done_reading_called_ = true; |
| 350 } | 433 } |
| 351 | 434 |
| 352 int MockNetworkLayer::CreateTransaction( | 435 int MockNetworkLayer::CreateTransaction( |
| 353 net::RequestPriority priority, | 436 net::RequestPriority priority, |
| 354 scoped_ptr<net::HttpTransaction>* trans, | 437 scoped_ptr<net::HttpTransaction>* trans, |
| 355 net::HttpTransactionDelegate* delegate) { | 438 net::HttpTransactionDelegate* delegate) { |
| 356 transaction_count_++; | 439 transaction_count_++; |
| 357 trans->reset(new MockNetworkTransaction(priority, this)); | 440 last_transaction_.reset(new MockNetworkTransaction(priority, this)); |
| 441 // Assume |trans| won't outlive us. | |
| 442 trans->reset(new DelegatingTransaction(last_transaction_.get())); | |
| 358 return net::OK; | 443 return net::OK; |
| 359 } | 444 } |
| 360 | 445 |
| 361 net::HttpCache* MockNetworkLayer::GetCache() { | 446 net::HttpCache* MockNetworkLayer::GetCache() { |
| 362 return NULL; | 447 return NULL; |
| 363 } | 448 } |
| 364 | 449 |
| 365 net::HttpNetworkSession* MockNetworkLayer::GetSession() { | 450 net::HttpNetworkSession* MockNetworkLayer::GetSession() { |
| 366 return NULL; | 451 return NULL; |
| 367 } | 452 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 383 | 468 |
| 384 if (rv > 0) | 469 if (rv > 0) |
| 385 content.append(buf->data(), rv); | 470 content.append(buf->data(), rv); |
| 386 else if (rv < 0) | 471 else if (rv < 0) |
| 387 return rv; | 472 return rv; |
| 388 } while (rv > 0); | 473 } while (rv > 0); |
| 389 | 474 |
| 390 result->swap(content); | 475 result->swap(content); |
| 391 return net::OK; | 476 return net::OK; |
| 392 } | 477 } |
| OLD | NEW |