Commit 5b513b0c authored by falken@chromium.org's avatar falken@chromium.org

Service Worker: Add a test for calling respondWith() asynchronously.

According to the spec discussion, it should throw an exception:
https://github.com/slightlyoff/ServiceWorker/issues/51#issuecomment-51172079

Existing code happens to handle this case; tweak the error message to make
sense for it.

BUG=410699

Review URL: https://codereview.chromium.org/723063002

git-svn-id: svn://svn.chromium.org/blink/trunk@185280 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent b22cdec1
<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharness-helpers.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
promise_test(function(t) {
var script = 'resources/fetch-event-async-respond-with-worker.js';
var scope = 'resources/simple.html';
return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
return wait_for_activated(t, registration);
})
.then(function() {
return with_iframe(scope);
})
.then(function(frame) {
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = function(e) { resolve(e.data); }
});
var worker = frame.contentWindow.navigator.serviceWorker.controller;
frame.remove();
worker.postMessage({port: channel.port2}, [channel.port2]);
return saw_message;
})
.then(function(message) {
assert_equals(message, 'PASS');
return service_worker_unregister_and_done(t, scope);
})
}, 'Calling respondWith asynchronously throws an exception');
</script>
var result;
self.addEventListener('message', function(event) {
event.data.port.postMessage(result);
});
self.addEventListener('fetch', function(event) {
setTimeout(function() {
try {
event.respondWith(new Response());
result = 'FAIL: did not throw';
} catch (error) {
if (error.name == 'InvalidStateError')
result = 'PASS';
else
result = 'FAIL: Unexpected exception: ' + error;
}
}, 0);
});
...@@ -87,7 +87,7 @@ void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu ...@@ -87,7 +87,7 @@ void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu
{ {
ASSERT(RuntimeEnabledFeatures::serviceWorkerOnFetchEnabled()); ASSERT(RuntimeEnabledFeatures::serviceWorkerOnFetchEnabled());
if (m_state != Initial) { if (m_state != Initial) {
exceptionState.throwDOMException(InvalidStateError, "respondWith is already called."); exceptionState.throwDOMException(InvalidStateError, "The fetch event has already been responded to.");
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment