Merge pull request #9360 from electron/sw_protocol_patch
protocol: provide default response code for custom request jobs
This commit is contained in:
commit
afe58231ba
3 changed files with 48 additions and 4 deletions
|
@ -13,6 +13,7 @@
|
||||||
#include "content/public/browser/browser_thread.h"
|
#include "content/public/browser/browser_thread.h"
|
||||||
#include "net/base/net_errors.h"
|
#include "net/base/net_errors.h"
|
||||||
#include "net/http/http_response_headers.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_context_getter.h"
|
||||||
#include "net/url_request/url_request_job.h"
|
#include "net/url_request/url_request_job.h"
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
|
@ -82,6 +83,9 @@ class JsAsker : public RequestJob {
|
||||||
base::Bind(&JsAsker::OnResponse,
|
base::Bind(&JsAsker::OnResponse,
|
||||||
weak_factory_.GetWeakPtr())));
|
weak_factory_.GetWeakPtr())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetResponseCode() const override { return net::HTTP_OK; }
|
||||||
|
|
||||||
void GetResponseInfo(net::HttpResponseInfo* info) override {
|
void GetResponseInfo(net::HttpResponseInfo* info) override {
|
||||||
info->headers = new net::HttpResponseHeaders("");
|
info->headers = new net::HttpResponseHeaders("");
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ void URLRequestBufferJob::StartAsync(std::unique_ptr<base::Value> options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void URLRequestBufferJob::GetResponseInfo(net::HttpResponseInfo* info) {
|
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(base::IntToString(status_code_));
|
||||||
status.append(" ");
|
status.append(" ");
|
||||||
status.append(net::GetHttpReasonPhrase(status_code_));
|
status.append(net::GetHttpReasonPhrase(status_code_));
|
||||||
|
|
|
@ -149,8 +149,6 @@ describe('chromium feature', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('navigator.serviceWorker', function () {
|
describe('navigator.serviceWorker', function () {
|
||||||
var url = 'file://' + fixtures + '/pages/service-worker/index.html'
|
|
||||||
|
|
||||||
it('should register for file scheme', function (done) {
|
it('should register for file scheme', function (done) {
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false
|
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`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue