From 3c88447be676b8b2de599d6750ba8071c28a6fdb Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 3 May 2017 16:48:02 +0530 Subject: [PATCH 1/3] protocol: provide default response code for custom request jobs --- atom/browser/net/js_asker.h | 3 ++ atom/browser/net/url_request_buffer_job.cc | 2 +- spec/chromium-spec.js | 42 ++++++++++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/atom/browser/net/js_asker.h b/atom/browser/net/js_asker.h index df64a2175049..ebe53d982f7d 100644 --- a/atom/browser/net/js_asker.h +++ b/atom/browser/net/js_asker.h @@ -82,6 +82,9 @@ class JsAsker : public RequestJob { base::Bind(&JsAsker::OnResponse, weak_factory_.GetWeakPtr()))); } + + int GetResponseCode() const override { return 200; } + void GetResponseInfo(net::HttpResponseInfo* info) override { info->headers = new net::HttpResponseHeaders(""); } diff --git a/atom/browser/net/url_request_buffer_job.cc b/atom/browser/net/url_request_buffer_job.cc index fa848862bb28..117ba96ff1a4 100644 --- a/atom/browser/net/url_request_buffer_job.cc +++ b/atom/browser/net/url_request_buffer_job.cc @@ -67,7 +67,7 @@ void URLRequestBufferJob::StartAsync(std::unique_ptr options) { } void URLRequestBufferJob::GetResponseInfo(net::HttpResponseInfo* info) { - std::string status("HTTP/1.1 "); + std::string status("HTTP/1.1 200 OK"); status.append(base::IntToString(status_code_)); status.append(" "); status.append(net::GetHttpReasonPhrase(status_code_)); diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 1302fbbc4331..b2dd89a0e5ac 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -149,8 +149,6 @@ describe('chromium feature', function () { }) describe('navigator.serviceWorker', function () { - var url = 'file://' + fixtures + '/pages/service-worker/index.html' - it('should register for file scheme', function (done) { w = new BrowserWindow({ show: false @@ -169,7 +167,45 @@ describe('chromium feature', function () { }) } }) - w.loadURL(url) + w.loadURL(`file://${fixtures}/pages/service-worker/index.html`) + }) + + it('should register for intercepted file scheme', function (done) { + const customSession = session.fromPartition('intercept-file') + customSession.protocol.interceptBufferProtocol('file', function (request, callback) { + const file = url.parse(request.url).pathname + const content = fs.readFileSync(path.normalize(file)) + const ext = path.extname(file) + let type = 'text/html' + if (ext === '.js') { + type = 'application/javascript' + } + callback({data: content, mimeType: type}) + }, function (error) { + if (error) done(error) + }) + + w = new BrowserWindow({ + show: false, + webPreferences: { + session: customSession + } + }) + w.webContents.on('ipc-message', function (event, args) { + if (args[0] === 'reload') { + w.webContents.reload() + } else if (args[0] === 'error') { + done('unexpected error : ' + args[1]) + } else if (args[0] === 'response') { + assert.equal(args[1], 'Hello from serviceWorker!') + customSession.clearStorageData({ + storages: ['serviceworkers'] + }, function () { + customSession.protocol.uninterceptProtocol('file', (error) => done(error)) + }) + } + }) + w.loadURL(`file://${fixtures}/pages/service-worker/index.html`) }) }) From 4fdb884feb7c52731d7195ce5edc7752e8e4e9e4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 11 May 2017 15:18:40 -0700 Subject: [PATCH 2/3] Fix windows path in service worker spec --- spec/chromium-spec.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index b2dd89a0e5ac..8274caf0213f 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -173,7 +173,11 @@ describe('chromium feature', function () { it('should register for intercepted file scheme', function (done) { const customSession = session.fromPartition('intercept-file') customSession.protocol.interceptBufferProtocol('file', function (request, callback) { - const file = url.parse(request.url).pathname + let file = url.parse(request.url).pathname + // Remove leading slash before drive letter on Windows + if (file[0] === '/' && process.platform === 'win32') { + file = file.slice(1) + } const content = fs.readFileSync(path.normalize(file)) const ext = path.extname(file) let type = 'text/html' From ef4bf7ed93fd40d4f8bc37427f048117b64e3718 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 11 May 2017 15:30:59 -0700 Subject: [PATCH 3/3] Use status code constant --- atom/browser/net/js_asker.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atom/browser/net/js_asker.h b/atom/browser/net/js_asker.h index ebe53d982f7d..f9859f115ad2 100644 --- a/atom/browser/net/js_asker.h +++ b/atom/browser/net/js_asker.h @@ -13,6 +13,7 @@ #include "content/public/browser/browser_thread.h" #include "net/base/net_errors.h" #include "net/http/http_response_headers.h" +#include "net/http/http_status_code.h" #include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_job.h" #include "v8/include/v8.h" @@ -83,7 +84,7 @@ class JsAsker : public RequestJob { weak_factory_.GetWeakPtr()))); } - int GetResponseCode() const override { return 200; } + int GetResponseCode() const override { return net::HTTP_OK; } void GetResponseInfo(net::HttpResponseInfo* info) override { info->headers = new net::HttpResponseHeaders("");