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

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

Powered by Google App Engine
This is Rietveld 408576698