| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "net/http/http_auth_handler_factory.h" | 34 #include "net/http/http_auth_handler_factory.h" |
| 35 #include "net/http/http_cache.h" | 35 #include "net/http/http_cache.h" |
| 36 #include "net/http/http_network_layer.h" | 36 #include "net/http/http_network_layer.h" |
| 37 #include "net/test/test_server.h" | 37 #include "net/test/test_server.h" |
| 38 #include "net/url_request/url_request.h" | 38 #include "net/url_request/url_request.h" |
| 39 #include "net/url_request/url_request_context.h" | 39 #include "net/url_request/url_request_context.h" |
| 40 #include "net/proxy/proxy_service.h" | 40 #include "net/proxy/proxy_service.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 42 #include "googleurl/src/url_util.h" | 42 #include "googleurl/src/url_util.h" |
| 43 | 43 |
| 44 const int kHTTPDefaultPort = 1337; | |
| 45 const int kFTPDefaultPort = 1338; | |
| 46 | |
| 47 const std::string kDefaultHostName("localhost"); | |
| 48 | |
| 49 using base::TimeDelta; | 44 using base::TimeDelta; |
| 50 | 45 |
| 51 //----------------------------------------------------------------------------- | 46 //----------------------------------------------------------------------------- |
| 52 | 47 |
| 53 class TestCookiePolicy : public net::CookiePolicy { | 48 class TestCookiePolicy : public net::CookiePolicy { |
| 54 public: | 49 public: |
| 55 enum Options { | 50 enum Options { |
| 56 NO_GET_COOKIES = 1 << 0, | 51 NO_GET_COOKIES = 1 << 0, |
| 57 NO_SET_COOKIE = 1 << 1, | 52 NO_SET_COOKIE = 1 << 1, |
| 58 ASYNC = 1 << 2, | 53 ASYNC = 1 << 2, |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 int set_cookie_count_; | 384 int set_cookie_count_; |
| 390 bool received_data_before_response_; | 385 bool received_data_before_response_; |
| 391 bool request_failed_; | 386 bool request_failed_; |
| 392 bool have_certificate_errors_; | 387 bool have_certificate_errors_; |
| 393 std::string data_received_; | 388 std::string data_received_; |
| 394 | 389 |
| 395 // our read buffer | 390 // our read buffer |
| 396 scoped_refptr<net::IOBuffer> buf_; | 391 scoped_refptr<net::IOBuffer> buf_; |
| 397 }; | 392 }; |
| 398 | 393 |
| 399 //----------------------------------------------------------------------------- | |
| 400 | |
| 401 // This object bounds the lifetime of an external python-based HTTP/FTP server | |
| 402 // that can provide various responses useful for testing. | |
| 403 class BaseTestServer : public base::RefCounted<BaseTestServer> { | |
| 404 protected: | |
| 405 BaseTestServer() {} | |
| 406 | |
| 407 public: | |
| 408 bool WaitToFinish(int milliseconds) { | |
| 409 return launcher_.WaitToFinish(milliseconds); | |
| 410 } | |
| 411 | |
| 412 bool Stop() { | |
| 413 return launcher_.Stop(); | |
| 414 } | |
| 415 | |
| 416 GURL TestServerPage(const std::string& base_address, | |
| 417 const std::string& path) { | |
| 418 return GURL(base_address + path); | |
| 419 } | |
| 420 | |
| 421 GURL TestServerPage(const std::string& path) { | |
| 422 // TODO(phajdan.jr): Check for problems with IPv6. | |
| 423 return GURL(scheme_ + "://" + host_name_ + ":" + port_str_ + "/" + path); | |
| 424 } | |
| 425 | |
| 426 GURL TestServerPage(const std::string& path, | |
| 427 const std::string& user, | |
| 428 const std::string& password) { | |
| 429 // TODO(phajdan.jr): Check for problems with IPv6. | |
| 430 | |
| 431 if (password.empty()) | |
| 432 return GURL(scheme_ + "://" + user + "@" + | |
| 433 host_name_ + ":" + port_str_ + "/" + path); | |
| 434 | |
| 435 return GURL(scheme_ + "://" + user + ":" + password + | |
| 436 "@" + host_name_ + ":" + port_str_ + "/" + path); | |
| 437 } | |
| 438 | |
| 439 FilePath GetDataDirectory() { | |
| 440 return launcher_.GetDocumentRootPath(); | |
| 441 } | |
| 442 | |
| 443 protected: | |
| 444 friend class base::RefCounted<BaseTestServer>; | |
| 445 virtual ~BaseTestServer() { } | |
| 446 | |
| 447 bool Start(net::TestServerLauncher::Protocol protocol, | |
| 448 const std::string& host_name, int port, | |
| 449 const FilePath& document_root, | |
| 450 const FilePath& cert_path, | |
| 451 const std::wstring& file_root_url) { | |
| 452 if (!launcher_.Start(protocol, | |
| 453 host_name, port, document_root, cert_path, file_root_url)) | |
| 454 return false; | |
| 455 | |
| 456 if (protocol == net::TestServerLauncher::ProtoFTP) | |
| 457 scheme_ = "ftp"; | |
| 458 else | |
| 459 scheme_ = "http"; | |
| 460 if (!cert_path.empty()) | |
| 461 scheme_.push_back('s'); | |
| 462 | |
| 463 host_name_ = host_name; | |
| 464 port_str_ = IntToString(port); | |
| 465 return true; | |
| 466 } | |
| 467 | |
| 468 net::TestServerLauncher launcher_; | |
| 469 std::string scheme_; | |
| 470 std::string host_name_; | |
| 471 std::string port_str_; | |
| 472 }; | |
| 473 | |
| 474 //----------------------------------------------------------------------------- | |
| 475 | |
| 476 // HTTP | |
| 477 class HTTPTestServer : public BaseTestServer { | |
| 478 protected: | |
| 479 HTTPTestServer() {} | |
| 480 | |
| 481 public: | |
| 482 // Creates and returns a new HTTPTestServer. If |loop| is non-null, requests | |
| 483 // are serviced on it, otherwise a new thread and message loop are created. | |
| 484 static scoped_refptr<HTTPTestServer> CreateServer( | |
| 485 const std::wstring& document_root) { | |
| 486 return CreateServerWithFileRootURL(document_root, std::wstring()); | |
| 487 } | |
| 488 | |
| 489 static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL( | |
| 490 const std::wstring& document_root, | |
| 491 const std::wstring& file_root_url) { | |
| 492 scoped_refptr<HTTPTestServer> test_server(new HTTPTestServer()); | |
| 493 FilePath no_cert; | |
| 494 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 495 if (!StartTestServer(test_server.get(), docroot, no_cert, file_root_url)) | |
| 496 return NULL; | |
| 497 return test_server; | |
| 498 } | |
| 499 | |
| 500 static bool StartTestServer(HTTPTestServer* server, | |
| 501 const FilePath& document_root, | |
| 502 const FilePath& cert_path, | |
| 503 const std::wstring& file_root_url) { | |
| 504 return server->Start(net::TestServerLauncher::ProtoHTTP, kDefaultHostName, | |
| 505 kHTTPDefaultPort, document_root, cert_path, | |
| 506 file_root_url); | |
| 507 } | |
| 508 | |
| 509 virtual std::string scheme() { return "http"; } | |
| 510 }; | |
| 511 | |
| 512 //----------------------------------------------------------------------------- | |
| 513 | |
| 514 class HTTPSTestServer : public HTTPTestServer { | |
| 515 protected: | |
| 516 explicit HTTPSTestServer() { | |
| 517 } | |
| 518 | |
| 519 public: | |
| 520 // Create a server with a valid certificate | |
| 521 // TODO(dkegel): HTTPSTestServer should not require an instance to specify | |
| 522 // stock test certificates | |
| 523 static scoped_refptr<HTTPSTestServer> CreateGoodServer( | |
| 524 const std::wstring& document_root) { | |
| 525 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | |
| 526 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 527 FilePath certpath = test_server->launcher_.GetOKCertPath(); | |
| 528 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | |
| 529 net::TestServerLauncher::kHostName, | |
| 530 net::TestServerLauncher::kOKHTTPSPort, | |
| 531 docroot, certpath, std::wstring())) { | |
| 532 return NULL; | |
| 533 } | |
| 534 return test_server; | |
| 535 } | |
| 536 | |
| 537 // Create a server with an up to date certificate for the wrong hostname | |
| 538 // for this host | |
| 539 static scoped_refptr<HTTPSTestServer> CreateMismatchedServer( | |
| 540 const std::wstring& document_root) { | |
| 541 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | |
| 542 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 543 FilePath certpath = test_server->launcher_.GetOKCertPath(); | |
| 544 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | |
| 545 net::TestServerLauncher::kMismatchedHostName, | |
| 546 net::TestServerLauncher::kOKHTTPSPort, | |
| 547 docroot, certpath, std::wstring())) { | |
| 548 return NULL; | |
| 549 } | |
| 550 return test_server; | |
| 551 } | |
| 552 | |
| 553 // Create a server with an expired certificate | |
| 554 static scoped_refptr<HTTPSTestServer> CreateExpiredServer( | |
| 555 const std::wstring& document_root) { | |
| 556 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | |
| 557 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 558 FilePath certpath = test_server->launcher_.GetExpiredCertPath(); | |
| 559 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | |
| 560 net::TestServerLauncher::kHostName, | |
| 561 net::TestServerLauncher::kBadHTTPSPort, | |
| 562 docroot, certpath, std::wstring())) { | |
| 563 return NULL; | |
| 564 } | |
| 565 return test_server; | |
| 566 } | |
| 567 | |
| 568 // Create a server with an arbitrary certificate | |
| 569 static scoped_refptr<HTTPSTestServer> CreateServer( | |
| 570 const std::string& host_name, int port, | |
| 571 const std::wstring& document_root, | |
| 572 const std::wstring& cert_path) { | |
| 573 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | |
| 574 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 575 FilePath certpath = FilePath::FromWStringHack(cert_path); | |
| 576 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | |
| 577 host_name, port, docroot, certpath, std::wstring())) { | |
| 578 return NULL; | |
| 579 } | |
| 580 return test_server; | |
| 581 } | |
| 582 | |
| 583 protected: | |
| 584 std::wstring cert_path_; | |
| 585 | |
| 586 private: | |
| 587 virtual ~HTTPSTestServer() {} | |
| 588 }; | |
| 589 | |
| 590 //----------------------------------------------------------------------------- | |
| 591 | |
| 592 class FTPTestServer : public BaseTestServer { | |
| 593 public: | |
| 594 FTPTestServer() { | |
| 595 } | |
| 596 | |
| 597 static scoped_refptr<FTPTestServer> CreateServer( | |
| 598 const std::wstring& document_root) { | |
| 599 scoped_refptr<FTPTestServer> test_server = new FTPTestServer(); | |
| 600 FilePath docroot = FilePath::FromWStringHack(document_root); | |
| 601 FilePath no_cert; | |
| 602 if (!test_server->Start(net::TestServerLauncher::ProtoFTP, | |
| 603 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring())) { | |
| 604 return NULL; | |
| 605 } | |
| 606 return test_server; | |
| 607 } | |
| 608 | |
| 609 private: | |
| 610 ~FTPTestServer() {} | |
| 611 }; | |
| 612 | |
| 613 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 394 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
| OLD | NEW |