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

Unified Diff: content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc

Issue 1283273002: Service Worker: Change last update check location and HTTP cache bypass rule (2/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use |request->response_info().network_accessed| to check network access. Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/service_worker/service_worker_write_to_cache_job.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc
diff --git a/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc b/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc
index 18115d9b8aa24b82b95b028b1dc49c1ed2ab22a0..cc7d549362998d8d1edf369dde7ecb01c21f3a9f 100644
--- a/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc
+++ b/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc
@@ -60,14 +60,45 @@ net::URLRequestJob* CreateNormalURLRequestJob(
true);
}
-net::URLRequestJob* CreateResponseJob(const std::string& response_data,
- net::URLRequest* request,
- net::NetworkDelegate* network_delegate) {
+net::URLRequestJob* CreateCachedResponseJob(
+ const std::string& response_data,
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) {
return new net::URLRequestTestJob(request, network_delegate,
std::string(kHeaders, arraysize(kHeaders)),
response_data, true);
}
+class NetworkResponseJob : public net::URLRequestTestJob {
+ public:
+ NetworkResponseJob(net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const std::string& response_headers,
+ const std::string& response_data,
+ bool auto_advance)
+ : net::URLRequestTestJob(request,
+ network_delegate,
+ response_headers,
+ response_data,
+ auto_advance) {}
+ void GetResponseInfo(net::HttpResponseInfo* info) override {
+ URLRequestTestJob::GetResponseInfo(info);
+ info->network_accessed = true;
+ }
+
+ protected:
+ ~NetworkResponseJob() override {}
+};
+
+net::URLRequestJob* CreateNetworkResponseJob(
+ const std::string& response_data,
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) {
+ return new NetworkResponseJob(request, network_delegate,
+ std::string(kHeaders, arraysize(kHeaders)),
+ response_data, true);
+}
+
net::URLRequestJob* CreateInvalidMimeTypeJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
@@ -84,6 +115,15 @@ net::URLRequestJob* CreateInvalidMimeTypeJob(
true);
}
+net::URLRequestJob* CreateErrorReponseJob(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) {
+ const char kErrorHeaders[] = "HTTP/1.1 404 Not Found\0\0";
+ return new net::URLRequestTestJob(
+ request, network_delegate,
+ std::string(kErrorHeaders, arraysize(kErrorHeaders)), kScriptCode, true);
+}
+
class SSLCertificateErrorJob : public net::URLRequestTestJob {
public:
SSLCertificateErrorJob(net::URLRequest* request,
@@ -333,7 +373,7 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test {
int CreateIncumbent(const std::string& response) {
mock_protocol_handler_->SetCreateJobCallback(
- base::Bind(&CreateResponseJob, response));
+ base::Bind(&CreateNetworkResponseJob, response));
request_->Start();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request_->status().status());
@@ -360,7 +400,9 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test {
// Performs the net request for an update of |registration_|'s incumbent
// to the script |response|. Returns the new version.
scoped_refptr<ServiceWorkerVersion> UpdateScript(
- const std::string& response) {
+ const std::string& response,
+ bool status_success = true,
+ bool network_accessed = true) {
nhiroki 2015/09/09 08:27:48 nit: Passing multiple booleans could be error-pron
jungkees 2015/09/09 13:13:37 Agreed. I did it with the passing a callback optio
int render_process_id = NextRenderProcessId();
int provider_id = NextProviderId();
scoped_refptr<ServiceWorkerVersion> new_version =
@@ -369,8 +411,20 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test {
CreateHostForVersion(render_process_id, provider_id, new_version);
SetUpScriptRequest(render_process_id, provider_id);
- mock_protocol_handler_->SetCreateJobCallback(
- base::Bind(&CreateResponseJob, response));
+ if (status_success) {
+ // Whether the script load request accessed the network or was served from
+ // HTTPCache.
+ if (network_accessed) {
+ mock_protocol_handler_->SetCreateJobCallback(
+ base::Bind(&CreateNetworkResponseJob, response));
+ } else {
+ mock_protocol_handler_->SetCreateJobCallback(
+ base::Bind(&CreateCachedResponseJob, response));
+ }
+ } else {
+ mock_protocol_handler_->SetCreateJobCallback(
+ base::Bind(&CreateErrorReponseJob));
+ }
request_->Start();
base::RunLoop().RunUntilIdle();
return new_version;
@@ -573,4 +627,35 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, Update_EmptyScript) {
EXPECT_EQ(kInvalidServiceWorkerResponseId, GetResourceId(version.get()));
}
+TEST_F(ServiceWorkerWriteToCacheJobTest, Update_BumpLastUpdateCheckTime) {
+ std::string response = GenerateLongResponse();
+ CreateIncumbent(response);
+
+ // Script was served from HTTPCache.
+ base::Time current = base::Time::Now();
+ registration_->set_last_update_check(current);
+ scoped_refptr<ServiceWorkerVersion> version = UpdateScript(
+ response, true /* status_success */, false /* network_accessed */);
+ EXPECT_EQ(current, registration_->last_update_check());
+
+ // Same script but the request accessed the network.
+ version = UpdateScript(response);
+ EXPECT_LT(current, registration_->last_update_check());
+
+ // Changed the first byte and the request accessed the network.
+ response[0] = 'x';
+ current = base::Time::Now();
+ registration_->set_last_update_check(current);
+ version = UpdateScript(response);
+ VerifyResource(GetResourceId(version.get()), response);
+ registration_->SetWaitingVersion(version);
+ EXPECT_LT(current, registration_->last_update_check());
+
+ // The request accessed the network but received a non-2xx error.
+ current = base::Time::Now();
+ registration_->set_last_update_check(current);
+ version = UpdateScript(response, false /* status_success */);
+ EXPECT_EQ(current, registration_->last_update_check());
+}
+
} // namespace content
« no previous file with comments | « content/browser/service_worker/service_worker_write_to_cache_job.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698