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

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: Address comments. Created 6 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 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..033c25969256ae6d490aee9ddb6ff0d6c491099d
--- /dev/null
+++ b/LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js
@@ -0,0 +1,233 @@
+importScripts('worker-test-harness.js');
+//
+// Variations:
+// - ignoreSearch : Ignores search parameter.
+// - prefixMatch : only matches a prefix of the URL.
+// - ignoreVary : ignores a 'Vary' header if there is one.
+var 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('')
+ },
+
+ c_is_for_cookie: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'is-for-cookie'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies'}})
+ },
+
+ c_is_for_cake: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'is-for-cake'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies'}})
+ },
+
+ c_x_key_is_1: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'x', 'X-Key': '1'}}),
+ response: new Response('',
+ {headers: {'Vary': '*'}})
+ },
+
+ c_y_key_is_1: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'y', 'X-Key': '1'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies,X-Key'}})
+ },
+
+ c_y_key_is_2: {
+ request: new Request('http://example.com/c',
+ {headers: {'Cookies': 'y', 'X-Key': '2'}}),
+ response: new Response('',
+ {headers: {'Vary': 'Cookies,X-Key'}})
+ }
+};
+
+promise_test(function(t) {
+ var cache;
+ return create_temporary_cache(t)
+ .then(function(result) {
+ cache = result;
+ return put_all_requests_in_cache(cache);
+ })
+
+ .then(function() {
+
+ promise_test(function() {
+ return cache.matchAll(entries.a.request.url)
+ .then(function(result) {
+ assert_array_equals(result, [entries.a.response],
+ 'Cache.matchAll should match by URL.');
+ });
+ }, 'Cache.matchAll with URL');
+
+ promise_test(function() {
+ return cache.matchAll(entries.a.request)
+ .then(function(result) {
+ assert_array_equals(result, [entries.a.response],
+ 'Cache.matchAll should match by ' +
+ 'Request.');
+ });
+ }, 'Cache.matchAll with Request');
+
+ promise_test(function() {
+ return cache.matchAll(new Request(entries.a.request.url))
+ .then(function(result) {
+ assert_array_equals(result, [entries.a.response],
+ 'Cache.matchAll should match by ' +
+ 'Request.');
+ });
+ }, 'Cache.matchAll with new Request');
+
+ promise_test(function() {
+ return cache.match(entries.a.request)
+ .then(function(result) {
+ assert_equals(result, entries.a.response,
+ 'Cache.match should match by Request.');
+ });
+ }, 'Cache.match with Request');
+
+ promise_test(function() {
+ return cache.matchAll(entries.a.request, { ignoreSearch: true })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ entries.a.response,
+ 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() {
+ return cache.matchAll(entries.a_with_query.request,
+ { ignoreSearch: true })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ entries.a.response,
+ 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() {
+ return cache.matchAll(entries.cat.request)
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ entries.cat.response,
+ entries.cat_with_fragment.response
+ ],
+ 'Cache.matchAll should ignore URL hash.');
+ });
+ }, 'Cache.matchAll with request containing hash');
+
+ promise_test(function() {
+ 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');
+
+ promise_test(function() {
+ return cache.matchAll('http://example.com/cat',
+ { prefixMatch: true })
+ .then(function(result) {
+ assert_array_equivalent(
+ result,
+ [
+ entries.cat.response,
+ entries.cat_with_fragment.response,
+ entries.cat_in_the_hat.response
+ ],
+ 'Cache.matchAll should honor prefixMatch.');
+ });
+ }, 'Cache.matchAll with prefixMatch option');
+
+ promise_test(function() {
+ return cache.matchAll('http://example.com/cat/',
+ { prefixMatch: true })
+ .then(function(result) {
+ assert_array_equivalent(
+ result, [entries.cat_in_the_hat.response],
+ 'Cache.matchAll should honor prefixMatch.');
+ });
+ }, 'Cache.matchAll with prefixMatch option');
+ });
+ }, 'Cache.match');
+
+// Helpers ---
+
+function put_all_requests_in_cache(cache) {
+ return Promise.all(Object.keys(entries).map(function(k) {
+ return cache.put(entries[k].request, entries[k].response);
+ }));
+}
+
+// Assert that the two arrays |actual| and |expected| contain the same set of
+// elements ignoring order. This is necessary because the result array returned
+// by Cache.matchAll() does not guarantee any specific ordering.
+//
+// |expected| is assumed to not contain any duplicates.
+function assert_array_equivalent(actual, expected, description) {
+ assert_true(Array.isArray(actual), description);
+ assert_equals(actual.length, expected.length, description);
+ expected.forEach(function(element) {
+ assert_in_array(element, actual, description);
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698