OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 virtual bool MakeGETRequest(const std::string& page_name) = 0; | 253 virtual bool MakeGETRequest(const std::string& page_name) = 0; |
254 | 254 |
255 std::wstring GetDataDirectory() { | 255 std::wstring GetDataDirectory() { |
256 return launcher_.GetDocumentRootPath().ToWStringHack(); | 256 return launcher_.GetDocumentRootPath().ToWStringHack(); |
257 } | 257 } |
258 | 258 |
259 protected: | 259 protected: |
260 bool Start(net::TestServerLauncher::Protocol protocol, | 260 bool Start(net::TestServerLauncher::Protocol protocol, |
261 const std::string& host_name, int port, | 261 const std::string& host_name, int port, |
262 const FilePath& document_root, | 262 const FilePath& document_root, |
263 const FilePath& cert_path) { | 263 const FilePath& cert_path, |
| 264 const std::wstring& file_root_url) { |
264 std::string blank; | 265 std::string blank; |
265 return Start(protocol, host_name, port, document_root, cert_path, | 266 return Start(protocol, host_name, port, document_root, cert_path, |
266 blank, blank); | 267 file_root_url, blank, blank); |
267 } | 268 } |
268 | 269 |
269 bool Start(net::TestServerLauncher::Protocol protocol, | 270 bool Start(net::TestServerLauncher::Protocol protocol, |
270 const std::string& host_name, int port, | 271 const std::string& host_name, int port, |
271 const FilePath& document_root, | 272 const FilePath& document_root, |
272 const FilePath& cert_path, | 273 const FilePath& cert_path, |
| 274 const std::wstring& file_root_url, |
273 const std::string& url_user, | 275 const std::string& url_user, |
274 const std::string& url_password) { | 276 const std::string& url_password) { |
275 if (!launcher_.Start(protocol, | 277 if (!launcher_.Start(protocol, |
276 host_name, port, document_root, cert_path)) | 278 host_name, port, document_root, cert_path, file_root_url)) |
277 return false; | 279 return false; |
278 | 280 |
279 std::string scheme; | 281 std::string scheme; |
280 if (protocol == net::TestServerLauncher::ProtoFTP) | 282 if (protocol == net::TestServerLauncher::ProtoFTP) |
281 scheme = "ftp"; | 283 scheme = "ftp"; |
282 else | 284 else |
283 scheme = "http"; | 285 scheme = "http"; |
284 if (!cert_path.empty()) | 286 if (!cert_path.empty()) |
285 scheme.push_back('s'); | 287 scheme.push_back('s'); |
286 | 288 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 protected: | 333 protected: |
332 explicit HTTPTestServer() : loop_(NULL) { | 334 explicit HTTPTestServer() : loop_(NULL) { |
333 } | 335 } |
334 | 336 |
335 public: | 337 public: |
336 // Creates and returns a new HTTPTestServer. If |loop| is non-null, requests | 338 // Creates and returns a new HTTPTestServer. If |loop| is non-null, requests |
337 // are serviced on it, otherwise a new thread and message loop are created. | 339 // are serviced on it, otherwise a new thread and message loop are created. |
338 static scoped_refptr<HTTPTestServer> CreateServer( | 340 static scoped_refptr<HTTPTestServer> CreateServer( |
339 const std::wstring& document_root, | 341 const std::wstring& document_root, |
340 MessageLoop* loop) { | 342 MessageLoop* loop) { |
| 343 return CreateServerWithFileRootURL(document_root, std::wstring(), loop); |
| 344 } |
| 345 |
| 346 static scoped_refptr<HTTPTestServer> CreateServerWithFileRootURL( |
| 347 const std::wstring& document_root, |
| 348 const std::wstring& file_root_url, |
| 349 MessageLoop* loop) { |
341 scoped_refptr<HTTPTestServer> test_server = new HTTPTestServer(); | 350 scoped_refptr<HTTPTestServer> test_server = new HTTPTestServer(); |
342 test_server->loop_ = loop; | 351 test_server->loop_ = loop; |
343 FilePath no_cert; | 352 FilePath no_cert; |
344 FilePath docroot = FilePath::FromWStringHack(document_root); | 353 FilePath docroot = FilePath::FromWStringHack(document_root); |
345 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | 354 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, |
346 kDefaultHostName, kHTTPDefaultPort, | 355 kDefaultHostName, kHTTPDefaultPort, |
347 docroot, no_cert)) { | 356 docroot, no_cert, file_root_url)) { |
348 return NULL; | 357 return NULL; |
349 } | 358 } |
350 return test_server; | 359 return test_server; |
351 } | 360 } |
352 | 361 |
353 // A subclass may wish to send the request in a different manner | 362 // A subclass may wish to send the request in a different manner |
354 virtual bool MakeGETRequest(const std::string& page_name) { | 363 virtual bool MakeGETRequest(const std::string& page_name) { |
355 const GURL& url = TestServerPage(page_name); | 364 const GURL& url = TestServerPage(page_name); |
356 | 365 |
357 // Spin up a background thread for this request so that we have access to | 366 // Spin up a background thread for this request so that we have access to |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 // TODO(dkegel): HTTPSTestServer should not require an instance to specify | 440 // TODO(dkegel): HTTPSTestServer should not require an instance to specify |
432 // stock test certificates | 441 // stock test certificates |
433 static scoped_refptr<HTTPSTestServer> CreateGoodServer( | 442 static scoped_refptr<HTTPSTestServer> CreateGoodServer( |
434 const std::wstring& document_root) { | 443 const std::wstring& document_root) { |
435 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | 444 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); |
436 FilePath docroot = FilePath::FromWStringHack(document_root); | 445 FilePath docroot = FilePath::FromWStringHack(document_root); |
437 FilePath certpath = test_server->launcher_.GetOKCertPath(); | 446 FilePath certpath = test_server->launcher_.GetOKCertPath(); |
438 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | 447 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, |
439 net::TestServerLauncher::kHostName, | 448 net::TestServerLauncher::kHostName, |
440 net::TestServerLauncher::kOKHTTPSPort, | 449 net::TestServerLauncher::kOKHTTPSPort, |
441 docroot, certpath)) { | 450 docroot, certpath, std::wstring())) { |
442 return NULL; | 451 return NULL; |
443 } | 452 } |
444 return test_server; | 453 return test_server; |
445 } | 454 } |
446 | 455 |
447 // Create a server with an up to date certificate for the wrong hostname | 456 // Create a server with an up to date certificate for the wrong hostname |
448 // for this host | 457 // for this host |
449 static scoped_refptr<HTTPSTestServer> CreateMismatchedServer( | 458 static scoped_refptr<HTTPSTestServer> CreateMismatchedServer( |
450 const std::wstring& document_root) { | 459 const std::wstring& document_root) { |
451 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | 460 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); |
452 FilePath docroot = FilePath::FromWStringHack(document_root); | 461 FilePath docroot = FilePath::FromWStringHack(document_root); |
453 FilePath certpath = test_server->launcher_.GetOKCertPath(); | 462 FilePath certpath = test_server->launcher_.GetOKCertPath(); |
454 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | 463 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, |
455 net::TestServerLauncher::kMismatchedHostName, | 464 net::TestServerLauncher::kMismatchedHostName, |
456 net::TestServerLauncher::kOKHTTPSPort, | 465 net::TestServerLauncher::kOKHTTPSPort, |
457 docroot, certpath)) { | 466 docroot, certpath, std::wstring())) { |
458 return NULL; | 467 return NULL; |
459 } | 468 } |
460 return test_server; | 469 return test_server; |
461 } | 470 } |
462 | 471 |
463 // Create a server with an expired certificate | 472 // Create a server with an expired certificate |
464 static scoped_refptr<HTTPSTestServer> CreateExpiredServer( | 473 static scoped_refptr<HTTPSTestServer> CreateExpiredServer( |
465 const std::wstring& document_root) { | 474 const std::wstring& document_root) { |
466 HTTPSTestServer* test_server = new HTTPSTestServer(); | 475 HTTPSTestServer* test_server = new HTTPSTestServer(); |
467 FilePath docroot = FilePath::FromWStringHack(document_root); | 476 FilePath docroot = FilePath::FromWStringHack(document_root); |
468 FilePath certpath = test_server->launcher_.GetExpiredCertPath(); | 477 FilePath certpath = test_server->launcher_.GetExpiredCertPath(); |
469 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | 478 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, |
470 net::TestServerLauncher::kHostName, | 479 net::TestServerLauncher::kHostName, |
471 net::TestServerLauncher::kBadHTTPSPort, | 480 net::TestServerLauncher::kBadHTTPSPort, |
472 docroot, certpath)) { | 481 docroot, certpath, std::wstring())) { |
473 return NULL; | 482 return NULL; |
474 } | 483 } |
475 return test_server; | 484 return test_server; |
476 } | 485 } |
477 | 486 |
478 // Create a server with an arbitrary certificate | 487 // Create a server with an arbitrary certificate |
479 static scoped_refptr<HTTPSTestServer> CreateServer( | 488 static scoped_refptr<HTTPSTestServer> CreateServer( |
480 const std::string& host_name, int port, | 489 const std::string& host_name, int port, |
481 const std::wstring& document_root, | 490 const std::wstring& document_root, |
482 const std::wstring& cert_path) { | 491 const std::wstring& cert_path) { |
483 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); | 492 scoped_refptr<HTTPSTestServer> test_server = new HTTPSTestServer(); |
484 FilePath docroot = FilePath::FromWStringHack(document_root); | 493 FilePath docroot = FilePath::FromWStringHack(document_root); |
485 FilePath certpath = FilePath::FromWStringHack(cert_path); | 494 FilePath certpath = FilePath::FromWStringHack(cert_path); |
486 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, | 495 if (!test_server->Start(net::TestServerLauncher::ProtoHTTP, |
487 host_name, port, docroot, certpath)) { | 496 host_name, port, docroot, certpath, std::wstring())) { |
488 return NULL; | 497 return NULL; |
489 } | 498 } |
490 return test_server; | 499 return test_server; |
491 } | 500 } |
492 | 501 |
493 virtual ~HTTPSTestServer() { | 502 virtual ~HTTPSTestServer() { |
494 } | 503 } |
495 | 504 |
496 protected: | 505 protected: |
497 std::wstring cert_path_; | 506 std::wstring cert_path_; |
(...skipping 12 matching lines...) Expand all Loading... |
510 } | 519 } |
511 | 520 |
512 static scoped_refptr<FTPTestServer> CreateServer( | 521 static scoped_refptr<FTPTestServer> CreateServer( |
513 const std::wstring& document_root, | 522 const std::wstring& document_root, |
514 const std::string& url_user, | 523 const std::string& url_user, |
515 const std::string& url_password) { | 524 const std::string& url_password) { |
516 scoped_refptr<FTPTestServer> test_server = new FTPTestServer(); | 525 scoped_refptr<FTPTestServer> test_server = new FTPTestServer(); |
517 FilePath docroot = FilePath::FromWStringHack(document_root); | 526 FilePath docroot = FilePath::FromWStringHack(document_root); |
518 FilePath no_cert; | 527 FilePath no_cert; |
519 if (!test_server->Start(net::TestServerLauncher::ProtoFTP, | 528 if (!test_server->Start(net::TestServerLauncher::ProtoFTP, |
520 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, | 529 kDefaultHostName, kFTPDefaultPort, docroot, no_cert, std::wstring(), |
521 url_user, url_password)) { | 530 url_user, url_password)) { |
522 return NULL; | 531 return NULL; |
523 } | 532 } |
524 return test_server; | 533 return test_server; |
525 } | 534 } |
526 | 535 |
527 virtual bool MakeGETRequest(const std::string& page_name) { | 536 virtual bool MakeGETRequest(const std::string& page_name) { |
528 const GURL& url = TestServerPage(base_address_, page_name); | 537 const GURL& url = TestServerPage(base_address_, page_name); |
529 TestDelegate d; | 538 TestDelegate d; |
530 URLRequest request(url, &d); | 539 URLRequest request(url, &d); |
531 request.set_context(new TestURLRequestContext()); | 540 request.set_context(new TestURLRequestContext()); |
532 request.set_method("GET"); | 541 request.set_method("GET"); |
533 request.Start(); | 542 request.Start(); |
534 EXPECT_TRUE(request.is_pending()); | 543 EXPECT_TRUE(request.is_pending()); |
535 | 544 |
536 MessageLoop::current()->Run(); | 545 MessageLoop::current()->Run(); |
537 if (request.is_pending()) | 546 if (request.is_pending()) |
538 return false; | 547 return false; |
539 | 548 |
540 return true; | 549 return true; |
541 } | 550 } |
542 | 551 |
543 }; | 552 }; |
544 | 553 |
545 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 554 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
OLD | NEW |