From 43be510baf5da3267c93512abfd4a6699531611b Mon Sep 17 00:00:00 2001 From: Nikhil Marathe Date: Mon, 31 Aug 2015 18:56:21 -0700 Subject: [PATCH] Bug 1180737 - Add update-test.py and update test to latest version. r=bkelly. Added update-test.py which is a conversion of Blink update-test.php to deliver workers with different mime types. Updated test file to latest version to check for SecurityError. Dealt with the usual waiting/active issue. Update web-platform-tests expected data --HG-- extra : commitid : 9XzorHqlWDi extra : rebase_source : a02d66beb08f4eee83f27c21a6fee1178aeea71e --- .../service-worker/update.https.html.ini | 5 -- .../service-worker/resources/update-worker.py | 34 +++++++++++ .../service-worker/update.https.html | 61 ++++++++++++------- 3 files changed, 73 insertions(+), 27 deletions(-) delete mode 100644 testing/web-platform/mozilla/meta/service-workers/service-worker/update.https.html.ini create mode 100644 testing/web-platform/mozilla/tests/service-workers/service-worker/resources/update-worker.py diff --git a/testing/web-platform/mozilla/meta/service-workers/service-worker/update.https.html.ini b/testing/web-platform/mozilla/meta/service-workers/service-worker/update.https.html.ini deleted file mode 100644 index 0eb2278a5553..000000000000 --- a/testing/web-platform/mozilla/meta/service-workers/service-worker/update.https.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[update.https.html] - type: testharness - [Update a registration] - expected: FAIL - diff --git a/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/update-worker.py b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/update-worker.py new file mode 100644 index 000000000000..225092f2fab2 --- /dev/null +++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/update-worker.py @@ -0,0 +1,34 @@ +import time + +def main(request, response): + # Set mode to 'init' for initial fetch. + mode = 'init' + if 'mode' in request.cookies: + mode = request.cookies['mode'].value + + # no-cache itself to ensure the user agent finds a new version for each update. + headers = [('Cache-Control', 'no-cache, must-revalidate'), + ('Pragma', 'no-cache')] + + content_type = '' + + if mode == 'init': + # Set a normal mimetype. + # Set cookie value to 'normal' so the next fetch will work in 'normal' mode. + content_type = 'application/javascript' + response.set_cookie('mode', 'normal') + elif mode == 'normal': + # Set a normal mimetype. + # Set cookie value to 'error' so the next fetch will work in 'error' mode. + content_type = 'application/javascript' + response.set_cookie('mode', 'error'); + elif mode == 'error': + # Set a disallowed mimetype. + # Unset and delete cookie to clean up the test setting. + content_type = 'text/html' + response.delete_cookie('mode') + + headers.append(('Content-Type', content_type)) + # Return a different script for each access. + return headers, '// %s' % (time.time()) + diff --git a/testing/web-platform/mozilla/tests/service-workers/service-worker/update.https.html b/testing/web-platform/mozilla/tests/service-workers/service-worker/update.https.html index 4831bff3acd2..40fa6cda177d 100644 --- a/testing/web-platform/mozilla/tests/service-workers/service-worker/update.https.html +++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/update.https.html @@ -9,7 +9,6 @@ promise_test(function(t) { var worker_url = 'resources/update-worker.py'; var expected_url = normalizeURL(worker_url); var registration; - return service_worker_unregister_and_register(t, worker_url, scope) .then(function(r) { registration = r; @@ -17,42 +16,60 @@ promise_test(function(t) { }) .then(function() { assert_equals(registration.installing, null, - 'installing should be null in the initial state'); + 'installing should be null in the initial state.'); assert_equals(registration.waiting, null, - 'waiting should be null in the initial state'); + 'waiting should be null in the initial state.'); assert_equals(registration.active.scriptURL, expected_url, - 'active should exist in the initial state'); - - // A new worker should be found. - registration.update(); - return wait_for_update(t, registration); + 'active should exist in the initial state.'); + // A new worker (generated by update-worker.py) should be found. + // The returned promise should resolve when a new worker script is + // fetched and starts installing. + return Promise.all([registration.update(), + wait_for_update(t, registration)]); }) .then(function() { assert_equals(registration.installing.scriptURL, expected_url, - 'new installing should be set after updatefound'); + 'new installing should be set after update resolves.'); assert_equals(registration.waiting, null, - 'waiting should still be null after updatefound'); + 'waiting should still be null after update resolves.'); assert_equals(registration.active.scriptURL, expected_url, - 'active should still exist after update found'); + 'active should still exist after update found.'); return wait_for_state(t, registration.installing, 'installed'); }) .then(function() { assert_equals(registration.installing, null, - 'installing should be null after installing'); - assert_equals(registration.waiting.scriptURL, expected_url, - 'waiting should be set after installing'); - assert_equals(registration.active.scriptURL, expected_url, - 'active should still exist after installing'); - return wait_for_state(t, registration.waiting, 'activated'); + 'installing should be null after installing.'); + if (registration.waiting) { + assert_equals(registration.waiting.scriptURL, expected_url, + 'waiting should be set after installing.'); + assert_equals(registration.active.scriptURL, expected_url, + 'active should still exist after installing.'); + return wait_for_state(t, registration.waiting, 'activated'); + } }) .then(function() { assert_equals(registration.installing, null, - 'installing should be null after activated'); + 'installing should be null after activated.'); assert_equals(registration.waiting, null, - 'waiting should be null after activated'); + 'waiting should be null after activated.'); assert_equals(registration.active.scriptURL, expected_url, - 'new worker should be promoted to active'); - return service_worker_unregister_and_done(t, scope); + 'new worker should be promoted to active.'); }) - }, 'Update a registration'); + .then(function() { + // A new worker(generated by update-worker.py) should be found. + // The returned promise should reject as update-worker.py sets the + // mimetype to a disallowed value for this attempt. + return registration.update(); + }) + .then( + function() { assert_unreached("update() should reject."); }, + function(e) { + assert_throws('SecurityError', function() { throw e; }, + 'Using a disallowed mimetype should make update() ' + + 'promise reject with a SecurityError.'); + assert_equals(registration.active.scriptURL, expected_url, + 'active should still exist after update failure.'); + return service_worker_unregister_and_done(t, scope); + }); + }, 'Update a registration.');