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

Unified Diff: LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js

Issue 425413002: [ServiceWorker] Tests for Cache (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: CacheStorage.{create => open} Created 6 years, 2 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
Index: LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js
diff --git a/LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js b/LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js
new file mode 100644
index 0000000000000000000000000000000000000000..6e64a79a61183f797336206b636787fa34b78d48
--- /dev/null
+++ b/LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js
@@ -0,0 +1,363 @@
+importScripts('worker-testharness.js');
+importScripts('/resources/testharness-helpers.js');
+
+//
+// Variations:
+// - ignoreSearch : Ignores search parameter.
+// - prefixMatch : only matches a prefix of the URL.
+// - ignoreVary : ignores a 'Vary' header if there is one.
+var simple_entries = {
+ a: {
+ request: new Request('http://example.com/a'),
+ response: new Response('')
+ },
+
+ b: {
+ request: new Request('http://example.com/b'),
+ response: new Response('')
+ },
+
+ a_with_query: {
+ request: new Request('http://example.com/a?q=r'),
+ response: new Response('')
+ },
+
+ A: {
+ request: new Request('http://example.com/A'),
+ response: new Response('')
+ },
+
+ a_https: {
+ request: new Request('https://example.com/a'),
+ response: new Response('')
+ },
+
+ a_org: {
+ request: new Request('http://example.org/a'),
+ response: new Response('')
+ },
+
+ cat: {
+ request: new Request('http://example.com/cat'),
+ response: new Response('')
+ },
+
+ cat_with_fragment: {
+ request: new Request('http://example.com/cat#mouse'),
+ response: new Response('')
+ },
+
+ cat_in_the_hat: {
+ request: new Request('http://example.com/cat/in/the/hat'),
+ response: new Response('')
+ }
+};
+
+var vary_entries = {
+ no_vary_header: {
+ request: new Request('http://example.com/c'),
+ response: new Response('')
+ },
+
+ vary_cookie_is_cookie: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'is-for-cookie'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies'}})
+ },
+
+ vary_cookie_is_good: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'is-good-enough-for-me'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies'}})
+ },
+
+ vary_cookie_absent: {
+ request: new Request('http://example.com/c'),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies'}})
+ },
+
+ vary_wildcard: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'x', 'X-Key': '1'}}),
+ response: new Response('',
+ {headers: {'Vary': '*'}})
+ }
+};
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(simple_entries.a.request.url);
+ })
+ .then(function(result) {
+ assert_array_objects_equals(result, [simple_entries.a.response],
+ 'Cache.matchAll should match by URL.');
+ });
+ }, 'Cache.matchAll with URL');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.match(simple_entries.a.request.url);
+ })
+ .then(function(result) {
+ assert_object_equals(result, simple_entries.a.response,
+ 'Cache.match should match by URL.');
+ });
+ }, 'Cache.match with URL');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(simple_entries.a.request);
+ })
+ .then(function(result) {
+ assert_array_objects_equals(
+ result, [simple_entries.a.response],
+ 'Cache.matchAll should match by Request.');
+ });
+ }, 'Cache.matchAll with Request');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.match(simple_entries.a.request);
+ })
+ .then(function(result) {
+ assert_object_equals(result, simple_entries.a.response,
+ 'Cache.match should match by Request.');
+ });
+ }, 'Cache.match with Request');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(new Request(simple_entries.a.request.url));
+ })
+ .then(function(result) {
+ assert_array_objects_equals(
+ result, [simple_entries.a.response],
+ 'Cache.matchAll should match by Request.');
+ });
+ }, 'Cache.matchAll with new Request');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.match(new Request(simple_entries.a.request.url));
+ })
+ .then(function(result) {
+ assert_object_equals(result, simple_entries.a.response,
+ 'Cache.match should match by Request.');
+ });
+ }, 'Cache.match with new Request');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(simple_entries.a.request,
+ {ignoreSearch: true});
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ simple_entries.a.response,
+ simple_entries.a_with_query.response
+ ],
+ 'Cache.matchAll with ignoreSearch should ignore the ' +
+ 'search parameters of cached request.');
+ });
+ },
+ 'Cache.matchAll with ignoreSearch option (request with no search ' +
+ 'parameters)');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(simple_entries.a_with_query.request,
+ {ignoreSearch: true});
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ simple_entries.a.response,
+ simple_entries.a_with_query.response
+ ],
+ 'Cache.matchAll with ignoreSearch should ignore the ' +
+ 'search parameters of request.');
+ });
+ },
+ 'Cache.matchAll with ignoreSearch option (request with search parameter)');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll(simple_entries.cat.request);
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ simple_entries.cat.response,
+ simple_entries.cat_with_fragment.response
+ ],
+ 'Cache.matchAll should ignore URL hash.');
+ });
+ }, 'Cache.matchAll with request containing hash');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll('http');
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result, [],
+ 'Cache.matchAll should treat query as a URL and not ' +
+ 'just a string fragment.');
+ });
+ }, 'Cache.matchAll with string fragment \'http\' as query');
jsbell 2014/10/20 18:20:41 Fine as-is, but also feel free to use " here to av
asanka 2014/10/22 22:35:31 Done.
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll('http://example.com/cat',
+ {prefixMatch: true});
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ simple_entries.cat.response,
+ simple_entries.cat_with_fragment.response,
+ simple_entries.cat_in_the_hat.response
+ ],
+ 'Cache.matchAll should honor prefixMatch.');
+ });
+ }, 'Cache.matchAll with prefixMatch option');
+
+promise_test(function(t) {
+ return create_populated_cache(t, simple_entries)
+ .then(function(cache) {
+ return cache.matchAll('http://example.com/cat/',
+ {prefixMatch: true});
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result, [simple_entries.cat_in_the_hat.response],
+ 'Cache.matchAll should honor prefixMatch.');
+ });
+ }, 'Cache.matchAll with prefixMatch option');
+
+promise_test(function(t) {
+ var cache;
+ return create_populated_cache(t, vary_entries)
+ .then(function(populated_cache) {
+ cache = populated_cache;
+ return cache.matchAll('http://example.com/c');
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ vary_entries.no_vary_header.response,
+ vary_entries.vary_wildcard.response,
+ vary_entries.vary_cookie_absent.response
+ ],
+ 'Cache.matchAll should exclude matches if a vary header is ' +
+ 'missing in the query request, but is present in the cached ' +
+ 'request.');
+ })
+
+ .then(function() {
+ return cache.matchAll(
+ new Request('http://example.com/c',
+ {headers: {'Cookies': 'none-of-the-above'}}));
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ vary_entries.no_vary_header.response,
+ vary_entries.vary_wildcard.response
+ ],
+ 'Cache.matchAll should exclude matches if a vary header is ' +
+ 'missing in the cached request, but is present in the query ' +
+ 'request.');
+ })
+
+ .then(function() {
+ return cache.matchAll(
+ new Request('http://example.com/c',
+ {headers: {'Cookies': 'is-for-cookie'}}));
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [vary_entries.vary_cookie_is_cookie.response],
+ 'Cache.matchAll should match the entire header if a vary header ' +
+ 'is present in both the query and cached requests.');
+ });
+ }, 'Cache.matchAll with responses containing \'Vary\' header');
+
+promise_test(function(t) {
+ return create_populated_cache(t, vary_entries)
+ .then(function(cache) {
+ return cache.match('http://example.com/c');
+ })
+ .then(function(result) {
+ assert_object_in_array(
+ result,
+ [
+ vary_entries.no_vary_header.response,
+ vary_entries.vary_wildcard.response,
+ vary_entries.vary_cookie_absent.response
+ ],
+ 'Cache.match should honor \'Vary\' header.');
+ });
+ }, 'Cache.match with responses containing \'Vary\' header');
+
+promise_test(function(t) {
+ return create_populated_cache(t, vary_entries)
+ .then(function(cache) {
+ return cache.matchAll('http://example.com/c',
+ {ignoreVary: true});
+ })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ vary_entries.no_vary_header.response,
+ vary_entries.vary_cookie_is_cookie.response,
+ vary_entries.vary_cookie_is_good.response,
+ vary_entries.vary_cookie_absent.response,
+ vary_entries.vary_wildcard.response
+ ],
+ 'Cache.matchAll should honor \'ignoreVary\' parameter.');
+ });
+ }, 'Cache.matchAll with \'ignoreVary\' parameter');
+
+// Helpers ---
+
+// Create a new Cache and populate it with requests from |entries|. The latter
+// is expected to be an Object mapping arbitrary keys to objects of the form
+// {request: <Request object>, response: <Response object>}.
+//
+// The return value is a Promise that resolves to the populated Cache. The
+// lifetime of the Cache is bound to the duration of |test|.
+function create_populated_cache(test, entries) {
+ return create_temporary_cache(test)
+ .then(function(cache) {
+ return Promise.all(Object.keys(entries).map(function(k) {
+ return cache.put(entries[k].request, entries[k].response);
+ }))
+ .then(function() {
+ return Promise.resolve(cache);
jsbell 2014/10/20 18:20:41 You should just be able to return `cache` here.
asanka 2014/10/22 22:35:32 Done.
+ });
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698