Merge pull request #9360 from electron/sw_protocol_patch

protocol: provide default response code for custom request jobs
This commit is contained in:
Kevin Sawicki 2017-05-11 16:01:14 -07:00 committed by GitHub
commit afe58231ba
3 changed files with 48 additions and 4 deletions

View file

@ -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"
@ -82,6 +83,9 @@ class JsAsker : public RequestJob {
base::Bind(&JsAsker::OnResponse,
weak_factory_.GetWeakPtr())));
}
int GetResponseCode() const override { return net::HTTP_OK; }
void GetResponseInfo(net::HttpResponseInfo* info) override {
info->headers = new net::HttpResponseHeaders("");
}

View file

@ -67,7 +67,7 @@ void URLRequestBufferJob::StartAsync(std::unique_ptr<base::Value> 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_));

View file

@ -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,49 @@ 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) {
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'
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`)
})
})