From a25f82c91ffd81c56bc298d2ca2590bc2cd97e35 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 28 Jan 2019 14:42:36 -0800 Subject: [PATCH] fix: reject with error when url not loaded (#16571) * fix: reject with error when url not loaded * improve descriptive spec naming --- atom/browser/api/atom_api_web_contents.cc | 11 ++++++++--- spec/api-web-contents-spec.js | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 9604274db1f4..b157d3f8de2f 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1444,12 +1444,17 @@ v8::Local WebContents::HasServiceWorker() { auto* context = GetServiceWorkerContext(web_contents()); if (!context) { promise->RejectWithErrorMessage("Unable to get ServiceWorker context."); + return promise->GetHandle(); + } + + GURL url = web_contents()->GetLastCommittedURL(); + if (!url.is_valid()) { + promise->RejectWithErrorMessage("URL invalid or not yet loaded."); + return promise->GetHandle(); } context->CheckHasServiceWorker( - web_contents()->GetLastCommittedURL(), - web_contents()->GetLastCommittedURL(), - base::BindOnce(&OnServiceWorkerCheckDone, promise)); + url, url, base::BindOnce(&OnServiceWorkerCheckDone, promise)); return promise->GetHandle(); } diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index d5c60c9d82ef..139dc7678aaa 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -229,13 +229,18 @@ describe('webContents module', () => { }) describe('ServiceWorker APIs', () => { - it('can successfully register a ServiceWorker', async () => { + it('can successfully check for presence of a ServiceWorker', async () => { await w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html')) const hasSW = await w.webContents.hasServiceWorker() expect(hasSW).to.be.true() }) - it('can successfully register a ServiceWorker (callback)', (done) => { + it('throws properly for invalid url', async () => { + const promise = w.webContents.hasServiceWorker() + return expect(promise).to.be.eventually.rejectedWith(Error, 'URL invalid or not yet loaded.') + }) + + it('can successfully check for presence of a ServiceWorker (callback)', (done) => { w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html')).then(() => { w.webContents.hasServiceWorker(hasSW => { expect(hasSW).to.be.true()