2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { protocol, webContents, WebContents, session, BrowserWindow, ipcMain } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { AddressInfo } from 'net';
|
2020-04-01 02:33:16 +00:00
|
|
|
import * as ChildProcess from 'child_process';
|
2020-03-20 20:28:31 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as qs from 'querystring';
|
|
|
|
import * as stream from 'stream';
|
2020-06-11 16:55:59 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { closeWindow } from './window-helpers';
|
|
|
|
import { emittedOnce } from './events-helpers';
|
2020-06-08 16:49:36 +00:00
|
|
|
import { WebmGenerator } from './video-helpers';
|
2021-02-23 00:16:17 +00:00
|
|
|
import { delay, ifit } from './spec-helpers';
|
2020-03-20 20:28:31 +00:00
|
|
|
|
|
|
|
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures');
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
const registerStringProtocol = protocol.registerStringProtocol;
|
|
|
|
const registerBufferProtocol = protocol.registerBufferProtocol;
|
|
|
|
const registerFileProtocol = protocol.registerFileProtocol;
|
|
|
|
const registerHttpProtocol = protocol.registerHttpProtocol;
|
|
|
|
const registerStreamProtocol = protocol.registerStreamProtocol;
|
|
|
|
const interceptStringProtocol = protocol.interceptStringProtocol;
|
|
|
|
const interceptBufferProtocol = protocol.interceptBufferProtocol;
|
|
|
|
const interceptHttpProtocol = protocol.interceptHttpProtocol;
|
|
|
|
const interceptStreamProtocol = protocol.interceptStreamProtocol;
|
|
|
|
const unregisterProtocol = protocol.unregisterProtocol;
|
|
|
|
const uninterceptProtocol = protocol.uninterceptProtocol;
|
2020-03-20 20:28:31 +00:00
|
|
|
|
|
|
|
const text = 'valar morghulis';
|
|
|
|
const protocolName = 'sp';
|
2019-06-27 21:20:29 +00:00
|
|
|
const postData = {
|
|
|
|
name: 'post test',
|
|
|
|
type: 'string'
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-06-27 21:20:29 +00:00
|
|
|
|
|
|
|
function getStream (chunkSize = text.length, data: Buffer | string = text) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const body = new stream.PassThrough();
|
2019-06-27 21:20:29 +00:00
|
|
|
|
|
|
|
async function sendChunks () {
|
2020-03-20 20:28:31 +00:00
|
|
|
await delay(0); // the stream protocol API breaks if you send data immediately.
|
|
|
|
let buf = Buffer.from(data as any); // nodejs typings are wrong, Buffer.from can take a Buffer
|
2019-06-27 21:20:29 +00:00
|
|
|
for (;;) {
|
2020-03-20 20:28:31 +00:00
|
|
|
body.push(buf.slice(0, chunkSize));
|
|
|
|
buf = buf.slice(chunkSize);
|
2019-06-27 21:20:29 +00:00
|
|
|
if (!buf.length) {
|
2020-03-20 20:28:31 +00:00
|
|
|
break;
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
|
|
|
// emulate some network delay
|
2020-03-20 20:28:31 +00:00
|
|
|
await delay(10);
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
body.push(null);
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
sendChunks();
|
|
|
|
return body;
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A promise that can be resolved externally.
|
2019-11-01 20:37:02 +00:00
|
|
|
function defer (): Promise<any> & {resolve: Function, reject: Function} {
|
2020-03-20 20:28:31 +00:00
|
|
|
let promiseResolve: Function = null as unknown as Function;
|
|
|
|
let promiseReject: Function = null as unknown as Function;
|
2019-06-27 21:20:29 +00:00
|
|
|
const promise: any = new Promise((resolve, reject) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
promiseResolve = resolve;
|
|
|
|
promiseReject = reject;
|
|
|
|
});
|
|
|
|
promise.resolve = promiseResolve;
|
|
|
|
promise.reject = promiseReject;
|
|
|
|
return promise;
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
2017-11-23 22:22:43 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol module', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let contents: WebContents = null as unknown as WebContents;
|
2019-06-27 21:20:29 +00:00
|
|
|
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
|
2020-03-20 20:28:31 +00:00
|
|
|
before(() => { contents = (webContents as any).create({ sandbox: true }); });
|
|
|
|
after(() => (contents as any).destroy());
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-06-27 21:20:29 +00:00
|
|
|
async function ajax (url: string, options = {}) {
|
2019-05-10 00:51:44 +00:00
|
|
|
// Note that we need to do navigation every time after a protocol is
|
|
|
|
// registered or unregistered, otherwise the new protocol won't be
|
|
|
|
// recognized by current page when NetworkService is used.
|
2020-03-20 20:28:31 +00:00
|
|
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'jquery.html'));
|
|
|
|
return contents.executeJavaScript(`ajax("${url}", ${JSON.stringify(options)})`);
|
2019-05-10 00:51:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
afterEach(() => {
|
|
|
|
protocol.unregisterProtocol(protocolName);
|
|
|
|
protocol.uninterceptProtocol('http');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.register(Any)Protocol', () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
it('fails when scheme is already registered', () => {
|
|
|
|
expect(registerStringProtocol(protocolName, (req, cb) => cb(''))).to.equal(true);
|
|
|
|
expect(registerBufferProtocol(protocolName, (req, cb) => cb(Buffer.from('')))).to.equal(false);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('does not crash when handler is called twice', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(text);
|
2020-06-02 16:46:18 +00:00
|
|
|
callback('');
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-01-19 19:31:47 +00:00
|
|
|
// Ignore error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends error when callback is called with nothing', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerBufferProtocol(protocolName, (req, cb: any) => cb());
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.eventually.be.rejectedWith(Error, '404');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('does not crash when callback is called in next tick', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
setImmediate(() => callback(text));
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.unregisterProtocol', () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
it('returns false when scheme does not exist', () => {
|
|
|
|
expect(unregisterProtocol('not-exist')).to.equal(false);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerStringProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends string as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.headers).to.include('access-control-allow-origin: *');
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends object as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
data: text,
|
|
|
|
mimeType: 'text/html'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending object other than string', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const notAString = () => {};
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerBufferProtocol', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const buffer = Buffer.from(text);
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends Buffer as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.headers).to.include('access-control-allow-origin: *');
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends object as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerBufferProtocol(protocolName, (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
data: buffer,
|
|
|
|
mimeType: 'text/html'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending string', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerFileProtocol', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const filePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'file1');
|
|
|
|
const fileContent = fs.readFileSync(filePath);
|
|
|
|
const normalPath = path.join(fixturesPath, 'pages', 'a.html');
|
|
|
|
const normalContent = fs.readFileSync(normalPath);
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends file path as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
expect(r.headers).to.include('access-control-allow-origin: *');
|
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sets custom headers', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({
|
2018-12-19 06:17:02 +00:00
|
|
|
path: filePath,
|
|
|
|
headers: { 'X-Great-Header': 'sogreat' }
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
expect(r.headers).to.include('x-great-header: sogreat');
|
|
|
|
});
|
2018-12-19 06:17:02 +00:00
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
it.skip('throws an error when custom headers are invalid', (done) => {
|
2019-06-27 21:20:29 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => {
|
2019-06-21 16:23:57 +00:00
|
|
|
expect(() => callback({
|
|
|
|
path: filePath,
|
2019-06-27 21:20:29 +00:00
|
|
|
headers: { 'X-Great-Header': (42 as any) }
|
2020-03-20 20:28:31 +00:00
|
|
|
})).to.throw(Error, 'Value of \'X-Great-Header\' header has to be a string');
|
|
|
|
done();
|
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
ajax(protocolName + '://fake-host');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-21 16:23:57 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends object as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can send normal file', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(normalPath));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(normalContent));
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending unexist-file', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(fakeFilePath));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending unsupported content', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.registerHttpProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends url as response', async () => {
|
2017-10-26 20:30:07 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(req.headers.accept).to.not.equal('');
|
|
|
|
res.end(text);
|
|
|
|
server.close();
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
2020-03-20 20:28:31 +00:00
|
|
|
|
|
|
|
const port = (server.address() as AddressInfo).port;
|
|
|
|
const url = 'http://127.0.0.1:' + port;
|
2020-06-02 16:46:18 +00:00
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url }));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending invalid url', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('fails when sending unsupported content', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
2016-05-29 08:13:26 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('works when target URL redirects', async () => {
|
2017-10-26 20:30:07 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2016-06-29 16:37:10 +00:00
|
|
|
if (req.url === '/serverRedirect') {
|
2020-03-20 20:28:31 +00:00
|
|
|
res.statusCode = 301;
|
|
|
|
res.setHeader('Location', `http://${req.rawHeaders[1]}`);
|
|
|
|
res.end();
|
2016-05-29 08:13:26 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
res.end(text);
|
2016-05-29 08:13:26 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
after(() => server.close());
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
2019-05-10 00:51:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const port = (server.address() as AddressInfo).port;
|
|
|
|
const url = `${protocolName}://fake-host`;
|
|
|
|
const redirectURL = `http://127.0.0.1:${port}/serverRedirect`;
|
2020-06-02 16:46:18 +00:00
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: redirectURL }));
|
2019-05-10 00:51:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(url);
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2018-10-30 18:37:49 +00:00
|
|
|
|
|
|
|
it('can access request headers', (done) => {
|
2019-06-27 21:20:29 +00:00
|
|
|
protocol.registerHttpProtocol(protocolName, (request) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(request).to.have.property('headers');
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
ajax(protocolName + '://fake-host');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-10 12:35:32 +00:00
|
|
|
describe('protocol.registerStreamProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends Stream as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback(getStream()));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends object as response', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({ data: getStream() }));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.status).to.equal(200);
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends custom response headers', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
2017-11-10 12:35:32 +00:00
|
|
|
data: getStream(3),
|
|
|
|
headers: {
|
|
|
|
'x-electron': ['a', 'b']
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.status).to.equal(200);
|
|
|
|
expect(r.headers).to.include('x-electron: a, b');
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends custom status code', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
2017-11-10 12:35:32 +00:00
|
|
|
statusCode: 204,
|
2020-06-02 16:46:18 +00:00
|
|
|
data: null as any
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.be.undefined('data');
|
|
|
|
expect(r.status).to.equal(204);
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('receives request headers', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
2017-11-10 12:35:32 +00:00
|
|
|
callback({
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
data: getStream(5, JSON.stringify(Object.assign({}, request.headers)))
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
|
|
|
expect(r.data['x-return-headers']).to.equal('yes');
|
|
|
|
});
|
2018-10-25 00:34:20 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('returns response multiple response headers with the same name', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
2018-10-25 00:34:20 +00:00
|
|
|
callback({
|
|
|
|
headers: {
|
2020-03-20 15:12:18 +00:00
|
|
|
header1: ['value1', 'value2'],
|
|
|
|
header2: 'value3'
|
2018-10-25 00:34:20 +00:00
|
|
|
},
|
|
|
|
data: getStream()
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
2019-05-10 00:51:44 +00:00
|
|
|
// SUBTLE: when the response headers have multiple values it
|
|
|
|
// separates values by ", ". When the response headers are incorrectly
|
|
|
|
// converting an array to a string it separates values by ",".
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(r.headers).to.equal('header1: value1, value2\r\nheader2: value3\r\n');
|
|
|
|
});
|
2019-01-25 18:57:26 +00:00
|
|
|
|
|
|
|
it('can handle large responses', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const data = Buffer.alloc(128 * 1024);
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(getStream(data.length, data));
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.have.lengthOf(data.length);
|
|
|
|
});
|
2020-01-13 18:28:21 +00:00
|
|
|
|
|
|
|
it('can handle a stream completing while writing', async () => {
|
|
|
|
function dumbPassthrough () {
|
|
|
|
return new stream.Transform({
|
|
|
|
async transform (chunk, encoding, cb) {
|
2020-03-20 20:28:31 +00:00
|
|
|
cb(null, chunk);
|
2020-01-13 18:28:21 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-01-13 18:28:21 +00:00
|
|
|
}
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
2020-01-13 18:28:21 +00:00
|
|
|
callback({
|
|
|
|
statusCode: 200,
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
data: getStream(1024 * 1024, Buffer.alloc(1024 * 1024 * 2)).pipe(dumbPassthrough())
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.have.lengthOf(1024 * 1024 * 2);
|
|
|
|
});
|
2020-06-11 16:55:59 +00:00
|
|
|
|
|
|
|
it('can handle next-tick scheduling during read calls', async () => {
|
|
|
|
const events = new EventEmitter();
|
|
|
|
function createStream () {
|
|
|
|
const buffers = [
|
|
|
|
Buffer.alloc(65536),
|
|
|
|
Buffer.alloc(65537),
|
|
|
|
Buffer.alloc(39156)
|
|
|
|
];
|
|
|
|
const e = new stream.Readable({ highWaterMark: 0 });
|
|
|
|
e.push(buffers.shift());
|
|
|
|
e._read = function () {
|
|
|
|
process.nextTick(() => this.push(buffers.shift() || null));
|
|
|
|
};
|
|
|
|
e.on('end', function () {
|
|
|
|
events.emit('end');
|
|
|
|
});
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
statusCode: 200,
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
data: createStream()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const hasEndedPromise = emittedOnce(events, 'end');
|
|
|
|
ajax(protocolName + '://fake-host');
|
|
|
|
await hasEndedPromise;
|
|
|
|
});
|
2020-07-21 00:51:38 +00:00
|
|
|
|
|
|
|
it('destroys response streams when aborted before completion', async () => {
|
|
|
|
const events = new EventEmitter();
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
const responseStream = new stream.PassThrough();
|
|
|
|
responseStream.push('data\r\n');
|
|
|
|
responseStream.on('close', () => {
|
|
|
|
events.emit('close');
|
|
|
|
});
|
|
|
|
callback({
|
|
|
|
statusCode: 200,
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
data: responseStream
|
|
|
|
});
|
|
|
|
events.emit('respond');
|
|
|
|
});
|
|
|
|
|
|
|
|
const hasRespondedPromise = emittedOnce(events, 'respond');
|
|
|
|
const hasClosedPromise = emittedOnce(events, 'close');
|
|
|
|
ajax(protocolName + '://fake-host');
|
|
|
|
await hasRespondedPromise;
|
|
|
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'jquery.html'));
|
|
|
|
await hasClosedPromise;
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
describe('protocol.isProtocolRegistered', () => {
|
|
|
|
it('returns false when scheme is not registered', () => {
|
|
|
|
const result = protocol.isProtocolRegistered('no-exist');
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(result).to.be.false('no-exist: is handled');
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
it('returns true for custom protocol', () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(''));
|
|
|
|
const result = protocol.isProtocolRegistered(protocolName);
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(result).to.be.true('custom protocol is handled');
|
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
describe('protocol.isProtocolIntercepted', () => {
|
|
|
|
it('returns true for intercepted protocol', () => {
|
|
|
|
interceptStringProtocol('http', (request, callback) => callback(''));
|
|
|
|
const result = protocol.isProtocolIntercepted('http');
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(result).to.be.true('intercepted protocol is handled');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.intercept(Any)Protocol', () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
it('returns false when scheme is already intercepted', () => {
|
|
|
|
expect(protocol.interceptStringProtocol('http', (request, callback) => callback(''))).to.equal(true);
|
|
|
|
expect(protocol.interceptBufferProtocol('http', (request, callback) => callback(Buffer.from('')))).to.equal(false);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('does not crash when handler is called twice', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(text);
|
2020-06-02 16:46:18 +00:00
|
|
|
callback('');
|
2016-01-19 22:49:40 +00:00
|
|
|
} catch (error) {
|
2016-01-19 19:31:47 +00:00
|
|
|
// Ignore error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.be.equal(text);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('sends error when callback is called with nothing', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback: any) => callback());
|
2020-03-20 20:28:31 +00:00
|
|
|
await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.interceptStringProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback) => callback(text));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can set content-type', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2016-02-17 01:39:11 +00:00
|
|
|
callback({
|
2016-01-12 02:40:23 +00:00
|
|
|
mimeType: 'application/json',
|
|
|
|
data: '{"value": 1}'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.be.an('object');
|
|
|
|
expect(r.data).to.have.property('value').that.is.equal(1);
|
|
|
|
});
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2019-10-11 18:54:50 +00:00
|
|
|
it('can set content-type with charset', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2019-10-11 18:54:50 +00:00
|
|
|
callback({
|
|
|
|
mimeType: 'application/json; charset=UTF-8',
|
|
|
|
data: '{"value": 1}'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.be.an('object');
|
|
|
|
expect(r.data).to.have.property('value').that.is.equal(1);
|
|
|
|
});
|
2019-10-11 18:54:50 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
|
|
|
const uploadData = request.uploadData![0].bytes.toString();
|
2020-03-20 20:28:31 +00:00
|
|
|
callback({ data: uploadData });
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.interceptBufferProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptBufferProtocol('http', (request, callback) => callback(Buffer.from(text)));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptBufferProtocol('http', (request, callback) => {
|
|
|
|
const uploadData = request.uploadData![0].bytes;
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(uploadData);
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
|
|
|
expect(r.data).to.equal('name=post+test&type=string');
|
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.interceptHttpProtocol', () => {
|
2019-05-07 02:33:05 +00:00
|
|
|
// FIXME(zcbenz): This test was passing because the test itself was wrong,
|
|
|
|
// I don't know whether it ever passed before and we should take a look at
|
|
|
|
// it in future.
|
2019-05-10 00:51:44 +00:00
|
|
|
xit('can send POST request', async () => {
|
2017-10-26 20:30:07 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let body = '';
|
2017-10-26 20:30:07 +00:00
|
|
|
req.on('data', (chunk) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
body += chunk;
|
|
|
|
});
|
2017-10-26 20:30:07 +00:00
|
|
|
req.on('end', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
res.end(body);
|
|
|
|
});
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
after(() => server.close());
|
2020-06-02 16:46:18 +00:00
|
|
|
server.listen(0, '127.0.0.1');
|
2020-03-20 20:28:31 +00:00
|
|
|
|
|
|
|
const port = (server.address() as AddressInfo).port;
|
|
|
|
const url = `http://127.0.0.1:${port}`;
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptHttpProtocol('http', (request, callback) => {
|
|
|
|
const data: Electron.ProtocolResponse = {
|
2019-05-10 00:51:44 +00:00
|
|
|
url: url,
|
|
|
|
method: 'POST',
|
|
|
|
uploadData: {
|
|
|
|
contentType: 'application/x-www-form-urlencoded',
|
2020-06-02 16:46:18 +00:00
|
|
|
data: request.uploadData![0].bytes
|
2019-05-10 00:51:44 +00:00
|
|
|
},
|
2020-06-02 16:46:18 +00:00
|
|
|
session: undefined
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
|
|
|
});
|
2016-11-27 19:07:50 +00:00
|
|
|
|
2019-08-20 11:46:20 +00:00
|
|
|
it('can use custom session', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customSession = session.fromPartition('custom-ses', { cache: false });
|
2017-10-26 20:30:07 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(details.url).to.equal('http://fake-host/');
|
|
|
|
callback({ cancel: true });
|
|
|
|
});
|
|
|
|
after(() => customSession.webRequest.onBeforeRequest(null));
|
2019-05-10 00:51:44 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptHttpProtocol('http', (request, callback) => {
|
2016-11-27 19:07:50 +00:00
|
|
|
callback({
|
|
|
|
url: request.url,
|
|
|
|
session: customSession
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error);
|
|
|
|
});
|
2018-10-30 18:37:49 +00:00
|
|
|
|
|
|
|
it('can access request headers', (done) => {
|
2019-06-27 21:20:29 +00:00
|
|
|
protocol.interceptHttpProtocol('http', (request) => {
|
2020-06-30 22:10:36 +00:00
|
|
|
try {
|
|
|
|
expect(request).to.have.property('headers');
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
ajax('http://fake-host');
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-10 12:35:32 +00:00
|
|
|
describe('protocol.interceptStreamProtocol', () => {
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStreamProtocol('http', (request, callback) => callback(getStream()));
|
2020-03-20 20:28:31 +00:00
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
|
|
|
callback(getStream(3, request.uploadData![0].bytes.toString()));
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can execute redirects', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
2017-11-10 12:35:32 +00:00
|
|
|
if (request.url.indexOf('http://fake-host') === 0) {
|
|
|
|
setTimeout(() => {
|
|
|
|
callback({
|
2020-06-02 16:46:18 +00:00
|
|
|
data: '',
|
2017-11-10 12:35:32 +00:00
|
|
|
statusCode: 302,
|
|
|
|
headers: {
|
|
|
|
Location: 'http://fake-redirect'
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
}, 300);
|
2017-11-10 12:35:32 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.url.indexOf('http://fake-redirect')).to.equal(0);
|
|
|
|
callback(getStream(1, 'redirect'));
|
2017-11-10 12:35:32 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host');
|
|
|
|
expect(r.data).to.equal('redirect');
|
|
|
|
});
|
2021-01-19 10:06:14 +00:00
|
|
|
|
|
|
|
it('should discard post data after redirection', async () => {
|
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
|
|
|
if (request.url.indexOf('http://fake-host') === 0) {
|
|
|
|
setTimeout(() => {
|
|
|
|
callback({
|
|
|
|
statusCode: 302,
|
|
|
|
headers: {
|
|
|
|
Location: 'http://fake-redirect'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 300);
|
|
|
|
} else {
|
|
|
|
expect(request.url.indexOf('http://fake-redirect')).to.equal(0);
|
|
|
|
callback(getStream(3, request.method));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
|
|
|
expect(r.data).to.equal('GET');
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-11-10 12:35:32 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
describe('protocol.uninterceptProtocol', () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
it('returns false when scheme does not exist', () => {
|
|
|
|
expect(uninterceptProtocol('not-exist')).to.equal(false);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2020-06-02 16:46:18 +00:00
|
|
|
it('returns false when scheme is not intercepted', () => {
|
|
|
|
expect(uninterceptProtocol('http')).to.equal(false);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2020-04-01 02:33:16 +00:00
|
|
|
describe('protocol.registerSchemeAsPrivileged', () => {
|
2021-02-23 00:16:17 +00:00
|
|
|
// TODO(jeremy): figure out why this times out under ASan
|
|
|
|
ifit(!process.env.IS_ASAN)('does not crash on exit', async () => {
|
2020-04-01 02:33:16 +00:00
|
|
|
const appPath = path.join(__dirname, 'fixtures', 'api', 'custom-protocol-shutdown.js');
|
|
|
|
const appProcess = ChildProcess.spawn(process.execPath, ['--enable-logging', appPath]);
|
|
|
|
let stdout = '';
|
|
|
|
let stderr = '';
|
2021-02-23 00:16:17 +00:00
|
|
|
appProcess.stdout.on('data', data => { process.stdout.write(data); stdout += data; });
|
|
|
|
appProcess.stderr.on('data', data => { process.stderr.write(data); stderr += data; });
|
2020-04-01 02:33:16 +00:00
|
|
|
const [code] = await emittedOnce(appProcess, 'exit');
|
|
|
|
if (code !== 0) {
|
|
|
|
console.log('Exit code : ', code);
|
|
|
|
console.log('stdout : ', stdout);
|
|
|
|
console.log('stderr : ', stderr);
|
|
|
|
}
|
|
|
|
expect(code).to.equal(0);
|
|
|
|
expect(stdout).to.not.contain('VALIDATION_ERROR_DESERIALIZATION_FAILED');
|
|
|
|
expect(stderr).to.not.contain('VALIDATION_ERROR_DESERIALIZATION_FAILED');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
describe.skip('protocol.registerSchemesAsPrivileged standard', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const standardScheme = (global as any).standardScheme;
|
|
|
|
const origin = `${standardScheme}://fake-host`;
|
|
|
|
const imageURL = `${origin}/test.png`;
|
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'b.html');
|
|
|
|
const fileContent = '<img src="/test.png" />';
|
|
|
|
let w: BrowserWindow = null as unknown as BrowserWindow;
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
beforeEach(() => {
|
2019-01-07 19:19:27 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2019-06-27 21:20:29 +00:00
|
|
|
afterEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await closeWindow(w);
|
2020-06-02 16:46:18 +00:00
|
|
|
unregisterProtocol(standardScheme);
|
2020-03-20 20:28:31 +00:00
|
|
|
w = null as unknown as BrowserWindow;
|
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('resolves relative resources', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerFileProtocol(standardScheme, (request, callback) => {
|
2016-05-05 18:34:16 +00:00
|
|
|
if (request.url === imageURL) {
|
2020-06-02 16:46:18 +00:00
|
|
|
callback('');
|
2016-05-05 18:34:16 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback(filePath);
|
2016-05-05 18:34:16 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
await w.loadURL(origin);
|
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('resolves absolute resources', async () => {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => {
|
2016-05-05 18:34:16 +00:00
|
|
|
if (request.url === imageURL) {
|
2020-06-02 16:46:18 +00:00
|
|
|
callback('');
|
2016-05-05 18:34:16 +00:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
data: fileContent,
|
|
|
|
mimeType: 'text/html'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-05-05 18:34:16 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
await w.loadURL(origin);
|
|
|
|
});
|
2016-06-08 05:44:30 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('can have fetch working in it', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestReceived = defer();
|
2019-06-27 21:20:29 +00:00
|
|
|
const server = http.createServer((req, res) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
res.end();
|
|
|
|
server.close();
|
|
|
|
requestReceived.resolve();
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
2020-03-20 20:28:31 +00:00
|
|
|
const port = (server.address() as AddressInfo).port;
|
|
|
|
const content = `<script>fetch("http://127.0.0.1:${port}")</script>`;
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => callback({ data: content, mimeType: 'text/html' }));
|
2020-03-20 20:28:31 +00:00
|
|
|
await w.loadURL(origin);
|
|
|
|
await requestReceived;
|
|
|
|
});
|
2016-08-24 01:23:14 +00:00
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
it.skip('can access files through the FileSystem API', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'filesystem.html');
|
2020-06-02 16:46:18 +00:00
|
|
|
protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath }));
|
|
|
|
w.loadURL(origin);
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.once('file-system-error', (event, err) => done(err));
|
|
|
|
ipcMain.once('file-system-write-end', () => done());
|
|
|
|
});
|
2016-11-14 16:20:04 +00:00
|
|
|
|
2017-10-26 20:30:07 +00:00
|
|
|
it('registers secure, when {secure: true}', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'cache-storage.html');
|
|
|
|
ipcMain.once('success', () => done());
|
|
|
|
ipcMain.once('failure', (event, err) => done(err));
|
2020-06-02 16:46:18 +00:00
|
|
|
protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath }));
|
|
|
|
w.loadURL(origin);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-08-02 23:56:46 +00:00
|
|
|
describe.skip('protocol.registerSchemesAsPrivileged cors-fetch', function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const standardScheme = (global as any).standardScheme;
|
|
|
|
let w: BrowserWindow = null as unknown as BrowserWindow;
|
2019-06-27 21:20:29 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false });
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-06-27 21:20:29 +00:00
|
|
|
afterEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
2020-06-02 16:46:18 +00:00
|
|
|
for (const scheme of [standardScheme, 'cors', 'no-cors', 'no-fetch']) {
|
|
|
|
protocol.unregisterProtocol(scheme);
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('supports fetch api by default', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const url = `file://${fixturesPath}/assets/logo.png`;
|
|
|
|
await w.loadURL(`file://${fixturesPath}/pages/blank.html`);
|
|
|
|
const ok = await w.webContents.executeJavaScript(`fetch(${JSON.stringify(url)}).then(r => r.ok)`);
|
|
|
|
expect(ok).to.be.true('response ok');
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('allows CORS requests by default', async () => {
|
2019-06-27 21:20:29 +00:00
|
|
|
await allowsCORSRequests('cors', 200, new RegExp(''), () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-01-29 07:11:01 +00:00
|
|
|
fetch('cors://myhost').then(function (response) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('response', response.status);
|
2019-11-01 20:37:02 +00:00
|
|
|
}).catch(function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('response', 'failed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-06-27 21:20:29 +00:00
|
|
|
it('disallows CORS and fetch requests when only supportFetchAPI is specified', async () => {
|
|
|
|
await allowsCORSRequests('no-cors', ['failed xhr', 'failed fetch'], /has been blocked by CORS policy/, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-06-27 21:20:29 +00:00
|
|
|
Promise.all([
|
|
|
|
new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.onload = () => resolve('loaded xhr');
|
|
|
|
req.onerror = () => resolve('failed xhr');
|
|
|
|
req.open('GET', 'no-cors://myhost');
|
|
|
|
req.send();
|
2019-06-27 21:20:29 +00:00
|
|
|
}),
|
|
|
|
fetch('no-cors://myhost')
|
|
|
|
.then(() => 'loaded fetch')
|
|
|
|
.catch(() => 'failed fetch')
|
|
|
|
]).then(([xhr, fetch]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('response', [xhr, fetch]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-05-10 00:51:44 +00:00
|
|
|
it('allows CORS, but disallows fetch requests, when specified', async () => {
|
2019-06-27 21:20:29 +00:00
|
|
|
await allowsCORSRequests('no-fetch', ['loaded xhr', 'failed fetch'], /Fetch API cannot load/, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-06-27 21:20:29 +00:00
|
|
|
Promise.all([
|
|
|
|
new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.onload = () => resolve('loaded xhr');
|
|
|
|
req.onerror = () => resolve('failed xhr');
|
|
|
|
req.open('GET', 'no-fetch://myhost');
|
|
|
|
req.send();
|
2019-06-27 21:20:29 +00:00
|
|
|
}),
|
|
|
|
fetch('no-fetch://myhost')
|
|
|
|
.then(() => 'loaded fetch')
|
|
|
|
.catch(() => 'failed fetch')
|
|
|
|
]).then(([xhr, fetch]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcRenderer.send('response', [xhr, fetch]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2019-06-27 21:20:29 +00:00
|
|
|
async function allowsCORSRequests (corsScheme: string, expected: any, expectedConsole: RegExp, content: Function) {
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback({ data: `<script>(${content})()</script>`, mimeType: 'text/html' });
|
|
|
|
});
|
2020-06-02 16:46:18 +00:00
|
|
|
registerStringProtocol(corsScheme, (request, callback) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
callback('');
|
|
|
|
});
|
2019-01-29 07:11:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const newContents: WebContents = (webContents as any).create({ nodeIntegration: true });
|
|
|
|
const consoleMessages: string[] = [];
|
|
|
|
newContents.on('console-message', (e, level, message) => consoleMessages.push(message));
|
2019-06-27 21:20:29 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
newContents.loadURL(standardScheme + '://fake-host');
|
|
|
|
const [, response] = await emittedOnce(ipcMain, 'response');
|
|
|
|
expect(response).to.deep.equal(expected);
|
|
|
|
expect(consoleMessages.join('\n')).to.match(expectedConsole);
|
2019-06-27 21:20:29 +00:00
|
|
|
} finally {
|
|
|
|
// This is called in a timeout to avoid a crash that happens when
|
|
|
|
// calling destroy() in a microtask.
|
|
|
|
setTimeout(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(newContents as any).destroy();
|
|
|
|
});
|
2019-06-27 21:20:29 +00:00
|
|
|
}
|
2019-01-29 07:11:01 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-06-08 16:49:36 +00:00
|
|
|
|
|
|
|
describe('protocol.registerSchemesAsPrivileged stream', async function () {
|
|
|
|
const pagePath = path.join(fixturesPath, 'pages', 'video.html');
|
|
|
|
const videoSourceImagePath = path.join(fixturesPath, 'video-source-image.webp');
|
|
|
|
const videoPath = path.join(fixturesPath, 'video.webm');
|
|
|
|
const standardScheme = (global as any).standardScheme;
|
|
|
|
let w: BrowserWindow = null as unknown as BrowserWindow;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
// generate test video
|
|
|
|
const imageBase64 = await fs.promises.readFile(videoSourceImagePath, 'base64');
|
|
|
|
const imageDataUrl = `data:image/webp;base64,${imageBase64}`;
|
|
|
|
const encoder = new WebmGenerator(15);
|
|
|
|
for (let i = 0; i < 30; i++) {
|
|
|
|
encoder.add(imageDataUrl);
|
|
|
|
}
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
encoder.compile((output:Uint8Array) => {
|
|
|
|
fs.promises.writeFile(videoPath, output).then(resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await fs.promises.unlink(videoPath);
|
|
|
|
});
|
|
|
|
|
2021-01-07 19:45:29 +00:00
|
|
|
beforeEach(async function () {
|
2020-06-08 16:49:36 +00:00
|
|
|
w = new BrowserWindow({ show: false });
|
2021-01-07 19:45:29 +00:00
|
|
|
await w.loadURL('about:blank');
|
|
|
|
if (!await w.webContents.executeJavaScript('document.createElement(\'video\').canPlayType(\'video/webm; codecs="vp8.0"\')')) {
|
|
|
|
this.skip();
|
|
|
|
}
|
2020-06-08 16:49:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
|
|
|
await protocol.unregisterProtocol(standardScheme);
|
|
|
|
await protocol.unregisterProtocol('stream');
|
|
|
|
});
|
|
|
|
|
2020-10-06 00:53:13 +00:00
|
|
|
it('successfully plays videos when content is buffered (stream: false)', async () => {
|
|
|
|
await streamsResponses(standardScheme, 'play');
|
2020-06-08 16:49:36 +00:00
|
|
|
});
|
|
|
|
|
2020-10-06 00:53:13 +00:00
|
|
|
it('successfully plays videos when streaming content (stream: true)', async () => {
|
2020-06-08 16:49:36 +00:00
|
|
|
await streamsResponses('stream', 'play');
|
|
|
|
});
|
|
|
|
|
|
|
|
async function streamsResponses (testingScheme: string, expected: any) {
|
|
|
|
const protocolHandler = (request: any, callback: Function) => {
|
|
|
|
if (request.url.includes('/video.webm')) {
|
|
|
|
const stat = fs.statSync(videoPath);
|
|
|
|
const fileSize = stat.size;
|
|
|
|
const range = request.headers.Range;
|
|
|
|
if (range) {
|
|
|
|
const parts = range.replace(/bytes=/, '').split('-');
|
|
|
|
const start = parseInt(parts[0], 10);
|
|
|
|
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
|
|
|
|
const chunksize = (end - start) + 1;
|
|
|
|
const headers = {
|
|
|
|
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
|
|
|
'Accept-Ranges': 'bytes',
|
|
|
|
'Content-Length': String(chunksize),
|
|
|
|
'Content-Type': 'video/webm'
|
|
|
|
};
|
|
|
|
callback({ statusCode: 206, headers, data: fs.createReadStream(videoPath, { start, end }) });
|
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
statusCode: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Length': String(fileSize),
|
|
|
|
'Content-Type': 'video/webm'
|
|
|
|
},
|
|
|
|
data: fs.createReadStream(videoPath)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback({ data: fs.createReadStream(pagePath), headers: { 'Content-Type': 'text/html' }, statusCode: 200 });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await registerStreamProtocol(standardScheme, protocolHandler);
|
|
|
|
await registerStreamProtocol('stream', protocolHandler);
|
|
|
|
|
2021-03-01 21:52:29 +00:00
|
|
|
const newContents: WebContents = (webContents as any).create({ nodeIntegration: true, contextIsolation: false });
|
2020-06-08 16:49:36 +00:00
|
|
|
try {
|
|
|
|
newContents.loadURL(testingScheme + '://fake-host');
|
|
|
|
const [, response] = await emittedOnce(ipcMain, 'result');
|
|
|
|
expect(response).to.deep.equal(expected);
|
|
|
|
} finally {
|
|
|
|
// This is called in a timeout to avoid a crash that happens when
|
|
|
|
// calling destroy() in a microtask.
|
|
|
|
setTimeout(() => {
|
|
|
|
(newContents as any).destroy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|