| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stack> | 5 #include <stack> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 13 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 15 #include "content/browser/appcache/appcache_response.h" | 17 #include "content/browser/appcache/appcache_response.h" |
| 16 #include "content/browser/appcache/appcache_url_request_job.h" | 18 #include "content/browser/appcache/appcache_url_request_job.h" |
| 17 #include "content/browser/appcache/mock_appcache_service.h" | 19 #include "content/browser/appcache/mock_appcache_service.h" |
| 18 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 20 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 21 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 22 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_error_job.h" | 26 #include "net/url_request/url_request_error_job.h" |
| 25 #include "net/url_request/url_request_job_factory.h" | 27 #include "net/url_request/url_request_job_factory.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "url/gurl.h" |
| 27 | 30 |
| 28 using net::IOBuffer; | 31 using net::IOBuffer; |
| 29 using net::WrappedIOBuffer; | 32 using net::WrappedIOBuffer; |
| 30 | 33 |
| 31 namespace content { | 34 namespace content { |
| 32 | 35 |
| 33 namespace { | 36 namespace { |
| 34 | 37 |
| 35 const char kHttpBasicHeaders[] = "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; | 38 const char kHttpBasicHeaders[] = "HTTP/1.0 200 OK\0Content-Length: 5\0\0"; |
| 36 const char kHttpBasicBody[] = "Hello"; | 39 const char kHttpBasicBody[] = "Hello"; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 81 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 79 return url.SchemeIs("http"); | 82 return url.SchemeIs("http"); |
| 80 } | 83 } |
| 81 | 84 |
| 82 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { | 85 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 83 return false; | 86 return false; |
| 84 } | 87 } |
| 85 | 88 |
| 86 private: | 89 private: |
| 87 mutable net::URLRequestJob* job_; | 90 mutable net::URLRequestJob* job_; |
| 88 }; | 91 }; |
| 89 | 92 |
| 90 class AppCacheURLRequestJobTest : public testing::Test { | 93 class AppCacheURLRequestJobTest : public testing::Test { |
| 91 public: | 94 public: |
| 92 | 95 |
| 93 // Test Harness ------------------------------------------------------------- | 96 // Test Harness ------------------------------------------------------------- |
| 94 // TODO(michaeln): share this test harness with AppCacheResponseTest | 97 // TODO(michaeln): share this test harness with AppCacheResponseTest |
| 95 | 98 |
| 96 class MockStorageDelegate : public AppCacheStorage::Delegate { | 99 class MockStorageDelegate : public AppCacheStorage::Delegate { |
| 97 public: | 100 public: |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 return true; | 412 return true; |
| 410 } | 413 } |
| 411 | 414 |
| 412 // Individual Tests --------------------------------------------------------- | 415 // Individual Tests --------------------------------------------------------- |
| 413 // Some of the individual tests involve multiple async steps. Each test | 416 // Some of the individual tests involve multiple async steps. Each test |
| 414 // is delineated with a section header. | 417 // is delineated with a section header. |
| 415 | 418 |
| 416 // Basic ------------------------------------------------------------------- | 419 // Basic ------------------------------------------------------------------- |
| 417 void Basic() { | 420 void Basic() { |
| 418 AppCacheStorage* storage = service_->storage(); | 421 AppCacheStorage* storage = service_->storage(); |
| 419 net::URLRequest request(GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, | 422 scoped_ptr<net::URLRequest> request(empty_context_->CreateRequest( |
| 420 empty_context_.get()); | 423 GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, NULL)); |
| 421 scoped_refptr<AppCacheURLRequestJob> job; | 424 scoped_refptr<AppCacheURLRequestJob> job; |
| 422 | 425 |
| 423 // Create an instance and see that it looks as expected. | 426 // Create an instance and see that it looks as expected. |
| 424 | 427 |
| 425 job = new AppCacheURLRequestJob( | 428 job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false); |
| 426 &request, NULL, storage, NULL, false); | |
| 427 EXPECT_TRUE(job->is_waiting()); | 429 EXPECT_TRUE(job->is_waiting()); |
| 428 EXPECT_FALSE(job->is_delivering_appcache_response()); | 430 EXPECT_FALSE(job->is_delivering_appcache_response()); |
| 429 EXPECT_FALSE(job->is_delivering_network_response()); | 431 EXPECT_FALSE(job->is_delivering_network_response()); |
| 430 EXPECT_FALSE(job->is_delivering_error_response()); | 432 EXPECT_FALSE(job->is_delivering_error_response()); |
| 431 EXPECT_FALSE(job->has_been_started()); | 433 EXPECT_FALSE(job->has_been_started()); |
| 432 EXPECT_FALSE(job->has_been_killed()); | 434 EXPECT_FALSE(job->has_been_killed()); |
| 433 EXPECT_EQ(GURL(), job->manifest_url()); | 435 EXPECT_EQ(GURL(), job->manifest_url()); |
| 434 EXPECT_EQ(kAppCacheNoCacheId, job->cache_id()); | 436 EXPECT_EQ(kAppCacheNoCacheId, job->cache_id()); |
| 435 EXPECT_FALSE(job->entry().has_response_id()); | 437 EXPECT_FALSE(job->entry().has_response_id()); |
| 436 | 438 |
| 437 TestFinished(); | 439 TestFinished(); |
| 438 } | 440 } |
| 439 | 441 |
| 440 // DeliveryOrders ----------------------------------------------------- | 442 // DeliveryOrders ----------------------------------------------------- |
| 441 void DeliveryOrders() { | 443 void DeliveryOrders() { |
| 442 AppCacheStorage* storage = service_->storage(); | 444 AppCacheStorage* storage = service_->storage(); |
| 443 net::URLRequest request(GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, | 445 scoped_ptr<net::URLRequest> request(empty_context_->CreateRequest( |
| 444 empty_context_.get()); | 446 GURL("http://blah/"), net::DEFAULT_PRIORITY, NULL, NULL)); |
| 445 scoped_refptr<AppCacheURLRequestJob> job; | 447 scoped_refptr<AppCacheURLRequestJob> job; |
| 446 | 448 |
| 447 // Create an instance, give it a delivery order and see that | 449 // Create an instance, give it a delivery order and see that |
| 448 // it looks as expected. | 450 // it looks as expected. |
| 449 | 451 |
| 450 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false); | 452 job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false); |
| 451 job->DeliverErrorResponse(); | 453 job->DeliverErrorResponse(); |
| 452 EXPECT_TRUE(job->is_delivering_error_response()); | 454 EXPECT_TRUE(job->is_delivering_error_response()); |
| 453 EXPECT_FALSE(job->has_been_started()); | 455 EXPECT_FALSE(job->has_been_started()); |
| 454 | 456 |
| 455 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false); | 457 job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false); |
| 456 job->DeliverNetworkResponse(); | 458 job->DeliverNetworkResponse(); |
| 457 EXPECT_TRUE(job->is_delivering_network_response()); | 459 EXPECT_TRUE(job->is_delivering_network_response()); |
| 458 EXPECT_FALSE(job->has_been_started()); | 460 EXPECT_FALSE(job->has_been_started()); |
| 459 | 461 |
| 460 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL, false); | 462 job = new AppCacheURLRequestJob(request.get(), NULL, storage, NULL, false); |
| 461 const GURL kManifestUrl("http://blah/"); | 463 const GURL kManifestUrl("http://blah/"); |
| 462 const int64 kCacheId(1); | 464 const int64 kCacheId(1); |
| 463 const int64 kGroupId(1); | 465 const int64 kGroupId(1); |
| 464 const AppCacheEntry kEntry(AppCacheEntry::EXPLICIT, 1); | 466 const AppCacheEntry kEntry(AppCacheEntry::EXPLICIT, 1); |
| 465 job->DeliverAppCachedResponse(kManifestUrl, kCacheId, kGroupId, | 467 job->DeliverAppCachedResponse(kManifestUrl, kCacheId, kGroupId, |
| 466 kEntry, false); | 468 kEntry, false); |
| 467 EXPECT_FALSE(job->is_waiting()); | 469 EXPECT_FALSE(job->is_waiting()); |
| 468 EXPECT_TRUE(job->is_delivering_appcache_response()); | 470 EXPECT_TRUE(job->is_delivering_appcache_response()); |
| 469 EXPECT_FALSE(job->has_been_started()); | 471 EXPECT_FALSE(job->has_been_started()); |
| 470 EXPECT_EQ(kManifestUrl, job->manifest_url()); | 472 EXPECT_EQ(kManifestUrl, job->manifest_url()); |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 854 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
| 853 } | 855 } |
| 854 | 856 |
| 855 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 857 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
| 856 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 858 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
| 857 } | 859 } |
| 858 | 860 |
| 859 } // namespace | 861 } // namespace |
| 860 | 862 |
| 861 } // namespace content | 863 } // namespace content |
| OLD | NEW |