2019-06-28 00:20:29 +03:00
|
|
|
import { expect } from 'chai';
|
2021-03-23 18:16:53 +03:00
|
|
|
import { v4 } from 'uuid';
|
2023-03-27 20:00:55 +03:00
|
|
|
import { protocol, webContents, WebContents, session, BrowserWindow, ipcMain, net } from 'electron/main';
|
2023-06-15 17:42:27 +03:00
|
|
|
import * as ChildProcess from 'node:child_process';
|
|
|
|
import * as path from 'node:path';
|
|
|
|
import * as url from 'node:url';
|
|
|
|
import * as http from 'node:http';
|
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as qs from 'node:querystring';
|
|
|
|
import * as stream from 'node:stream';
|
2024-02-16 22:29:29 +03:00
|
|
|
import * as streamConsumers from 'node:stream/consumers';
|
|
|
|
import * as webStream from 'node:stream/web';
|
2023-06-15 17:42:27 +03:00
|
|
|
import { EventEmitter, once } from 'node:events';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { closeAllWindows, closeWindow } from './lib/window-helpers';
|
|
|
|
import { WebmGenerator } from './lib/video-helpers';
|
2023-03-27 20:00:55 +03:00
|
|
|
import { listen, defer, ifit } from './lib/spec-helpers';
|
2023-06-15 17:42:27 +03:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2022-08-16 22:23:13 +03:00
|
|
|
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
const registerStringProtocol = protocol.registerStringProtocol;
|
|
|
|
const registerBufferProtocol = protocol.registerBufferProtocol;
|
|
|
|
const registerFileProtocol = protocol.registerFileProtocol;
|
|
|
|
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 23:28:31 +03:00
|
|
|
|
2019-06-28 00:20:29 +03:00
|
|
|
const text = 'valar morghulis';
|
2022-01-24 12:34:23 +03:00
|
|
|
const protocolName = 'no-cors';
|
2019-06-28 00:20:29 +03:00
|
|
|
const postData = {
|
|
|
|
name: 'post test',
|
|
|
|
type: 'string'
|
|
|
|
};
|
|
|
|
|
|
|
|
function getStream (chunkSize = text.length, data: Buffer | string = text) {
|
2023-03-27 20:00:55 +03:00
|
|
|
// allowHalfOpen required, otherwise Readable.toWeb gets confused and thinks
|
|
|
|
// the stream isn't done when the readable half ends.
|
|
|
|
const body = new stream.PassThrough({ allowHalfOpen: false });
|
2019-06-28 00:20:29 +03:00
|
|
|
|
|
|
|
async function sendChunks () {
|
2023-02-24 02:53:53 +03:00
|
|
|
await setTimeout(0); // the stream protocol API breaks if you send data immediately.
|
2019-11-01 23:37:02 +03:00
|
|
|
let buf = Buffer.from(data as any); // nodejs typings are wrong, Buffer.from can take a Buffer
|
2019-06-28 00:20:29 +03:00
|
|
|
for (;;) {
|
|
|
|
body.push(buf.slice(0, chunkSize));
|
|
|
|
buf = buf.slice(chunkSize);
|
|
|
|
if (!buf.length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// emulate some network delay
|
2023-02-24 02:53:53 +03:00
|
|
|
await setTimeout(10);
|
2019-06-28 00:20:29 +03:00
|
|
|
}
|
|
|
|
body.push(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendChunks();
|
|
|
|
return body;
|
|
|
|
}
|
2023-03-27 20:00:55 +03:00
|
|
|
function getWebStream (chunkSize = text.length, data: Buffer | string = text): ReadableStream<ArrayBufferView> {
|
|
|
|
return stream.Readable.toWeb(getStream(chunkSize, data)) as ReadableStream<ArrayBufferView>;
|
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
|
|
|
|
// A promise that can be resolved externally.
|
2023-03-27 20:00:55 +03:00
|
|
|
function deferPromise (): Promise<any> & {resolve: Function, reject: Function} {
|
2019-06-28 00:20:29 +03:00
|
|
|
let promiseResolve: Function = null as unknown as Function;
|
|
|
|
let promiseReject: Function = null as unknown as Function;
|
|
|
|
const promise: any = new Promise((resolve, reject) => {
|
|
|
|
promiseResolve = resolve;
|
|
|
|
promiseReject = reject;
|
|
|
|
});
|
|
|
|
promise.resolve = promiseResolve;
|
|
|
|
promise.reject = promiseReject;
|
|
|
|
return promise;
|
|
|
|
}
|
2017-11-24 01:22:43 +03:00
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol module', () => {
|
2023-02-19 12:24:24 +03:00
|
|
|
let contents: WebContents;
|
2019-06-28 00:20:29 +03:00
|
|
|
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
|
2023-02-16 17:41:41 +03:00
|
|
|
before(() => { contents = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true }); });
|
2022-12-15 00:07:38 +03:00
|
|
|
after(() => contents.destroy());
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2019-06-28 00:20:29 +03:00
|
|
|
async function ajax (url: string, options = {}) {
|
2019-05-10 03:51:44 +03: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.
|
2022-01-24 12:34:23 +03:00
|
|
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
|
2019-05-10 03:51:44 +03:00
|
|
|
return contents.executeJavaScript(`ajax("${url}", ${JSON.stringify(options)})`);
|
|
|
|
}
|
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
afterEach(() => {
|
|
|
|
protocol.unregisterProtocol(protocolName);
|
|
|
|
protocol.uninterceptProtocol('http');
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.register(Any)Protocol', () => {
|
2020-06-02 19:46:18 +03: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);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('does not crash when handler is called twice', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
2016-01-12 05:40:23 +03:00
|
|
|
try {
|
2016-03-25 23:03:49 +03:00
|
|
|
callback(text);
|
2020-06-02 19:46:18 +03:00
|
|
|
callback('');
|
2023-07-27 17:53:45 +03:00
|
|
|
} catch {
|
2016-01-19 22:31:47 +03:00
|
|
|
// Ignore error
|
2016-01-12 05:40:23 +03:00
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('sends error when callback is called with nothing', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerBufferProtocol(protocolName, (req, cb: any) => cb());
|
2022-01-24 12:34:23 +03:00
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.eventually.be.rejected();
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('does not crash when callback is called in next tick', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
2017-10-26 23:30:07 +03:00
|
|
|
setImmediate(() => callback(text));
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2021-07-15 14:14:46 +03:00
|
|
|
|
|
|
|
it('can redirect to the same scheme', async () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
|
|
|
if (request.url === `${protocolName}://fake-host/redirect`) {
|
|
|
|
callback({
|
|
|
|
statusCode: 302,
|
|
|
|
headers: {
|
|
|
|
Location: `${protocolName}://fake-host`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
expect(request.url).to.equal(`${protocolName}://fake-host`);
|
|
|
|
callback('redirected');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const r = await ajax(`${protocolName}://fake-host/redirect`);
|
|
|
|
expect(r.data).to.equal('redirected');
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.unregisterProtocol', () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
it('returns false when scheme does not exist', () => {
|
|
|
|
expect(unregisterProtocol('not-exist')).to.equal(false);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
for (const [registerStringProtocol, name] of [
|
|
|
|
[protocol.registerStringProtocol, 'protocol.registerStringProtocol'] as const,
|
|
|
|
[(protocol as any).registerProtocol as typeof protocol.registerStringProtocol, 'protocol.registerProtocol'] as const
|
|
|
|
]) {
|
|
|
|
describe(name, () => {
|
|
|
|
it('sends string as response', async () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends object as response', async () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
data: text,
|
|
|
|
mimeType: 'text/html'
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('fails when sending object other than string', async () => {
|
|
|
|
const notAString = () => {};
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
for (const [registerBufferProtocol, name] of [
|
|
|
|
[protocol.registerBufferProtocol, 'protocol.registerBufferProtocol'] as const,
|
|
|
|
[(protocol as any).registerProtocol as typeof protocol.registerBufferProtocol, 'protocol.registerProtocol'] as const
|
|
|
|
]) {
|
|
|
|
describe(name, () => {
|
|
|
|
const buffer = Buffer.from(text);
|
|
|
|
it('sends Buffer as response', async () => {
|
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends object as response', async () => {
|
|
|
|
registerBufferProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
data: buffer,
|
|
|
|
mimeType: 'text/html'
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
if (name !== 'protocol.registerProtocol') {
|
|
|
|
it('fails when sending string', async () => {
|
|
|
|
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
|
|
});
|
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
for (const [registerFileProtocol, name] of [
|
|
|
|
[protocol.registerFileProtocol, 'protocol.registerFileProtocol'] as const,
|
|
|
|
[(protocol as any).registerProtocol as typeof protocol.registerFileProtocol, 'protocol.registerProtocol'] as const
|
|
|
|
]) {
|
|
|
|
describe(name, () => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
if (name === 'protocol.registerFileProtocol') {
|
|
|
|
it('sends file path as response', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
});
|
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sets Access-Control-Allow-Origin', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
|
|
|
});
|
2022-08-31 11:08:11 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sets custom headers', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({
|
|
|
|
path: filePath,
|
|
|
|
headers: { 'X-Great-Header': 'sogreat' }
|
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
expect(r.headers).to.have.property('x-great-header', 'sogreat');
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-09-04 13:33:29 +03:00
|
|
|
it('can load iframes with custom protocols', async () => {
|
2023-02-13 10:48:30 +03:00
|
|
|
registerFileProtocol('custom', (request, callback) => {
|
|
|
|
const filename = request.url.substring(9);
|
|
|
|
const p = path.join(__dirname, 'fixtures', 'pages', filename);
|
|
|
|
callback({ path: p });
|
|
|
|
});
|
2016-05-05 21:34:16 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
}
|
|
|
|
});
|
2018-12-19 09:17:02 +03:00
|
|
|
|
2023-09-04 13:33:29 +03:00
|
|
|
const loaded = once(ipcMain, 'loaded-iframe-custom-protocol');
|
2023-02-13 10:48:30 +03:00
|
|
|
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'iframe-protocol.html'));
|
2023-09-04 13:33:29 +03:00
|
|
|
await loaded;
|
2022-08-31 11:08:11 +03:00
|
|
|
});
|
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends object as response', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(fileContent));
|
|
|
|
});
|
2022-08-31 11:08:11 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('can send normal file', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: normalPath }));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(String(normalContent));
|
2019-06-21 19:23:57 +03:00
|
|
|
});
|
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('fails when sending unexist-file', async () => {
|
|
|
|
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: fakeFilePath }));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('fails when sending unsupported content', async () => {
|
|
|
|
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
for (const [registerHttpProtocol, name] of [
|
|
|
|
[protocol.registerHttpProtocol, 'protocol.registerHttpProtocol'] as const,
|
|
|
|
[(protocol as any).registerProtocol as typeof protocol.registerHttpProtocol, 'protocol.registerProtocol'] as const
|
|
|
|
]) {
|
|
|
|
describe(name, () => {
|
|
|
|
it('sends url as response', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
expect(req.headers.accept).to.not.equal('');
|
|
|
|
res.end(text);
|
|
|
|
server.close();
|
|
|
|
});
|
2023-02-20 14:30:57 +03:00
|
|
|
const { url } = await listen(server);
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url }));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('fails when sending invalid url', async () => {
|
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('fails when sending unsupported content', async () => {
|
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
|
|
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('works when target URL redirects', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === '/serverRedirect') {
|
|
|
|
res.statusCode = 301;
|
|
|
|
res.setHeader('Location', `http://${req.rawHeaders[1]}`);
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
res.end(text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
after(() => server.close());
|
2023-02-20 14:30:57 +03:00
|
|
|
const { port } = await listen(server);
|
2023-02-13 10:48:30 +03:00
|
|
|
const url = `${protocolName}://fake-host`;
|
|
|
|
const redirectURL = `http://127.0.0.1:${port}/serverRedirect`;
|
|
|
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: redirectURL }));
|
2016-05-29 11:13:26 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(url);
|
|
|
|
expect(r.data).to.equal(text);
|
2016-05-29 11:13:26 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('can access request headers', (done) => {
|
|
|
|
protocol.registerHttpProtocol(protocolName, (request) => {
|
|
|
|
try {
|
|
|
|
expect(request).to.have.property('headers');
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ajax(protocolName + '://fake-host').catch(() => {});
|
2018-10-30 21:37:49 +03:00
|
|
|
});
|
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
for (const [registerStreamProtocol, name] of [
|
|
|
|
[protocol.registerStreamProtocol, 'protocol.registerStreamProtocol'] as const,
|
|
|
|
[(protocol as any).registerProtocol as typeof protocol.registerStreamProtocol, 'protocol.registerProtocol'] as const
|
|
|
|
]) {
|
|
|
|
describe(name, () => {
|
|
|
|
it('sends Stream as response', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback(getStream()));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends object as response', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({ data: getStream() }));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.status).to.equal(200);
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends custom response headers', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
|
|
|
data: getStream(3),
|
|
|
|
headers: {
|
|
|
|
'x-electron': ['a', 'b']
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.equal(text);
|
|
|
|
expect(r.status).to.equal(200);
|
|
|
|
expect(r.headers).to.have.property('x-electron', 'a, b');
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('sends custom status code', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
|
|
|
statusCode: 204,
|
|
|
|
data: null as any
|
|
|
|
}));
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.be.empty('data');
|
|
|
|
expect(r.status).to.equal(204);
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('receives request headers', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
headers: {
|
|
|
|
'content-type': 'application/json'
|
|
|
|
},
|
|
|
|
data: getStream(5, JSON.stringify(Object.assign({}, request.headers)))
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
|
|
|
expect(JSON.parse(r.data)['x-return-headers']).to.equal('yes');
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2018-10-25 03:34:20 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('returns response multiple response headers with the same name', async () => {
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
headers: {
|
|
|
|
header1: ['value1', 'value2'],
|
|
|
|
header2: 'value3'
|
|
|
|
},
|
|
|
|
data: getStream()
|
|
|
|
});
|
2018-10-25 03:34:20 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
// 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 ",".
|
|
|
|
expect(r.headers).to.have.property('header1', 'value1, value2');
|
|
|
|
expect(r.headers).to.have.property('header2', 'value3');
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-01-25 21:57:26 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('can handle large responses', async () => {
|
|
|
|
const data = Buffer.alloc(128 * 1024);
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
callback(getStream(data.length, data));
|
|
|
|
});
|
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.have.lengthOf(data.length);
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2020-01-13 21:28:21 +03:00
|
|
|
|
2023-02-13 10:48:30 +03:00
|
|
|
it('can handle a stream completing while writing', async () => {
|
|
|
|
function dumbPassthrough () {
|
|
|
|
return new stream.Transform({
|
|
|
|
async transform (chunk, encoding, cb) {
|
|
|
|
cb(null, chunk);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
registerStreamProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
statusCode: 200,
|
|
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
|
|
data: getStream(1024 * 1024, Buffer.alloc(1024 * 1024 * 2)).pipe(dumbPassthrough())
|
|
|
|
});
|
2020-01-13 21:28:21 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
const r = await ajax(protocolName + '://fake-host');
|
|
|
|
expect(r.data).to.have.lengthOf(1024 * 1024 * 2);
|
2020-01-13 21:28:21 +03:00
|
|
|
});
|
2020-06-11 19:55:59 +03:00
|
|
|
|
2023-02-13 10:48:30 +03: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()
|
|
|
|
});
|
2020-06-11 19:55:59 +03:00
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
const hasEndedPromise = once(events, 'end');
|
2023-02-13 10:48:30 +03:00
|
|
|
ajax(protocolName + '://fake-host').catch(() => {});
|
|
|
|
await hasEndedPromise;
|
2020-06-11 19:55:59 +03:00
|
|
|
});
|
2020-07-21 03:51:38 +03:00
|
|
|
|
2023-02-13 10:48:30 +03: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');
|
2020-07-21 03:51:38 +03:00
|
|
|
});
|
|
|
|
|
2023-02-24 02:53:53 +03:00
|
|
|
const hasRespondedPromise = once(events, 'respond');
|
|
|
|
const hasClosedPromise = once(events, 'close');
|
2023-02-13 10:48:30 +03:00
|
|
|
ajax(protocolName + '://fake-host').catch(() => {});
|
|
|
|
await hasRespondedPromise;
|
|
|
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
|
|
|
|
await hasClosedPromise;
|
|
|
|
});
|
2020-07-21 03:51:38 +03:00
|
|
|
});
|
2023-02-13 10:48:30 +03:00
|
|
|
}
|
2017-11-10 15:35:32 +03:00
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
describe('protocol.isProtocolRegistered', () => {
|
|
|
|
it('returns false when scheme is not registered', () => {
|
|
|
|
const result = protocol.isProtocolRegistered('no-exist');
|
2019-06-28 00:20:29 +03:00
|
|
|
expect(result).to.be.false('no-exist: is handled');
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
it('returns true for custom protocol', () => {
|
|
|
|
registerStringProtocol(protocolName, (request, callback) => callback(''));
|
|
|
|
const result = protocol.isProtocolRegistered(protocolName);
|
2019-06-28 00:20:29 +03:00
|
|
|
expect(result).to.be.true('custom protocol is handled');
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2020-06-02 19:46:18 +03:00
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
describe('protocol.isProtocolIntercepted', () => {
|
|
|
|
it('returns true for intercepted protocol', () => {
|
|
|
|
interceptStringProtocol('http', (request, callback) => callback(''));
|
|
|
|
const result = protocol.isProtocolIntercepted('http');
|
2019-06-28 00:20:29 +03:00
|
|
|
expect(result).to.be.true('intercepted protocol is handled');
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.intercept(Any)Protocol', () => {
|
2020-06-02 19:46:18 +03: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);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('does not crash when handler is called twice', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2016-01-12 05:40:23 +03:00
|
|
|
try {
|
2016-03-25 23:03:49 +03:00
|
|
|
callback(text);
|
2020-06-02 19:46:18 +03:00
|
|
|
callback('');
|
2023-07-27 17:53:45 +03:00
|
|
|
} catch {
|
2016-01-19 22:31:47 +03:00
|
|
|
// Ignore error
|
2016-01-12 05:40:23 +03:00
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.be.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('sends error when callback is called with nothing', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback: any) => callback());
|
2022-01-24 12:34:23 +03:00
|
|
|
await expect(ajax('http://fake-host')).to.be.eventually.rejected();
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.interceptStringProtocol', () => {
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback) => callback(text));
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can set content-type', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2016-02-17 04:39:11 +03:00
|
|
|
callback({
|
2016-01-12 05:40:23 +03:00
|
|
|
mimeType: 'application/json',
|
|
|
|
data: '{"value": 1}'
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2022-01-24 12:34:23 +03:00
|
|
|
expect(JSON.parse(r.data)).to.have.property('value').that.is.equal(1);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2019-10-11 21:54:50 +03:00
|
|
|
it('can set content-type with charset', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
2019-10-11 21:54:50 +03:00
|
|
|
callback({
|
|
|
|
mimeType: 'application/json; charset=UTF-8',
|
|
|
|
data: '{"value": 1}'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const r = await ajax('http://fake-host');
|
2022-01-24 12:34:23 +03:00
|
|
|
expect(JSON.parse(r.data)).to.have.property('value').that.is.equal(1);
|
2019-10-11 21:54:50 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStringProtocol('http', (request, callback) => {
|
|
|
|
const uploadData = request.uploadData![0].bytes.toString();
|
2018-09-13 19:10:51 +03:00
|
|
|
callback({ data: uploadData });
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2022-01-24 12:34:23 +03:00
|
|
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
2019-05-20 20:04:18 +03:00
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.interceptBufferProtocol', () => {
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptBufferProtocol('http', (request, callback) => callback(Buffer.from(text)));
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptBufferProtocol('http', (request, callback) => {
|
|
|
|
const uploadData = request.uploadData![0].bytes;
|
2016-03-25 23:03:49 +03:00
|
|
|
callback(uploadData);
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2022-01-24 12:34:23 +03:00
|
|
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
|
|
|
expect(qs.parse(r.data)).to.deep.equal({ name: 'post test', type: 'string' });
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.interceptHttpProtocol', () => {
|
2019-05-07 05:33:05 +03: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 03:51:44 +03:00
|
|
|
xit('can send POST request', async () => {
|
2017-10-26 23:30:07 +03:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
let body = '';
|
|
|
|
req.on('data', (chunk) => {
|
2016-03-25 23:03:49 +03:00
|
|
|
body += chunk;
|
|
|
|
});
|
2017-10-26 23:30:07 +03:00
|
|
|
req.on('end', () => {
|
2016-03-25 23:03:49 +03:00
|
|
|
res.end(body);
|
|
|
|
});
|
|
|
|
server.close();
|
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
after(() => server.close());
|
2023-02-20 14:30:57 +03:00
|
|
|
const { url } = await listen(server);
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptHttpProtocol('http', (request, callback) => {
|
|
|
|
const data: Electron.ProtocolResponse = {
|
2019-05-10 03:51:44 +03:00
|
|
|
url: url,
|
|
|
|
method: 'POST',
|
|
|
|
uploadData: {
|
|
|
|
contentType: 'application/x-www-form-urlencoded',
|
2020-06-02 19:46:18 +03:00
|
|
|
data: request.uploadData![0].bytes
|
2019-05-10 03:51:44 +03:00
|
|
|
},
|
2020-06-02 19:46:18 +03:00
|
|
|
session: undefined
|
2016-03-25 23:03:49 +03:00
|
|
|
};
|
2019-05-10 03:51:44 +03:00
|
|
|
callback(data);
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
2019-05-20 20:04:18 +03:00
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-11-27 22:07:50 +03:00
|
|
|
|
2019-08-20 14:46:20 +03:00
|
|
|
it('can use custom session', async () => {
|
2018-09-13 19:10:51 +03:00
|
|
|
const customSession = session.fromPartition('custom-ses', { cache: false });
|
2017-10-26 23:30:07 +03:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(details.url).to.equal('http://fake-host/');
|
2018-09-13 19:10:51 +03:00
|
|
|
callback({ cancel: true });
|
2016-11-27 22:07:50 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
after(() => customSession.webRequest.onBeforeRequest(null));
|
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptHttpProtocol('http', (request, callback) => {
|
2016-11-27 22:07:50 +03:00
|
|
|
callback({
|
|
|
|
url: request.url,
|
|
|
|
session: customSession
|
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
|
|
|
await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error);
|
2016-11-27 22:07:50 +03:00
|
|
|
});
|
2018-10-30 21:37:49 +03:00
|
|
|
|
|
|
|
it('can access request headers', (done) => {
|
2019-06-28 00:20:29 +03:00
|
|
|
protocol.interceptHttpProtocol('http', (request) => {
|
2020-07-01 01:10:36 +03:00
|
|
|
try {
|
|
|
|
expect(request).to.have.property('headers');
|
|
|
|
done();
|
|
|
|
} catch (e) {
|
|
|
|
done(e);
|
|
|
|
}
|
2018-10-30 21:37:49 +03:00
|
|
|
});
|
2022-01-24 12:34:23 +03:00
|
|
|
ajax('http://fake-host').catch(() => {});
|
2018-10-30 21:37:49 +03:00
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2017-11-10 15:35:32 +03:00
|
|
|
describe('protocol.interceptStreamProtocol', () => {
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can intercept http protocol', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStreamProtocol('http', (request, callback) => callback(getStream()));
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal(text);
|
2017-11-10 15:35:32 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can receive post data', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
|
|
|
callback(getStream(3, request.uploadData![0].bytes.toString()));
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2022-01-24 12:34:23 +03:00
|
|
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
2019-05-20 20:04:18 +03:00
|
|
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
2017-11-10 15:35:32 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can execute redirects', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
2017-11-10 15:35:32 +03:00
|
|
|
if (request.url.indexOf('http://fake-host') === 0) {
|
2023-02-24 02:53:53 +03:00
|
|
|
setTimeout(300).then(() => {
|
2017-11-10 15:35:32 +03:00
|
|
|
callback({
|
2020-06-02 19:46:18 +03:00
|
|
|
data: '',
|
2017-11-10 15:35:32 +03:00
|
|
|
statusCode: 302,
|
|
|
|
headers: {
|
|
|
|
Location: 'http://fake-redirect'
|
|
|
|
}
|
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
} else {
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(request.url.indexOf('http://fake-redirect')).to.equal(0);
|
2017-11-10 15:35:32 +03:00
|
|
|
callback(getStream(1, 'redirect'));
|
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
const r = await ajax('http://fake-host');
|
2019-05-20 20:04:18 +03:00
|
|
|
expect(r.data).to.equal('redirect');
|
2017-11-10 15:35:32 +03:00
|
|
|
});
|
2021-01-19 13:06:14 +03:00
|
|
|
|
|
|
|
it('should discard post data after redirection', async () => {
|
|
|
|
interceptStreamProtocol('http', (request, callback) => {
|
|
|
|
if (request.url.indexOf('http://fake-host') === 0) {
|
2023-02-24 02:53:53 +03:00
|
|
|
setTimeout(300).then(() => {
|
2021-01-19 13:06:14 +03:00
|
|
|
callback({
|
|
|
|
statusCode: 302,
|
|
|
|
headers: {
|
|
|
|
Location: 'http://fake-redirect'
|
|
|
|
}
|
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
});
|
2021-01-19 13:06:14 +03:00
|
|
|
} 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');
|
|
|
|
});
|
2017-11-10 15:35:32 +03:00
|
|
|
});
|
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
describe('protocol.uninterceptProtocol', () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
it('returns false when scheme does not exist', () => {
|
|
|
|
expect(uninterceptProtocol('not-exist')).to.equal(false);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
|
2020-06-02 19:46:18 +03:00
|
|
|
it('returns false when scheme is not intercepted', () => {
|
|
|
|
expect(uninterceptProtocol('http')).to.equal(false);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
2016-05-05 21:34:16 +03:00
|
|
|
|
2020-04-01 05:33:16 +03:00
|
|
|
describe('protocol.registerSchemeAsPrivileged', () => {
|
2021-04-09 10:09:17 +03:00
|
|
|
it('does not crash on exit', async () => {
|
2020-04-01 05:33:16 +03: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 03:16:17 +03:00
|
|
|
appProcess.stdout.on('data', data => { process.stdout.write(data); stdout += data; });
|
|
|
|
appProcess.stderr.on('data', data => { process.stderr.write(data); stderr += data; });
|
2023-02-24 02:53:53 +03:00
|
|
|
const [code] = await once(appProcess, 'exit');
|
2020-04-01 05:33:16 +03:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-23 18:16:53 +03:00
|
|
|
describe('protocol.registerSchemesAsPrivileged allowServiceWorkers', () => {
|
|
|
|
protocol.registerStringProtocol(serviceWorkerScheme, (request, cb) => {
|
|
|
|
if (request.url.endsWith('.js')) {
|
|
|
|
cb({
|
|
|
|
mimeType: 'text/javascript',
|
|
|
|
charset: 'utf-8',
|
|
|
|
data: 'console.log("Loaded")'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb({
|
|
|
|
mimeType: 'text/html',
|
|
|
|
charset: 'utf-8',
|
|
|
|
data: '<!DOCTYPE html>'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
after(() => protocol.unregisterProtocol(serviceWorkerScheme));
|
|
|
|
|
|
|
|
it('should fail when registering invalid service worker', async () => {
|
|
|
|
await contents.loadURL(`${serviceWorkerScheme}://${v4()}.com`);
|
|
|
|
await expect(contents.executeJavaScript(`navigator.serviceWorker.register('${v4()}.notjs', {scope: './'})`)).to.be.rejected();
|
|
|
|
});
|
|
|
|
|
2022-05-23 10:42:37 +03:00
|
|
|
it('should be able to register service worker for custom scheme', async () => {
|
2021-03-23 18:16:53 +03:00
|
|
|
await contents.loadURL(`${serviceWorkerScheme}://${v4()}.com`);
|
|
|
|
await contents.executeJavaScript(`navigator.serviceWorker.register('${v4()}.js', {scope: './'})`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-15 04:55:08 +03:00
|
|
|
describe('protocol.registerSchemesAsPrivileged standard', () => {
|
2017-10-26 23:30:07 +03:00
|
|
|
const origin = `${standardScheme}://fake-host`;
|
|
|
|
const imageURL = `${origin}/test.png`;
|
2019-06-28 00:20:29 +03:00
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'b.html');
|
2016-05-05 21:34:16 +03:00
|
|
|
const fileContent = '<img src="/test.png" />';
|
2023-02-19 12:24:24 +03:00
|
|
|
let w: BrowserWindow;
|
2016-05-05 21:34:16 +03:00
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
beforeEach(() => {
|
2019-01-07 22:19:27 +03:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2021-06-15 04:55:08 +03:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-01-07 22:19:27 +03:00
|
|
|
}
|
|
|
|
});
|
2016-05-05 21:34:16 +03:00
|
|
|
});
|
|
|
|
|
2019-06-28 00:20:29 +03:00
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w);
|
2020-06-02 19:46:18 +03:00
|
|
|
unregisterProtocol(standardScheme);
|
2019-06-28 00:20:29 +03:00
|
|
|
w = null as unknown as BrowserWindow;
|
2016-05-05 21:34:16 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('resolves relative resources', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerFileProtocol(standardScheme, (request, callback) => {
|
2016-05-05 21:34:16 +03:00
|
|
|
if (request.url === imageURL) {
|
2020-06-02 19:46:18 +03:00
|
|
|
callback('');
|
2016-05-05 21:34:16 +03:00
|
|
|
} else {
|
|
|
|
callback(filePath);
|
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
await w.loadURL(origin);
|
2016-05-05 21:34:16 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('resolves absolute resources', async () => {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => {
|
2016-05-05 21:34:16 +03:00
|
|
|
if (request.url === imageURL) {
|
2020-06-02 19:46:18 +03:00
|
|
|
callback('');
|
2016-05-05 21:34:16 +03:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
data: fileContent,
|
|
|
|
mimeType: 'text/html'
|
|
|
|
});
|
|
|
|
}
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-05-10 03:51:44 +03:00
|
|
|
await w.loadURL(origin);
|
2016-06-29 19:37:10 +03:00
|
|
|
});
|
2016-06-08 08:44:30 +03:00
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('can have fetch working in it', async () => {
|
2023-03-27 20:00:55 +03:00
|
|
|
const requestReceived = deferPromise();
|
2019-06-28 00:20:29 +03:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.end();
|
|
|
|
server.close();
|
|
|
|
requestReceived.resolve();
|
|
|
|
});
|
2023-02-20 14:30:57 +03:00
|
|
|
const { url } = await listen(server);
|
|
|
|
const content = `<script>fetch(${JSON.stringify(url)})</script>`;
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => callback({ data: content, mimeType: 'text/html' }));
|
2019-05-10 03:51:44 +03:00
|
|
|
await w.loadURL(origin);
|
2019-06-28 00:20:29 +03:00
|
|
|
await requestReceived;
|
2016-05-05 21:34:16 +03:00
|
|
|
});
|
2016-08-24 04:23:14 +03:00
|
|
|
|
2023-04-04 16:48:51 +03:00
|
|
|
it('can access files through the FileSystem API', (done) => {
|
2019-06-28 00:20:29 +03:00
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'filesystem.html');
|
2020-06-02 19:46:18 +03:00
|
|
|
protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath }));
|
|
|
|
w.loadURL(origin);
|
2016-08-24 04:23:14 +03:00
|
|
|
ipcMain.once('file-system-error', (event, err) => done(err));
|
|
|
|
ipcMain.once('file-system-write-end', () => done());
|
|
|
|
});
|
2016-11-14 19:20:04 +03:00
|
|
|
|
2017-10-26 23:30:07 +03:00
|
|
|
it('registers secure, when {secure: true}', (done) => {
|
2019-06-28 00:20:29 +03:00
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'cache-storage.html');
|
2016-11-14 19:20:04 +03:00
|
|
|
ipcMain.once('success', () => done());
|
|
|
|
ipcMain.once('failure', (event, err) => done(err));
|
2020-06-02 19:46:18 +03:00
|
|
|
protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath }));
|
|
|
|
w.loadURL(origin);
|
2016-11-14 19:20:04 +03:00
|
|
|
});
|
2016-05-05 21:34:16 +03:00
|
|
|
});
|
2019-01-29 10:11:01 +03:00
|
|
|
|
2021-06-15 04:55:08 +03:00
|
|
|
describe('protocol.registerSchemesAsPrivileged cors-fetch', function () {
|
2023-02-19 12:24:24 +03:00
|
|
|
let w: BrowserWindow;
|
2019-06-28 00:20:29 +03:00
|
|
|
beforeEach(async () => {
|
2019-11-01 23:37:02 +03:00
|
|
|
w = new BrowserWindow({ show: false });
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2019-06-28 00:20:29 +03:00
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
2020-06-02 19:46:18 +03:00
|
|
|
for (const scheme of [standardScheme, 'cors', 'no-cors', 'no-fetch']) {
|
|
|
|
protocol.unregisterProtocol(scheme);
|
|
|
|
}
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('supports fetch api by default', async () => {
|
2019-06-28 00:20:29 +03: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 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('allows CORS requests by default', async () => {
|
2019-06-28 00:20:29 +03:00
|
|
|
await allowsCORSRequests('cors', 200, new RegExp(''), () => {
|
2019-11-01 23:37:02 +03:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-01-29 10:11:01 +03:00
|
|
|
fetch('cors://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status);
|
2019-11-01 23:37:02 +03:00
|
|
|
}).catch(function () {
|
2019-01-29 10:11:01 +03:00
|
|
|
ipcRenderer.send('response', 'failed');
|
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2023-04-04 16:48:51 +03:00
|
|
|
// DISABLED-FIXME: Figure out why this test is failing
|
|
|
|
it('disallows CORS and fetch requests when only supportFetchAPI is specified', async () => {
|
2019-06-28 00:20:29 +03:00
|
|
|
await allowsCORSRequests('no-cors', ['failed xhr', 'failed fetch'], /has been blocked by CORS policy/, () => {
|
2019-11-01 23:37:02 +03:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-06-28 00:20:29 +03:00
|
|
|
Promise.all([
|
|
|
|
new Promise(resolve => {
|
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.onload = () => resolve('loaded xhr');
|
|
|
|
req.onerror = () => resolve('failed xhr');
|
|
|
|
req.open('GET', 'no-cors://myhost');
|
|
|
|
req.send();
|
|
|
|
}),
|
|
|
|
fetch('no-cors://myhost')
|
|
|
|
.then(() => 'loaded fetch')
|
|
|
|
.catch(() => 'failed fetch')
|
|
|
|
]).then(([xhr, fetch]) => {
|
|
|
|
ipcRenderer.send('response', [xhr, fetch]);
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2019-05-10 03:51:44 +03:00
|
|
|
it('allows CORS, but disallows fetch requests, when specified', async () => {
|
2019-06-28 00:20:29 +03:00
|
|
|
await allowsCORSRequests('no-fetch', ['loaded xhr', 'failed fetch'], /Fetch API cannot load/, () => {
|
2019-11-01 23:37:02 +03:00
|
|
|
const { ipcRenderer } = require('electron');
|
2019-06-28 00:20:29 +03:00
|
|
|
Promise.all([
|
|
|
|
new Promise(resolve => {
|
|
|
|
const req = new XMLHttpRequest();
|
|
|
|
req.onload = () => resolve('loaded xhr');
|
|
|
|
req.onerror = () => resolve('failed xhr');
|
|
|
|
req.open('GET', 'no-fetch://myhost');
|
|
|
|
req.send();
|
|
|
|
}),
|
|
|
|
fetch('no-fetch://myhost')
|
|
|
|
.then(() => 'loaded fetch')
|
|
|
|
.catch(() => 'failed fetch')
|
|
|
|
]).then(([xhr, fetch]) => {
|
|
|
|
ipcRenderer.send('response', [xhr, fetch]);
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
2019-01-29 10:11:01 +03:00
|
|
|
});
|
|
|
|
|
2019-06-28 00:20:29 +03:00
|
|
|
async function allowsCORSRequests (corsScheme: string, expected: any, expectedConsole: RegExp, content: Function) {
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(standardScheme, (request, callback) => {
|
2019-06-28 00:20:29 +03:00
|
|
|
callback({ data: `<script>(${content})()</script>`, mimeType: 'text/html' });
|
2019-05-10 03:51:44 +03:00
|
|
|
});
|
2020-06-02 19:46:18 +03:00
|
|
|
registerStringProtocol(corsScheme, (request, callback) => {
|
2019-05-10 03:51:44 +03:00
|
|
|
callback('');
|
|
|
|
});
|
2019-01-29 10:11:01 +03:00
|
|
|
|
2023-02-16 17:41:41 +03:00
|
|
|
const newContents = (webContents as typeof ElectronInternal.WebContents).create({
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
});
|
2019-06-28 00:20:29 +03:00
|
|
|
const consoleMessages: string[] = [];
|
2019-11-01 23:37:02 +03:00
|
|
|
newContents.on('console-message', (e, level, message) => consoleMessages.push(message));
|
2019-06-28 00:20:29 +03:00
|
|
|
try {
|
|
|
|
newContents.loadURL(standardScheme + '://fake-host');
|
2023-02-24 02:53:53 +03:00
|
|
|
const [, response] = await once(ipcMain, 'response');
|
2019-06-28 00:20:29 +03:00
|
|
|
expect(response).to.deep.equal(expected);
|
|
|
|
expect(consoleMessages.join('\n')).to.match(expectedConsole);
|
|
|
|
} finally {
|
|
|
|
// This is called in a timeout to avoid a crash that happens when
|
|
|
|
// calling destroy() in a microtask.
|
2023-02-24 02:53:53 +03:00
|
|
|
setTimeout().then(() => {
|
2022-12-15 00:07:38 +03:00
|
|
|
newContents.destroy();
|
2019-06-28 00:20:29 +03:00
|
|
|
});
|
|
|
|
}
|
2019-01-29 10:11:01 +03:00
|
|
|
}
|
|
|
|
});
|
2020-06-08 19:49:36 +03: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');
|
2023-02-19 12:24:24 +03:00
|
|
|
let w: BrowserWindow;
|
2020-06-08 19:49:36 +03:00
|
|
|
|
|
|
|
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 22:45:29 +03:00
|
|
|
beforeEach(async function () {
|
2020-06-08 19:49:36 +03:00
|
|
|
w = new BrowserWindow({ show: false });
|
2021-01-07 22:45:29 +03: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 19:49:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
|
|
|
await protocol.unregisterProtocol(standardScheme);
|
|
|
|
await protocol.unregisterProtocol('stream');
|
|
|
|
});
|
|
|
|
|
2020-10-06 03:53:13 +03:00
|
|
|
it('successfully plays videos when content is buffered (stream: false)', async () => {
|
|
|
|
await streamsResponses(standardScheme, 'play');
|
2020-06-08 19:49:36 +03:00
|
|
|
});
|
|
|
|
|
2020-10-06 03:53:13 +03:00
|
|
|
it('successfully plays videos when streaming content (stream: true)', async () => {
|
2020-06-08 19:49:36 +03: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);
|
|
|
|
|
2023-02-16 17:41:41 +03:00
|
|
|
const newContents = (webContents as typeof ElectronInternal.WebContents).create({
|
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
|
|
|
});
|
|
|
|
|
2020-06-08 19:49:36 +03:00
|
|
|
try {
|
|
|
|
newContents.loadURL(testingScheme + '://fake-host');
|
2023-02-24 02:53:53 +03:00
|
|
|
const [, response] = await once(ipcMain, 'result');
|
2020-06-08 19:49:36 +03:00
|
|
|
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.
|
2023-02-24 02:53:53 +03:00
|
|
|
setTimeout().then(() => {
|
2022-12-15 00:07:38 +03:00
|
|
|
newContents.destroy();
|
2020-06-08 19:49:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-03-27 20:00:55 +03:00
|
|
|
|
2023-12-06 05:22:41 +03:00
|
|
|
describe('protocol.registerSchemesAsPrivileged codeCache', function () {
|
|
|
|
const temp = require('temp').track();
|
|
|
|
const appPath = path.join(fixturesPath, 'apps', 'refresh-page');
|
|
|
|
|
|
|
|
let w: BrowserWindow;
|
|
|
|
let codeCachePath: string;
|
|
|
|
beforeEach(async () => {
|
|
|
|
w = new BrowserWindow({ show: false });
|
|
|
|
codeCachePath = temp.path();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as BrowserWindow;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('code cache in custom protocol is disabled by default', async () => {
|
|
|
|
ChildProcess.spawnSync(process.execPath, [appPath, 'false', codeCachePath]);
|
|
|
|
expect(fs.readdirSync(path.join(codeCachePath, 'js')).length).to.equal(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('codeCache:true enables codeCache in custom protocol', async () => {
|
|
|
|
ChildProcess.spawnSync(process.execPath, [appPath, 'true', codeCachePath]);
|
|
|
|
expect(fs.readdirSync(path.join(codeCachePath, 'js')).length).to.above(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
describe('handle', () => {
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
it('receives requests to a custom scheme', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response('hello ' + req.url));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const resp = await net.fetch('test-scheme://foo');
|
|
|
|
expect(resp.status).to.equal(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be unhandled', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response('hello ' + req.url));
|
|
|
|
defer(() => {
|
|
|
|
try {
|
|
|
|
// In case of failure, make sure we unhandle. But we should succeed
|
|
|
|
// :)
|
|
|
|
protocol.unhandle('test-scheme');
|
2023-07-27 17:53:45 +03:00
|
|
|
} catch { /* ignore */ }
|
2023-03-27 20:00:55 +03:00
|
|
|
});
|
|
|
|
const resp1 = await net.fetch('test-scheme://foo');
|
|
|
|
expect(resp1.status).to.equal(200);
|
|
|
|
protocol.unhandle('test-scheme');
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.eventually.be.rejectedWith(/ERR_UNKNOWN_URL_SCHEME/);
|
|
|
|
});
|
|
|
|
|
2023-07-12 12:42:49 +03:00
|
|
|
it('receives requests to the existing https scheme', async () => {
|
2023-03-27 20:00:55 +03:00
|
|
|
protocol.handle('https', (req) => new Response('hello ' + req.url));
|
|
|
|
defer(() => { protocol.unhandle('https'); });
|
|
|
|
const body = await net.fetch('https://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('hello https://foo/');
|
|
|
|
});
|
|
|
|
|
2023-07-12 12:42:49 +03:00
|
|
|
it('receives requests to the existing file scheme', (done) => {
|
|
|
|
const filePath = path.join(__dirname, 'fixtures', 'pages', 'a.html');
|
|
|
|
|
|
|
|
protocol.handle('file', (req) => {
|
|
|
|
let file;
|
|
|
|
if (process.platform === 'win32') {
|
2023-09-07 09:50:14 +03:00
|
|
|
file = `file:///${filePath.replaceAll('\\', '/')}`;
|
2023-07-12 12:42:49 +03:00
|
|
|
} else {
|
|
|
|
file = `file://${filePath}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.url === file) done();
|
|
|
|
return new Response(req.url);
|
|
|
|
});
|
|
|
|
|
|
|
|
defer(() => { protocol.unhandle('file'); });
|
|
|
|
|
|
|
|
const w = new BrowserWindow();
|
|
|
|
w.loadFile(filePath);
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
it('receives requests to an existing scheme when navigating', async () => {
|
|
|
|
protocol.handle('https', (req) => new Response('hello ' + req.url));
|
|
|
|
defer(() => { protocol.unhandle('https'); });
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadURL('https://localhost');
|
|
|
|
expect(await w.webContents.executeJavaScript('document.body.textContent')).to.equal('hello https://localhost/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send buffer body', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(Buffer.from('hello ' + req.url)));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const body = await net.fetch('test-scheme://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('hello test-scheme://foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send stream body', async () => {
|
|
|
|
protocol.handle('test-scheme', () => new Response(getWebStream()));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const body = await net.fetch('test-scheme://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal(text);
|
|
|
|
});
|
|
|
|
|
2024-07-27 20:25:43 +03:00
|
|
|
it('calls destroy on aborted body stream', async () => {
|
|
|
|
const abortController = new AbortController();
|
|
|
|
|
|
|
|
class TestStream extends stream.Readable {
|
|
|
|
_read () {
|
|
|
|
this.push('infinite data');
|
|
|
|
|
|
|
|
// Abort the request that reads from this stream.
|
|
|
|
abortController.abort();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const body = new TestStream();
|
|
|
|
protocol.handle('test-scheme', () => {
|
|
|
|
return new Response(stream.Readable.toWeb(body) as ReadableStream<ArrayBufferView>);
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
|
|
|
|
const res = net.fetch('test-scheme://foo', {
|
|
|
|
signal: abortController.signal
|
|
|
|
});
|
|
|
|
await expect(res).to.be.rejectedWith('This operation was aborted');
|
|
|
|
await expect(once(body, 'end')).to.be.rejectedWith('The operation was aborted');
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
it('accepts urls with no hostname in non-standard schemes', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.url));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
{
|
|
|
|
const body = await net.fetch('test-scheme://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('test-scheme://foo');
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = await net.fetch('test-scheme:///foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('test-scheme:///foo');
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = await net.fetch('test-scheme://').then(r => r.text());
|
|
|
|
expect(body).to.equal('test-scheme://');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts urls with a port-like component in non-standard schemes', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.url));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
{
|
|
|
|
const body = await net.fetch('test-scheme://foo:30').then(r => r.text());
|
|
|
|
expect(body).to.equal('test-scheme://foo:30');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('normalizes urls in standard schemes', async () => {
|
|
|
|
// NB. 'app' is registered as a standard scheme in test setup.
|
|
|
|
protocol.handle('app', (req) => new Response(req.url));
|
|
|
|
defer(() => { protocol.unhandle('app'); });
|
|
|
|
{
|
|
|
|
const body = await net.fetch('app://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('app://foo/');
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const body = await net.fetch('app:///foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('app://foo/');
|
|
|
|
}
|
|
|
|
// NB. 'app' is registered with the default scheme type of 'host'.
|
|
|
|
{
|
|
|
|
const body = await net.fetch('app://foo:1234').then(r => r.text());
|
|
|
|
expect(body).to.equal('app://foo/');
|
|
|
|
}
|
|
|
|
await expect(net.fetch('app://')).to.be.rejectedWith('Invalid URL');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails on URLs with a username', async () => {
|
|
|
|
// NB. 'app' is registered as a standard scheme in test setup.
|
|
|
|
protocol.handle('http', (req) => new Response(req.url));
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
await expect(contents.loadURL('http://x@foo:1234')).to.be.rejectedWith(/ERR_UNEXPECTED/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('normalizes http urls', async () => {
|
|
|
|
protocol.handle('http', (req) => new Response(req.url));
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
{
|
|
|
|
const body = await net.fetch('http://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal('http://foo/');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can send errors', async () => {
|
|
|
|
protocol.handle('test-scheme', () => Response.error());
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.eventually.be.rejectedWith('net::ERR_FAILED');
|
|
|
|
});
|
|
|
|
|
2023-06-07 10:29:04 +03:00
|
|
|
it('handles invalid protocol response status', async () => {
|
|
|
|
protocol.handle('test-scheme', () => {
|
|
|
|
return { status: [] } as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invalid protocol response statusText', async () => {
|
|
|
|
protocol.handle('test-scheme', () => {
|
|
|
|
return { statusText: false } as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invalid protocol response header parameters', async () => {
|
|
|
|
protocol.handle('test-scheme', () => {
|
|
|
|
return { headers: false } as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles invalid protocol response body parameters', async () => {
|
|
|
|
protocol.handle('test-scheme', () => {
|
|
|
|
return { body: false } as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
it('handles a synchronous error in the handler', async () => {
|
|
|
|
protocol.handle('test-scheme', () => { throw new Error('test'); });
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles an asynchronous error in the handler', async () => {
|
|
|
|
protocol.handle('test-scheme', () => Promise.reject(new Error('rejected promise')));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly sets statusCode', async () => {
|
|
|
|
protocol.handle('test-scheme', () => new Response(null, { status: 201 }));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const resp = await net.fetch('test-scheme://foo');
|
|
|
|
expect(resp.status).to.equal(201);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly sets content-type and charset', async () => {
|
|
|
|
protocol.handle('test-scheme', () => new Response(null, { headers: { 'content-type': 'text/html; charset=testcharset' } }));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const resp = await net.fetch('test-scheme://foo');
|
|
|
|
expect(resp.headers.get('content-type')).to.equal('text/html; charset=testcharset');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can forward to http', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.end(text);
|
|
|
|
});
|
|
|
|
defer(() => { server.close(); });
|
|
|
|
const { url } = await listen(server);
|
|
|
|
|
|
|
|
protocol.handle('test-scheme', () => net.fetch(url));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const body = await net.fetch('test-scheme://foo').then(r => r.text());
|
|
|
|
expect(body).to.equal(text);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can forward an http request with headers', async () => {
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('foo', 'bar');
|
|
|
|
res.end(text);
|
|
|
|
});
|
|
|
|
defer(() => { server.close(); });
|
|
|
|
const { url } = await listen(server);
|
|
|
|
|
|
|
|
protocol.handle('test-scheme', (req) => net.fetch(url, { headers: req.headers }));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
|
|
|
|
const resp = await net.fetch('test-scheme://foo');
|
|
|
|
expect(resp.headers.get('foo')).to.equal('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can forward to file', async () => {
|
|
|
|
protocol.handle('test-scheme', () => net.fetch(url.pathToFileURL(path.join(__dirname, 'fixtures', 'hello.txt')).toString()));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
|
|
|
|
const body = await net.fetch('test-scheme://foo').then(r => r.text());
|
|
|
|
expect(body.trimEnd()).to.equal('hello world');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive simple request body', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const body = await net.fetch('test-scheme://foo', {
|
|
|
|
method: 'POST',
|
|
|
|
body: 'foobar'
|
|
|
|
}).then(r => r.text());
|
|
|
|
expect(body).to.equal('foobar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive stream request body', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
const body = await net.fetch('test-scheme://foo', {
|
|
|
|
method: 'POST',
|
|
|
|
body: getWebStream(),
|
|
|
|
duplex: 'half' // https://github.com/microsoft/TypeScript/issues/53157
|
|
|
|
} as any).then(r => r.text());
|
|
|
|
expect(body).to.equal(text);
|
|
|
|
});
|
|
|
|
|
2024-04-22 23:40:01 +03:00
|
|
|
it('can receive stream request body asynchronously', async () => {
|
|
|
|
let done: any;
|
|
|
|
const requestReceived: Promise<Buffer[]> = new Promise(resolve => { done = resolve; });
|
|
|
|
protocol.handle('http-like', async (req) => {
|
|
|
|
const chunks = [];
|
|
|
|
for await (const chunk of (req.body as any)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
done(chunks);
|
|
|
|
return new Response('ok');
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('http-like'); });
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
const expectedHashChunks = await w.webContents.executeJavaScript(`
|
|
|
|
const dataStream = () =>
|
|
|
|
new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
for (let i = 0; i < 10; i++) { controller.enqueue(Array(1024 * 128).fill(+i).join("\\n")); }
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
fetch(
|
|
|
|
new Request("http-like://host", {
|
|
|
|
method: "POST",
|
|
|
|
body: dataStream(),
|
|
|
|
duplex: "half",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
(async () => {
|
|
|
|
const chunks = []
|
|
|
|
for await (const chunk of dataStream()) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
}
|
|
|
|
return chunks;
|
|
|
|
})()
|
|
|
|
`);
|
|
|
|
const expectedHash = Buffer.from(await crypto.subtle.digest('SHA-256', Buffer.concat(expectedHashChunks))).toString('hex');
|
|
|
|
const body = Buffer.concat(await requestReceived);
|
|
|
|
const actualHash = Buffer.from(await crypto.subtle.digest('SHA-256', Buffer.from(body))).toString('hex');
|
|
|
|
expect(actualHash).to.equal(expectedHash);
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
it('can receive multi-part postData from loadURL', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await contents.loadURL('test-scheme://foo', { postData: [{ type: 'rawData', bytes: Buffer.from('a') }, { type: 'rawData', bytes: Buffer.from('b') }] });
|
|
|
|
expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal('ab');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive file postData from loadURL', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await contents.loadURL('test-scheme://foo', { postData: [{ type: 'file', filePath: path.join(fixturesPath, 'hello.txt'), length: 'hello world\n'.length, offset: 0, modificationTime: 0 }] });
|
|
|
|
expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal('hello world\n');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive file postData from a form', async () => {
|
|
|
|
protocol.handle('test-scheme', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('test-scheme'); });
|
|
|
|
await contents.loadURL('data:text/html,<form action="test-scheme://foo" method=POST enctype="multipart/form-data"><input name=foo type=file>');
|
|
|
|
const { debugger: dbg } = contents;
|
|
|
|
dbg.attach();
|
|
|
|
const { root } = await dbg.sendCommand('DOM.getDocument');
|
|
|
|
const { nodeId: fileInputNodeId } = await dbg.sendCommand('DOM.querySelector', { nodeId: root.nodeId, selector: 'input' });
|
|
|
|
await dbg.sendCommand('DOM.setFileInputFiles', {
|
|
|
|
nodeId: fileInputNodeId,
|
|
|
|
files: [
|
|
|
|
path.join(fixturesPath, 'hello.txt')
|
|
|
|
]
|
|
|
|
});
|
|
|
|
const navigated = once(contents, 'did-finish-load');
|
|
|
|
await contents.executeJavaScript('document.querySelector("form").submit()');
|
|
|
|
await navigated;
|
|
|
|
expect(await contents.executeJavaScript('document.documentElement.textContent')).to.match(/------WebKitFormBoundary.*\nContent-Disposition: form-data; name="foo"; filename="hello.txt"\nContent-Type: text\/plain\n\nhello world\n\n------WebKitFormBoundary.*--\n/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive streaming fetch upload', async () => {
|
|
|
|
protocol.handle('no-cors', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('no-cors'); });
|
|
|
|
await contents.loadURL('no-cors://foo');
|
|
|
|
const fetchBodyResult = await contents.executeJavaScript(`
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
controller.enqueue('hello world');
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
fetch(location.href, {method: 'POST', body: stream, duplex: 'half'}).then(x => x.text())
|
|
|
|
`);
|
|
|
|
expect(fetchBodyResult).to.equal('hello world');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive streaming fetch upload when a webRequest handler is present', async () => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest((details, cb) => {
|
|
|
|
console.log('webRequest', details.url, details.method);
|
|
|
|
cb({});
|
|
|
|
});
|
|
|
|
defer(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
protocol.handle('no-cors', (req) => {
|
|
|
|
console.log('handle', req.url, req.method);
|
|
|
|
return new Response(req.body);
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('no-cors'); });
|
|
|
|
await contents.loadURL('no-cors://foo');
|
|
|
|
const fetchBodyResult = await contents.executeJavaScript(`
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
controller.enqueue('hello world');
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
fetch(location.href, {method: 'POST', body: stream, duplex: 'half'}).then(x => x.text())
|
|
|
|
`);
|
|
|
|
expect(fetchBodyResult).to.equal('hello world');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can receive an error from streaming fetch upload', async () => {
|
|
|
|
protocol.handle('no-cors', (req) => new Response(req.body));
|
|
|
|
defer(() => { protocol.unhandle('no-cors'); });
|
|
|
|
await contents.loadURL('no-cors://foo');
|
|
|
|
const fetchBodyResult = await contents.executeJavaScript(`
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
controller.error('test')
|
|
|
|
},
|
|
|
|
});
|
|
|
|
fetch(location.href, {method: 'POST', body: stream, duplex: 'half'}).then(x => x.text()).catch(err => err)
|
|
|
|
`);
|
|
|
|
expect(fetchBodyResult).to.be.an.instanceOf(Error);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets an error from streaming fetch upload when the renderer dies', async () => {
|
|
|
|
let gotRequest: Function;
|
|
|
|
const receivedRequest = new Promise<Request>(resolve => { gotRequest = resolve; });
|
|
|
|
protocol.handle('no-cors', (req) => {
|
|
|
|
if (/fetch/.test(req.url)) gotRequest(req);
|
|
|
|
return new Response();
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('no-cors'); });
|
|
|
|
await contents.loadURL('no-cors://foo');
|
|
|
|
contents.executeJavaScript(`
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
window.controller = controller // no GC
|
|
|
|
},
|
|
|
|
});
|
|
|
|
fetch(location.href + '/fetch', {method: 'POST', body: stream, duplex: 'half'}).then(x => x.text()).catch(err => err)
|
|
|
|
`);
|
|
|
|
const req = await receivedRequest;
|
|
|
|
contents.destroy();
|
|
|
|
// Undo .destroy() for the next test
|
|
|
|
contents = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
|
|
|
|
await expect(req.body!.getReader().read()).to.eventually.be.rejectedWith('net::ERR_FAILED');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can bypass intercepeted protocol handlers', async () => {
|
|
|
|
protocol.handle('http', () => new Response('custom'));
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.end('default');
|
|
|
|
});
|
|
|
|
defer(() => server.close());
|
|
|
|
const { url } = await listen(server);
|
|
|
|
expect(await net.fetch(url, { bypassCustomProtocolHandlers: true }).then(r => r.text())).to.equal('default');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bypassing custom protocol handlers also bypasses new protocols', async () => {
|
|
|
|
protocol.handle('app', () => new Response('custom'));
|
|
|
|
defer(() => { protocol.unhandle('app'); });
|
|
|
|
await expect(net.fetch('app://foo', { bypassCustomProtocolHandlers: true })).to.be.rejectedWith('net::ERR_UNKNOWN_URL_SCHEME');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can forward to the original handler', async () => {
|
|
|
|
protocol.handle('http', (req) => net.fetch(req, { bypassCustomProtocolHandlers: true }));
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.end('hello');
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
const { url } = await listen(server);
|
|
|
|
await contents.loadURL(url);
|
|
|
|
expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal('hello');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports sniffing mime type', async () => {
|
|
|
|
protocol.handle('http', async (req) => {
|
|
|
|
return net.fetch(req, { bypassCustomProtocolHandlers: true });
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (/html/.test(req.url ?? '')) { res.end('<!doctype html><body>hi'); } else { res.end('hi'); }
|
|
|
|
});
|
|
|
|
const { url } = await listen(server);
|
|
|
|
defer(() => server.close());
|
|
|
|
|
|
|
|
{
|
|
|
|
await contents.loadURL(url);
|
|
|
|
const doc = await contents.executeJavaScript('document.documentElement.outerHTML');
|
|
|
|
expect(doc).to.match(/white-space: pre-wrap/);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
await contents.loadURL(url + '?html');
|
|
|
|
const doc = await contents.executeJavaScript('document.documentElement.outerHTML');
|
|
|
|
expect(doc).to.equal('<html><head></head><body>hi</body></html>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-16 22:29:29 +03:00
|
|
|
it('does not emit undefined chunks into the request body stream when uploading a stream', async () => {
|
|
|
|
protocol.handle('cors', async (request) => {
|
|
|
|
expect(request.body).to.be.an.instanceOf(webStream.ReadableStream);
|
|
|
|
for await (const value of request.body as webStream.ReadableStream<Uint8Array>) {
|
|
|
|
expect(value).to.not.be.undefined();
|
|
|
|
}
|
|
|
|
return new Response(undefined, { status: 200 });
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('cors'); });
|
|
|
|
|
|
|
|
await contents.loadFile(path.resolve(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
contents.on('console-message', (e, level, message) => console.log(message));
|
|
|
|
const ok = await contents.executeJavaScript(`(async () => {
|
|
|
|
function wait(milliseconds) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
|
|
}
|
|
|
|
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
await wait(4);
|
|
|
|
controller.enqueue('This ');
|
|
|
|
await wait(4);
|
|
|
|
controller.enqueue('is ');
|
|
|
|
await wait(4);
|
|
|
|
controller.enqueue('a ');
|
|
|
|
await wait(4);
|
|
|
|
controller.enqueue('slow ');
|
|
|
|
await wait(4);
|
|
|
|
controller.enqueue('request.');
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
}).pipeThrough(new TextEncoderStream());
|
|
|
|
return (await fetch('cors://url.invalid', { method: 'POST', body: stream, duplex: 'half' })).ok;
|
|
|
|
})()`);
|
|
|
|
expect(ok).to.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not emit undefined chunks into the request body stream when uploading a file', async () => {
|
|
|
|
protocol.handle('cors', async (request) => {
|
|
|
|
expect(request.body).to.be.an.instanceOf(webStream.ReadableStream);
|
|
|
|
for await (const value of request.body as webStream.ReadableStream<Uint8Array>) {
|
|
|
|
expect(value).to.not.be.undefined();
|
|
|
|
}
|
|
|
|
return new Response(undefined, { status: 200 });
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('cors'); });
|
|
|
|
|
|
|
|
await contents.loadFile(path.resolve(fixturesPath, 'pages', 'file-input.html'));
|
|
|
|
const { debugger: debug } = contents;
|
|
|
|
debug.attach();
|
|
|
|
try {
|
|
|
|
const { root: { nodeId } } = await debug.sendCommand('DOM.getDocument');
|
|
|
|
const { nodeId: inputNodeId } = await debug.sendCommand('DOM.querySelector', { nodeId, selector: 'input' });
|
|
|
|
await debug.sendCommand('DOM.setFileInputFiles', {
|
|
|
|
files: [path.join(fixturesPath, 'cat-spin.mp4')],
|
|
|
|
nodeId: inputNodeId
|
|
|
|
});
|
|
|
|
const ok = await contents.executeJavaScript(`(async () => {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("data", document.getElementById("file").files[0]);
|
|
|
|
return (await fetch('cors://url.invalid', { method: 'POST', body: formData })).ok;
|
|
|
|
})()`);
|
|
|
|
expect(ok).to.be.true();
|
|
|
|
} finally {
|
|
|
|
debug.detach();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('filters an illegal "origin: null" header', async () => {
|
|
|
|
protocol.handle('http', (req) => {
|
|
|
|
expect(new Headers(req.headers).get('origin')).to.not.equal('null');
|
|
|
|
return new Response();
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
|
|
|
|
const filePath = path.join(fixturesPath, 'pages', 'form-with-data.html');
|
|
|
|
await contents.loadFile(filePath);
|
|
|
|
|
|
|
|
const loadPromise = new Promise((resolve, reject) => {
|
|
|
|
contents.once('did-finish-load', resolve);
|
|
|
|
contents.once('did-fail-load', (_, errorCode, errorDescription) =>
|
|
|
|
reject(new Error(`did-fail-load: ${errorCode} ${errorDescription}. See AssertionError for details.`))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
await contents.executeJavaScript(`
|
|
|
|
const form = document.querySelector('form');
|
|
|
|
form.action = 'http://cors.invalid';
|
|
|
|
form.method = 'POST';
|
|
|
|
form.submit();
|
|
|
|
`);
|
|
|
|
await loadPromise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does forward Blob chunks', async () => {
|
|
|
|
// we register the protocol on a separate session to validate the assumption
|
|
|
|
// that `getBlobData()` indeed returns the blob data from a global variable
|
|
|
|
const s = session.fromPartition('protocol-handle-forwards-blob-chunks');
|
|
|
|
|
|
|
|
s.protocol.handle('cors', async (request) => {
|
|
|
|
expect(request.body).to.be.an.instanceOf(webStream.ReadableStream);
|
|
|
|
return new Response(
|
|
|
|
`hello to ${await streamConsumers.text(request.body as webStream.ReadableStream<Uint8Array>)}`,
|
|
|
|
{ status: 200 }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
defer(() => { s.protocol.unhandle('cors'); });
|
|
|
|
|
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { session: s } });
|
|
|
|
await w.webContents.loadFile(path.resolve(fixturesPath, 'pages', 'base-page.html'));
|
|
|
|
const response = await w.webContents.executeJavaScript(`(async () => {
|
|
|
|
const body = new Blob(["it's-a ", 'me! ', 'Mario!'], { type: 'text/plain' });
|
|
|
|
return await (await fetch('cors://url.invalid', { method: 'POST', body })).text();
|
|
|
|
})()`);
|
|
|
|
expect(response).to.be.string('hello to it\'s-a me! Mario!');
|
|
|
|
});
|
|
|
|
|
2023-03-27 20:00:55 +03:00
|
|
|
// TODO(nornagon): this test doesn't pass on Linux currently, investigate.
|
|
|
|
ifit(process.platform !== 'linux')('is fast', async () => {
|
|
|
|
// 128 MB of spaces.
|
|
|
|
const chunk = new Uint8Array(128 * 1024 * 1024);
|
|
|
|
chunk.fill(' '.charCodeAt(0));
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
// The sniffed mime type for the space-filled chunk will be
|
|
|
|
// text/plain, which chews up all its performance in the renderer
|
|
|
|
// trying to wrap lines. Setting content-type to text/html measures
|
|
|
|
// something closer to just the raw cost of getting the bytes over
|
|
|
|
// the wire.
|
|
|
|
res.setHeader('content-type', 'text/html');
|
|
|
|
res.end(chunk);
|
|
|
|
});
|
|
|
|
defer(() => server.close());
|
|
|
|
const { url } = await listen(server);
|
|
|
|
|
|
|
|
const rawTime = await (async () => {
|
|
|
|
await contents.loadURL(url); // warm
|
|
|
|
const begin = Date.now();
|
|
|
|
await contents.loadURL(url);
|
|
|
|
const end = Date.now();
|
|
|
|
return end - begin;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Fetching through an intercepted handler should not be too much slower
|
|
|
|
// than it would be if the protocol hadn't been intercepted.
|
|
|
|
|
|
|
|
protocol.handle('http', async (req) => {
|
|
|
|
return net.fetch(req, { bypassCustomProtocolHandlers: true });
|
|
|
|
});
|
|
|
|
defer(() => { protocol.unhandle('http'); });
|
|
|
|
|
|
|
|
const interceptedTime = await (async () => {
|
|
|
|
const begin = Date.now();
|
|
|
|
await contents.loadURL(url);
|
|
|
|
const end = Date.now();
|
|
|
|
return end - begin;
|
|
|
|
})();
|
2024-06-18 02:33:44 +03:00
|
|
|
expect(interceptedTime).to.be.lessThan(rawTime * 1.6);
|
2023-03-27 20:00:55 +03:00
|
|
|
});
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|