Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Side by Side Diff: webkit/appcache/appcache_update_job_unittest.cc

Issue 10855209: Refactoring: ProtocolHandler::MaybeCreateJob takes NetworkDelegate as argument (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Latest merge Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 } 44 }
45 45
46 static GURL GetMockHttpsUrl(const std::string& path) { 46 static GURL GetMockHttpsUrl(const std::string& path) {
47 return GURL("https://mockhost/" + path); 47 return GURL("https://mockhost/" + path);
48 } 48 }
49 49
50 static GURL GetMockCrossOriginHttpsUrl(const std::string& path) { 50 static GURL GetMockCrossOriginHttpsUrl(const std::string& path) {
51 return GURL("https://cross_origin_host/" + path); 51 return GURL("https://cross_origin_host/" + path);
52 } 52 }
53 53
54 static net::URLRequestJob* JobFactory(net::URLRequest* request) { 54 static net::URLRequestJob* JobFactory(
55 net::URLRequest* request, net::NetworkDelegate* network_delegate) {
55 if (request->url().host() != "mockhost" && 56 if (request->url().host() != "mockhost" &&
56 request->url().host() != "cross_origin_host") 57 request->url().host() != "cross_origin_host")
57 return new net::URLRequestErrorJob(request, -100); 58 return new net::URLRequestErrorJob(request, network_delegate, -100);
58 59
59 std::string headers, body; 60 std::string headers, body;
60 GetMockResponse(request->url().path(), &headers, &body); 61 GetMockResponse(request->url().path(), &headers, &body);
61 return new net::URLRequestTestJob(request, headers, body, true); 62 return new net::URLRequestTestJob(
63 request, network_delegate, headers, body, true);
62 } 64 }
63 65
64 private: 66 private:
65 static void GetMockResponse(const std::string& path, 67 static void GetMockResponse(const std::string& path,
66 std::string* headers, 68 std::string* headers,
67 std::string* body) { 69 std::string* body) {
68 const char ok_headers[] = 70 const char ok_headers[] =
69 "HTTP/1.1 200 OK\0" 71 "HTTP/1.1 200 OK\0"
70 "\0"; 72 "\0";
71 const char error_headers[] = 73 const char error_headers[] =
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 (*headers) = std::string(not_found_headers, 189 (*headers) = std::string(not_found_headers,
188 arraysize(not_found_headers)); 190 arraysize(not_found_headers));
189 (*body) = ""; 191 (*body) = "";
190 } 192 }
191 } 193 }
192 }; 194 };
193 195
194 class MockHttpServerJobFactory 196 class MockHttpServerJobFactory
195 : public net::URLRequestJobFactory::ProtocolHandler { 197 : public net::URLRequestJobFactory::ProtocolHandler {
196 public: 198 public:
197 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { 199 virtual net::URLRequestJob* MaybeCreateJob(
198 return MockHttpServer::JobFactory(request); 200 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
201 return MockHttpServer::JobFactory(request, network_delegate);
199 } 202 }
200 }; 203 };
201 204
202 inline bool operator==(const Namespace& lhs, const Namespace& rhs) { 205 inline bool operator==(const Namespace& lhs, const Namespace& rhs) {
203 return lhs.type == rhs.type && 206 return lhs.type == rhs.type &&
204 lhs.namespace_url == rhs.namespace_url && 207 lhs.namespace_url == rhs.namespace_url &&
205 lhs.target_url == rhs.target_url; 208 lhs.target_url == rhs.target_url;
206 } 209 }
207 210
208 } // namespace 211 } // namespace
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 332
330 // Add ability for frontend to add master entries to an inprogress update. 333 // Add ability for frontend to add master entries to an inprogress update.
331 EventID start_update_trigger_; 334 EventID start_update_trigger_;
332 AppCacheUpdateJob* update_; 335 AppCacheUpdateJob* update_;
333 std::vector<AppCacheHost*> update_hosts_; 336 std::vector<AppCacheHost*> update_hosts_;
334 }; 337 };
335 338
336 // Helper factories to simulate redirected URL responses for tests. 339 // Helper factories to simulate redirected URL responses for tests.
337 class RedirectFactory : public net::URLRequestJobFactory::ProtocolHandler { 340 class RedirectFactory : public net::URLRequestJobFactory::ProtocolHandler {
338 public: 341 public:
339 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { 342 virtual net::URLRequestJob* MaybeCreateJob(
343 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
340 return new net::URLRequestTestJob( 344 return new net::URLRequestTestJob(
341 request, 345 request,
346 network_delegate,
342 net::URLRequestTestJob::test_redirect_headers(), 347 net::URLRequestTestJob::test_redirect_headers(),
343 net::URLRequestTestJob::test_data_1(), 348 net::URLRequestTestJob::test_data_1(),
344 true); 349 true);
345 } 350 }
346 }; 351 };
347 352
348 // Helper class to simulate a URL that returns retry or success. 353 // Helper class to simulate a URL that returns retry or success.
349 class RetryRequestTestJob : public net::URLRequestTestJob { 354 class RetryRequestTestJob : public net::URLRequestTestJob {
350 public: 355 public:
351 enum RetryHeader { 356 enum RetryHeader {
(...skipping 13 matching lines...) Expand all
365 expected_requests_ = expected_requests; 370 expected_requests_ = expected_requests;
366 } 371 }
367 372
368 // Verifies results at end of test and resets counters. 373 // Verifies results at end of test and resets counters.
369 static void Verify() { 374 static void Verify() {
370 EXPECT_EQ(expected_requests_, num_requests_); 375 EXPECT_EQ(expected_requests_, num_requests_);
371 num_requests_ = 0; 376 num_requests_ = 0;
372 expected_requests_ = 0; 377 expected_requests_ = 0;
373 } 378 }
374 379
375 static net::URLRequestJob* RetryFactory(net::URLRequest* request) { 380 static net::URLRequestJob* RetryFactory(
381 net::URLRequest* request, net::NetworkDelegate* network_delegate) {
376 ++num_requests_; 382 ++num_requests_;
377 if (num_retries_ > 0 && request->original_url() == kRetryUrl) { 383 if (num_retries_ > 0 && request->original_url() == kRetryUrl) {
378 --num_retries_; 384 --num_retries_;
379 return new RetryRequestTestJob( 385 return new RetryRequestTestJob(
380 request, RetryRequestTestJob::retry_headers(), 503); 386 request, network_delegate, RetryRequestTestJob::retry_headers(), 503);
381 } else { 387 } else {
382 return new RetryRequestTestJob( 388 return new RetryRequestTestJob(
383 request, RetryRequestTestJob::manifest_headers(), 200); 389 request,
390 network_delegate,
391 RetryRequestTestJob::manifest_headers(), 200);
384 } 392 }
385 } 393 }
386 394
387 virtual int GetResponseCode() const { return response_code_; } 395 virtual int GetResponseCode() const { return response_code_; }
388 396
389 private: 397 private:
390 ~RetryRequestTestJob() {} 398 ~RetryRequestTestJob() {}
391 399
392 static std::string retry_headers() { 400 static std::string retry_headers() {
393 const char no_retry_after[] = 401 const char no_retry_after[] =
(...skipping 26 matching lines...) Expand all
420 "\0"; 428 "\0";
421 return std::string(headers, arraysize(headers)); 429 return std::string(headers, arraysize(headers));
422 } 430 }
423 431
424 static std::string data() { 432 static std::string data() {
425 return std::string("CACHE MANIFEST\r" 433 return std::string("CACHE MANIFEST\r"
426 "http://retry\r"); // must be same as kRetryUrl 434 "http://retry\r"); // must be same as kRetryUrl
427 } 435 }
428 436
429 RetryRequestTestJob(net::URLRequest* request, 437 RetryRequestTestJob(net::URLRequest* request,
438 net::NetworkDelegate* network_delegate,
430 const std::string& headers, 439 const std::string& headers,
431 int response_code) 440 int response_code)
432 : net::URLRequestTestJob(request, headers, data(), true), 441 : net::URLRequestTestJob(
442 request, network_delegate, headers, data(), true),
433 response_code_(response_code) { 443 response_code_(response_code) {
434 } 444 }
435 445
436 int response_code_; 446 int response_code_;
437 447
438 static int num_requests_; 448 static int num_requests_;
439 static int num_retries_; 449 static int num_retries_;
440 static RetryHeader retry_after_; 450 static RetryHeader retry_after_;
441 static int expected_requests_; 451 static int expected_requests_;
442 }; 452 };
443 453
444 class RetryRequestTestJobFactory 454 class RetryRequestTestJobFactory
445 : public net::URLRequestJobFactory::ProtocolHandler { 455 : public net::URLRequestJobFactory::ProtocolHandler {
446 public: 456 public:
447 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { 457 virtual net::URLRequestJob* MaybeCreateJob(
448 return RetryRequestTestJob::RetryFactory(request); 458 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
459 return RetryRequestTestJob::RetryFactory(request, network_delegate);
449 } 460 }
450 }; 461 };
451 462
452 // static 463 // static
453 const GURL RetryRequestTestJob::kRetryUrl("http://retry"); 464 const GURL RetryRequestTestJob::kRetryUrl("http://retry");
454 int RetryRequestTestJob::num_requests_ = 0; 465 int RetryRequestTestJob::num_requests_ = 0;
455 int RetryRequestTestJob::num_retries_; 466 int RetryRequestTestJob::num_retries_;
456 RetryRequestTestJob::RetryHeader RetryRequestTestJob::retry_after_; 467 RetryRequestTestJob::RetryHeader RetryRequestTestJob::retry_after_;
457 int RetryRequestTestJob::expected_requests_ = 0; 468 int RetryRequestTestJob::expected_requests_ = 0;
458 469
(...skipping 15 matching lines...) Expand all
474 EXPECT_TRUE(saw_if_none_match_); 485 EXPECT_TRUE(saw_if_none_match_);
475 486
476 // Reset. 487 // Reset.
477 expect_if_modified_since_.clear(); 488 expect_if_modified_since_.clear();
478 saw_if_modified_since_ = false; 489 saw_if_modified_since_ = false;
479 expect_if_none_match_.clear(); 490 expect_if_none_match_.clear();
480 saw_if_none_match_ = false; 491 saw_if_none_match_ = false;
481 already_checked_ = false; 492 already_checked_ = false;
482 } 493 }
483 494
484 static net::URLRequestJob* IfModifiedSinceFactory(net::URLRequest* request) { 495 static net::URLRequestJob* IfModifiedSinceFactory(
496 net::URLRequest* request, net::NetworkDelegate* network_delegate) {
485 if (!already_checked_) { 497 if (!already_checked_) {
486 already_checked_ = true; // only check once for a test 498 already_checked_ = true; // only check once for a test
487 const net::HttpRequestHeaders& extra_headers = 499 const net::HttpRequestHeaders& extra_headers =
488 request->extra_request_headers(); 500 request->extra_request_headers();
489 std::string header_value; 501 std::string header_value;
490 saw_if_modified_since_ = 502 saw_if_modified_since_ =
491 extra_headers.GetHeader( 503 extra_headers.GetHeader(
492 net::HttpRequestHeaders::kIfModifiedSince, &header_value) && 504 net::HttpRequestHeaders::kIfModifiedSince, &header_value) &&
493 header_value == expect_if_modified_since_; 505 header_value == expect_if_modified_since_;
494 506
495 saw_if_none_match_ = 507 saw_if_none_match_ =
496 extra_headers.GetHeader( 508 extra_headers.GetHeader(
497 net::HttpRequestHeaders::kIfNoneMatch, &header_value) && 509 net::HttpRequestHeaders::kIfNoneMatch, &header_value) &&
498 header_value == expect_if_none_match_; 510 header_value == expect_if_none_match_;
499 } 511 }
500 return MockHttpServer::JobFactory(request); 512 return MockHttpServer::JobFactory(request, network_delegate);
501 } 513 }
502 514
503 protected: 515 protected:
504 virtual ~HttpHeadersRequestTestJob() {} 516 virtual ~HttpHeadersRequestTestJob() {}
505 517
506 private: 518 private:
507 static std::string expect_if_modified_since_; 519 static std::string expect_if_modified_since_;
508 static bool saw_if_modified_since_; 520 static bool saw_if_modified_since_;
509 static std::string expect_if_none_match_; 521 static std::string expect_if_none_match_;
510 static bool saw_if_none_match_; 522 static bool saw_if_none_match_;
511 static bool already_checked_; 523 static bool already_checked_;
512 }; 524 };
513 525
514 // static 526 // static
515 std::string HttpHeadersRequestTestJob::expect_if_modified_since_; 527 std::string HttpHeadersRequestTestJob::expect_if_modified_since_;
516 bool HttpHeadersRequestTestJob::saw_if_modified_since_ = false; 528 bool HttpHeadersRequestTestJob::saw_if_modified_since_ = false;
517 std::string HttpHeadersRequestTestJob::expect_if_none_match_; 529 std::string HttpHeadersRequestTestJob::expect_if_none_match_;
518 bool HttpHeadersRequestTestJob::saw_if_none_match_ = false; 530 bool HttpHeadersRequestTestJob::saw_if_none_match_ = false;
519 bool HttpHeadersRequestTestJob::already_checked_ = false; 531 bool HttpHeadersRequestTestJob::already_checked_ = false;
520 532
521 class IfModifiedSinceJobFactory 533 class IfModifiedSinceJobFactory
522 : public net::URLRequestJobFactory::ProtocolHandler { 534 : public net::URLRequestJobFactory::ProtocolHandler {
523 public: 535 public:
524 virtual net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const { 536 virtual net::URLRequestJob* MaybeCreateJob(
525 return HttpHeadersRequestTestJob::IfModifiedSinceFactory(request); 537 net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
538 return HttpHeadersRequestTestJob::IfModifiedSinceFactory(
539 request, network_delegate);
526 } 540 }
527 }; 541 };
528 542
529 class IOThread : public base::Thread { 543 class IOThread : public base::Thread {
530 public: 544 public:
531 explicit IOThread(const char* name) 545 explicit IOThread(const char* name)
532 : base::Thread(name) { 546 : base::Thread(name) {
533 } 547 }
534 548
535 ~IOThread() { 549 ~IOThread() {
(...skipping 3031 matching lines...) Expand 10 before | Expand all | Expand 10 after
3567 3581
3568 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) { 3582 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsSuccess) {
3569 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest); 3583 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsSuccessTest);
3570 } 3584 }
3571 3585
3572 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) { 3586 TEST_F(AppCacheUpdateJobTest, CrossOriginHttpsDenied) {
3573 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest); 3587 RunTestOnIOThread(&AppCacheUpdateJobTest::CrossOriginHttpsDeniedTest);
3574 } 3588 }
3575 3589
3576 } // namespace appcache 3590 } // namespace appcache
OLDNEW
« no previous file with comments | « webkit/appcache/appcache_request_handler_unittest.cc ('k') | webkit/appcache/appcache_url_request_job.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698