Index: content/browser/cache_storage/cache_storage_cache_unittest.cc |
diff --git a/content/browser/cache_storage/cache_storage_cache_unittest.cc b/content/browser/cache_storage/cache_storage_cache_unittest.cc |
index ec4208b2a1d4c0977b0d83a1269dab89d5912977..3dfa51973ac38740cd9d0c19de69dbb26e77d08e 100644 |
--- a/content/browser/cache_storage/cache_storage_cache_unittest.cc |
+++ b/content/browser/cache_storage/cache_storage_cache_unittest.cc |
@@ -122,6 +122,34 @@ class DelayableBackend : public disk_cache::Backend { |
base::Closure open_entry_callback_; |
}; |
+bool ResponsesEqual(const ServiceWorkerResponse& expected, |
+ const ServiceWorkerResponse& actual) { |
+ EXPECT_EQ(expected.status_code, actual.status_code); |
+ if (expected.status_code != actual.status_code) |
+ return false; |
+ EXPECT_EQ(expected.status_text, actual.status_text); |
+ if (expected.status_text != actual.status_text) |
+ return false; |
+ EXPECT_EQ(expected.url, actual.url); |
+ if (expected.url != actual.url) |
+ return false; |
+ EXPECT_EQ(expected.blob_size, actual.blob_size); |
+ if (expected.blob_size != actual.blob_size) |
+ return false; |
+ |
+ if (expected.blob_size == 0) { |
+ EXPECT_STREQ("", actual.blob_uuid.c_str()); |
+ if (!actual.blob_uuid.empty()) |
+ return false; |
+ } else { |
+ EXPECT_STRNE("", actual.blob_uuid.c_str()); |
+ if (actual.blob_uuid.empty()) |
jkarlin
2015/08/05 12:09:05
Can you compare the blob contents of the two respo
nhiroki
2015/08/06 03:36:53
Calling CopyBody() here might complicate this func
|
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
} // namespace |
// A CacheStorageCache that can optionally delay during backend creation. |
@@ -295,6 +323,16 @@ class CacheStorageCacheTest : public testing::Test { |
return callback_error_ == CACHE_STORAGE_OK; |
} |
+ bool MatchAll(std::vector<ServiceWorkerResponse>* responses, |
+ ScopedVector<storage::BlobDataHandle>* body_handles) { |
+ base::RunLoop loop; |
+ cache_->MatchAll(base::Bind( |
+ &CacheStorageCacheTest::ResponsesAndErrorCallback, |
+ base::Unretained(this), loop.QuitClosure(), responses, body_handles)); |
+ loop.Run(); |
+ return callback_error_ == CACHE_STORAGE_OK; |
+ } |
+ |
bool Delete(const ServiceWorkerFetchRequest& request) { |
CacheStorageBatchOperation operation; |
operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; |
@@ -370,6 +408,19 @@ class CacheStorageCacheTest : public testing::Test { |
run_loop->Quit(); |
} |
+ void ResponsesAndErrorCallback( |
+ const base::Closure& quit_closure, |
+ std::vector<ServiceWorkerResponse>* responses_out, |
+ ScopedVector<storage::BlobDataHandle>* body_handles_out, |
+ CacheStorageError error, |
+ const std::vector<ServiceWorkerResponse>& responses, |
+ ScopedVector<storage::BlobDataHandle> body_handles) { |
+ callback_error_ = error; |
+ *responses_out = responses; |
+ body_handles_out->swap(body_handles); |
+ quit_closure.Run(); |
+ } |
+ |
void CloseCallback(base::RunLoop* run_loop) { |
EXPECT_FALSE(callback_closed_); |
callback_closed_ = true; |
@@ -620,6 +671,81 @@ TEST_P(CacheStorageCacheTestP, MatchBody) { |
EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
} |
+TEST_P(CacheStorageCacheTestP, MatchAll_Empty) { |
+ std::vector<ServiceWorkerResponse> responses; |
+ ScopedVector<storage::BlobDataHandle> body_handles; |
+ EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
+ EXPECT_TRUE(responses.empty()); |
+ EXPECT_TRUE(body_handles.empty()); |
+} |
+ |
+TEST_P(CacheStorageCacheTestP, MatchAll_NoBody) { |
+ EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
+ |
+ std::vector<ServiceWorkerResponse> responses; |
+ ScopedVector<storage::BlobDataHandle> body_handles; |
+ EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
+ ASSERT_EQ(1u, responses.size()); |
+ ASSERT_EQ(1u, body_handles.size()); |
+ |
+ EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[0])); |
+ EXPECT_FALSE(body_handles[0]); |
+} |
+ |
+TEST_P(CacheStorageCacheTestP, MatchAll_Body) { |
+ EXPECT_TRUE(Put(body_request_, body_response_)); |
+ |
+ std::vector<ServiceWorkerResponse> responses; |
+ ScopedVector<storage::BlobDataHandle> body_handles; |
+ EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
+ ASSERT_EQ(1u, responses.size()); |
+ ASSERT_EQ(1u, body_handles.size()); |
+ |
+ EXPECT_TRUE(ResponsesEqual(body_response_, responses[0])); |
+ std::string response_body; |
+ CopyBody(body_handles[0], &response_body); |
+ EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
+} |
+ |
+TEST_P(CacheStorageCacheTestP, MatchAll_TwoResponsesThenOne) { |
+ EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
+ EXPECT_TRUE(Put(body_request_, body_response_)); |
+ |
+ std::vector<ServiceWorkerResponse> responses; |
+ ScopedVector<storage::BlobDataHandle> body_handles; |
+ EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
+ ASSERT_EQ(2u, responses.size()); |
+ ASSERT_EQ(2u, body_handles.size()); |
+ |
+ // Order of returned responses is not guaranteed. |
+ std::set<std::string> matched_set; |
+ for (size_t i = 0; i < responses.size(); ++i) { |
+ if (responses[i].url.spec() == "http://example.com/no_body.html") { |
+ EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[i])); |
+ EXPECT_FALSE(body_handles[i]); |
+ matched_set.insert(responses[i].url.spec()); |
+ } else if (responses[i].url.spec() == "http://example.com/body.html") { |
+ EXPECT_TRUE(ResponsesEqual(body_response_, responses[i])); |
+ std::string response_body; |
+ CopyBody(body_handles[i], &response_body); |
+ EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
+ matched_set.insert(responses[i].url.spec()); |
+ } |
+ } |
+ EXPECT_EQ(2u, matched_set.size()); |
+ |
+ responses.clear(); |
+ body_handles.clear(); |
+ |
+ EXPECT_TRUE(Delete(body_request_)); |
+ EXPECT_TRUE(MatchAll(&responses, &body_handles)); |
+ |
+ ASSERT_EQ(1u, responses.size()); |
+ ASSERT_EQ(1u, body_handles.size()); |
+ EXPECT_TRUE(ResponsesEqual(no_body_response_, responses[0])); |
+ EXPECT_FALSE(body_handles[0]); |
+} |
+ |
TEST_P(CacheStorageCacheTestP, Vary) { |
body_request_.headers["vary_foo"] = "foo"; |
body_response_.headers["vary"] = "vary_foo"; |