2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2020-09-29 16:03:33 +00:00
|
|
|
import { net, session, ClientRequest, BrowserWindow, ClientRequestConstructorOptions } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import * as http from 'http';
|
|
|
|
import * as url from 'url';
|
|
|
|
import { AddressInfo, Socket } from 'net';
|
|
|
|
import { emittedOnce } from './events-helpers';
|
2020-10-06 00:47:41 +00:00
|
|
|
import { defer, delay } from './spec-helpers';
|
2019-10-10 12:14:41 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const kOneKiloByte = 1024;
|
|
|
|
const kOneMegaByte = kOneKiloByte * kOneKiloByte;
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
function randomBuffer (size: number, start: number = 0, end: number = 255) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const range = 1 + end - start;
|
|
|
|
const buffer = Buffer.allocUnsafe(size);
|
2019-06-14 23:26:07 +00:00
|
|
|
for (let i = 0; i < size; ++i) {
|
2020-03-20 20:28:31 +00:00
|
|
|
buffer[i] = start + Math.floor(Math.random() * range);
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return buffer;
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function randomString (length: number) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0));
|
|
|
|
return buffer.toString();
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
async function getResponse (urlRequest: Electron.ClientRequest) {
|
|
|
|
return new Promise<Electron.IncomingMessage>((resolve, reject) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest.on('error', reject);
|
|
|
|
urlRequest.on('abort', reject);
|
|
|
|
urlRequest.on('response', (response) => resolve(response));
|
|
|
|
urlRequest.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function collectStreamBody (response: Electron.IncomingMessage | http.IncomingMessage) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return (await collectStreamBodyBuffer(response)).toString();
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function collectStreamBodyBuffer (response: Electron.IncomingMessage | http.IncomingMessage) {
|
|
|
|
return new Promise<Buffer>((resolve, reject) => {
|
|
|
|
response.on('error', reject);
|
2020-03-20 20:28:31 +00:00
|
|
|
(response as NodeJS.EventEmitter).on('aborted', reject);
|
|
|
|
const data: Buffer[] = [];
|
|
|
|
response.on('data', (chunk) => data.push(chunk));
|
2020-03-18 15:59:44 +00:00
|
|
|
response.on('end', (chunk?: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (chunk) data.push(chunk);
|
|
|
|
resolve(Buffer.concat(data));
|
|
|
|
});
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function respondNTimes (fn: http.RequestListener, n: number): Promise<string> {
|
2019-06-14 23:26:07 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
const server = http.createServer((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
fn(request, response);
|
2019-06-14 23:26:07 +00:00
|
|
|
// don't close if a redirect was returned
|
2020-03-18 23:35:23 +00:00
|
|
|
if ((response.statusCode < 300 || response.statusCode >= 399) && n <= 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
n--;
|
|
|
|
server.close();
|
2020-03-18 23:35:23 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`);
|
|
|
|
});
|
|
|
|
const sockets: Socket[] = [];
|
|
|
|
server.on('connection', s => sockets.push(s));
|
2020-06-09 18:42:53 +00:00
|
|
|
defer(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
server.close();
|
|
|
|
sockets.forEach(s => s.destroy());
|
|
|
|
});
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
function respondOnce (fn: http.RequestListener) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return respondNTimes(fn, 1);
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let routeFailure = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
|
|
|
|
respondNTimes.toRoutes = (routes: Record<string, http.RequestListener>, n: number) => {
|
|
|
|
return respondNTimes((request, response) => {
|
2020-03-20 15:12:18 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(routes, request.url || '')) {
|
2020-03-18 15:59:44 +00:00
|
|
|
(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await Promise.resolve(routes[request.url || ''](request, response));
|
2020-03-18 15:59:44 +00:00
|
|
|
})().catch((err) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
routeFailure = true;
|
|
|
|
console.error('Route handler failed, this is probably why your test failed', err);
|
|
|
|
response.statusCode = 500;
|
|
|
|
response.end();
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 500;
|
|
|
|
response.end();
|
|
|
|
expect.fail(`Unexpected URL: ${request.url}`);
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
}, n);
|
|
|
|
};
|
|
|
|
respondOnce.toRoutes = (routes: Record<string, http.RequestListener>) => respondNTimes.toRoutes(routes, 1);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
respondNTimes.toURL = (url: string, fn: http.RequestListener, n: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
return respondNTimes.toRoutes({ [url]: fn }, n);
|
|
|
|
};
|
|
|
|
respondOnce.toURL = (url: string, fn: http.RequestListener) => respondNTimes.toURL(url, fn, 1);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
respondNTimes.toSingleURL = (fn: http.RequestListener, n: number) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestUrl = '/requestUrl';
|
|
|
|
return respondNTimes.toURL(requestUrl, fn, n).then(url => `${url}${requestUrl}`);
|
|
|
|
};
|
|
|
|
respondOnce.toSingleURL = (fn: http.RequestListener) => respondNTimes.toSingleURL(fn, 1);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
describe('net module', () => {
|
2020-03-18 15:59:44 +00:00
|
|
|
beforeEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
routeFailure = false;
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
afterEach(async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
await session.defaultSession.clearCache();
|
2020-03-18 15:59:44 +00:00
|
|
|
if (routeFailure && this.test) {
|
|
|
|
if (!this.test.isFailed()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error('Failing this test due an unhandled error in the respondOnce route handler, check the logs above for the actual error');
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2019-06-14 23:26:07 +00:00
|
|
|
describe('HTTP basics', () => {
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to issue a basic GET request', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.method).to.equal('GET');
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to issue a basic POST request', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.method).to.equal('POST');
|
|
|
|
response.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should fetch correct data in a GET request', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const expectedBodyData = 'Hello World!';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.method).to.equal('GET');
|
|
|
|
response.end(expectedBodyData);
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
const body = await collectStreamBody(response);
|
|
|
|
expect(body).to.equal(expectedBodyData);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should post the correct data in a POST request', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bodyData = 'Hello World!';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const postedBodyData = await collectStreamBody(request);
|
|
|
|
expect(postedBodyData).to.equal(bodyData);
|
|
|
|
response.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
urlRequest.write(bodyData);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should support chunked encoding', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.chunkedEncoding = true;
|
|
|
|
expect(request.method).to.equal('POST');
|
|
|
|
expect(request.headers['transfer-encoding']).to.equal('chunked');
|
|
|
|
expect(request.headers['content-length']).to.equal(undefined);
|
2019-06-14 23:26:07 +00:00
|
|
|
request.on('data', (chunk: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.write(chunk);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
request.on('end', (chunk: Buffer) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end(chunk);
|
|
|
|
});
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: serverUrl
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let chunkIndex = 0;
|
|
|
|
const chunkCount = 100;
|
|
|
|
let sent = Buffer.alloc(0);
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest.chunkedEncoding = true;
|
2020-03-18 15:59:44 +00:00
|
|
|
while (chunkIndex < chunkCount) {
|
2020-03-20 20:28:31 +00:00
|
|
|
chunkIndex += 1;
|
|
|
|
const chunk = randomBuffer(kOneKiloByte);
|
|
|
|
sent = Buffer.concat([sent, chunk]);
|
|
|
|
urlRequest.write(chunk);
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
const received = await collectStreamBodyBuffer(response);
|
|
|
|
expect(sent.equals(received)).to.be.true();
|
|
|
|
expect(chunkIndex).to.be.equal(chunkCount);
|
|
|
|
});
|
2019-11-14 18:01:18 +00:00
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
for (const extraOptions of [{}, { credentials: 'include' }, { useSessionCookies: false, credentials: 'include' }] as ClientRequestConstructorOptions[]) {
|
|
|
|
describe(`authentication when ${JSON.stringify(extraOptions)}`, () => {
|
|
|
|
it('should emit the login event when 401', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
response.writeHead(200).end('ok');
|
|
|
|
});
|
|
|
|
let loginAuthInfo: Electron.AuthInfo;
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, ...extraOptions });
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
loginAuthInfo = authInfo;
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(loginAuthInfo!.realm).to.equal('Foo');
|
|
|
|
expect(loginAuthInfo!.scheme).to.equal('basic');
|
|
|
|
});
|
2019-11-14 18:01:18 +00:00
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
it('should receive 401 response when cancelling authentication', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' });
|
|
|
|
response.end('unauthenticated');
|
|
|
|
} else {
|
|
|
|
response.writeHead(200).end('ok');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, ...extraOptions });
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
const body = await collectStreamBody(response);
|
|
|
|
expect(response.statusCode).to.equal(401);
|
|
|
|
expect(body).to.equal('unauthenticated');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should share credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const serverUrl = await respondNTimes.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok');
|
|
|
|
}, 2);
|
|
|
|
const bw = new BrowserWindow({ show: false });
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await bw.loadURL(serverUrl);
|
|
|
|
bw.close();
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, ...extraOptions });
|
|
|
|
let logInCount = 0;
|
|
|
|
request.on('login', () => {
|
|
|
|
logInCount++;
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
expect(logInCount).to.equal(0, 'should not receive a login event, credentials should be cached');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should share proxy credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const proxyUrl = await respondNTimes((request, response) => {
|
|
|
|
if (!request.headers['proxy-authorization']) {
|
|
|
|
return response.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok');
|
|
|
|
}, 2);
|
|
|
|
const customSession = session.fromPartition(`net-proxy-test-${Math.random()}`);
|
|
|
|
await customSession.setProxy({ proxyRules: proxyUrl.replace('http://', ''), proxyBypassRules: '<-loopback>' });
|
|
|
|
const bw = new BrowserWindow({ show: false, webPreferences: { session: customSession } });
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await bw.loadURL('http://127.0.0.1:9999');
|
|
|
|
bw.close();
|
|
|
|
const request = net.request({ method: 'GET', url: 'http://127.0.0.1:9999', session: customSession, ...extraOptions });
|
|
|
|
let logInCount = 0;
|
|
|
|
request.on('login', () => {
|
|
|
|
logInCount++;
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
const body = await collectStreamBody(response);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(body).to.equal('ok');
|
|
|
|
expect(logInCount).to.equal(0, 'should not receive a login event, credentials should be cached');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should upload body when 401', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
response.writeHead(200);
|
|
|
|
request.on('data', (chunk) => response.write(chunk));
|
|
|
|
request.on('end', () => response.end());
|
|
|
|
});
|
|
|
|
const requestData = randomString(kOneKiloByte);
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, ...extraOptions });
|
|
|
|
request.on('login', (authInfo, cb) => {
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
request.write(requestData);
|
|
|
|
const response = await getResponse(request);
|
|
|
|
const responseData = await collectStreamBody(response);
|
|
|
|
expect(responseData).to.equal(requestData);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-09-29 16:03:33 +00:00
|
|
|
}
|
2019-11-14 18:01:18 +00:00
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
describe('authentication when {"credentials":"omit"}', () => {
|
|
|
|
it('should not emit the login event when 401', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
response.writeHead(200).end('ok');
|
|
|
|
});
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, credentials: 'omit' });
|
|
|
|
request.on('login', () => {
|
|
|
|
expect.fail('unexpected login event');
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
expect(response.statusCode).to.equal(401);
|
|
|
|
expect(response.headers['www-authenticate']).to.equal('Basic realm="Foo"');
|
|
|
|
});
|
2019-11-14 18:01:18 +00:00
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
it('should not share credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const serverUrl = await respondNTimes.toSingleURL((request, response) => {
|
|
|
|
if (!request.headers.authorization) {
|
|
|
|
return response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok');
|
|
|
|
}, 2);
|
|
|
|
const bw = new BrowserWindow({ show: false });
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await bw.loadURL(serverUrl);
|
|
|
|
bw.close();
|
|
|
|
const request = net.request({ method: 'GET', url: serverUrl, credentials: 'omit' });
|
|
|
|
request.on('login', () => {
|
|
|
|
expect.fail();
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
expect(response.statusCode).to.equal(401);
|
|
|
|
expect(response.headers['www-authenticate']).to.equal('Basic realm="Foo"');
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
it('should share proxy credentials with WebContents', async () => {
|
|
|
|
const [user, pass] = ['user', 'pass'];
|
|
|
|
const proxyUrl = await respondNTimes((request, response) => {
|
|
|
|
if (!request.headers['proxy-authorization']) {
|
|
|
|
return response.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Foo"' }).end();
|
|
|
|
}
|
|
|
|
return response.writeHead(200).end('ok');
|
|
|
|
}, 2);
|
|
|
|
const customSession = session.fromPartition(`net-proxy-test-${Math.random()}`);
|
|
|
|
await customSession.setProxy({ proxyRules: proxyUrl.replace('http://', ''), proxyBypassRules: '<-loopback>' });
|
|
|
|
const bw = new BrowserWindow({ show: false, webPreferences: { session: customSession } });
|
|
|
|
bw.webContents.on('login', (event, details, authInfo, cb) => {
|
|
|
|
event.preventDefault();
|
|
|
|
cb(user, pass);
|
|
|
|
});
|
|
|
|
await bw.loadURL('http://127.0.0.1:9999');
|
|
|
|
bw.close();
|
|
|
|
const request = net.request({ method: 'GET', url: 'http://127.0.0.1:9999', session: customSession, credentials: 'omit' });
|
|
|
|
request.on('login', () => {
|
|
|
|
expect.fail();
|
|
|
|
});
|
|
|
|
const response = await getResponse(request);
|
|
|
|
const body = await collectStreamBody(response);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(body).to.equal('ok');
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
describe('ClientRequest API', () => {
|
2020-03-18 15:59:44 +00:00
|
|
|
it('request/response objects should emit expected events', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bodyData = randomString(kOneKiloByte);
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end(bodyData);
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
// request close event
|
2020-03-20 20:28:31 +00:00
|
|
|
const closePromise = emittedOnce(urlRequest, 'close');
|
2020-03-18 15:59:44 +00:00
|
|
|
// request finish event
|
2020-03-20 20:28:31 +00:00
|
|
|
const finishPromise = emittedOnce(urlRequest, 'close');
|
2020-03-18 15:59:44 +00:00
|
|
|
// request "response" event
|
2020-03-20 20:28:31 +00:00
|
|
|
const response = await getResponse(urlRequest);
|
2020-03-18 15:59:44 +00:00
|
|
|
response.on('error', (error: Error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(error).to.be.an('Error');
|
|
|
|
});
|
|
|
|
const statusCode = response.statusCode;
|
|
|
|
expect(statusCode).to.equal(200);
|
2020-03-18 15:59:44 +00:00
|
|
|
// response data event
|
|
|
|
// respond end event
|
2020-03-20 20:28:31 +00:00
|
|
|
const body = await collectStreamBody(response);
|
|
|
|
expect(body).to.equal(bodyData);
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('error', (error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(error).to.be.an('Error');
|
|
|
|
});
|
|
|
|
await Promise.all([closePromise, finishPromise]);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to set a custom HTTP request header before first write', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue);
|
|
|
|
urlRequest.write('');
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should be able to set a non-string object as a header value', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Integer-Value';
|
|
|
|
const customHeaderValue = 900;
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue.toString());
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue as any);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue);
|
|
|
|
urlRequest.write('');
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName.toLowerCase())).to.equal(customHeaderValue);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-11-17 22:25:41 +00:00
|
|
|
it('should not change the case of header name', async () => {
|
|
|
|
const customHeaderName = 'X-Header-Name';
|
|
|
|
const customHeaderValue = 'value';
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue.toString());
|
|
|
|
expect(request.rawHeaders.includes(customHeaderName)).to.equal(true);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
urlRequest.write('');
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should not be able to set a custom HTTP request header after first write', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(undefined);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.write('');
|
2020-03-18 15:59:44 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
}).to.throw();
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(undefined);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to remove a custom HTTP request header before first write', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(undefined);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
urlRequest.removeHeader(customHeaderName);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(undefined);
|
|
|
|
urlRequest.write('');
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should not be able to remove a custom HTTP request header after first write', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
urlRequest.write('');
|
2020-03-18 15:59:44 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest.removeHeader(customHeaderName);
|
|
|
|
}).to.throw();
|
|
|
|
expect(urlRequest.getHeader(customHeaderName)).to.equal(customHeaderValue);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-10-26 18:33:36 +00:00
|
|
|
it('should keep the order of headers', async () => {
|
|
|
|
const customHeaderNameA = 'X-Header-100';
|
|
|
|
const customHeaderNameB = 'X-Header-200';
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
const headerNames = Array.from(Object.keys(request.headers));
|
|
|
|
const headerAIndex = headerNames.indexOf(customHeaderNameA.toLowerCase());
|
|
|
|
const headerBIndex = headerNames.indexOf(customHeaderNameB.toLowerCase());
|
|
|
|
expect(headerBIndex).to.be.below(headerAIndex);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader(customHeaderNameB, 'b');
|
|
|
|
urlRequest.setHeader(customHeaderNameA, 'a');
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to set cookie header line', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const cookieHeaderName = 'Cookie';
|
|
|
|
const cookieHeaderValue = 'test=12345';
|
2020-09-29 16:03:33 +00:00
|
|
|
const customSession = session.fromPartition(`test-cookie-header-${Math.random()}`);
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.headers[cookieHeaderName.toLowerCase()]).to.equal(cookieHeaderValue);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
await customSession.cookies.set({
|
|
|
|
url: `${serverUrl}`,
|
|
|
|
name: 'test',
|
|
|
|
value: '11111',
|
|
|
|
expirationDate: 0
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'GET',
|
|
|
|
url: serverUrl,
|
|
|
|
session: customSession
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
urlRequest.setHeader(cookieHeaderName, cookieHeaderValue);
|
|
|
|
expect(urlRequest.getHeader(cookieHeaderName)).to.equal(cookieHeaderValue);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to receive cookies', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const cookie = ['cookie1', 'cookie2'];
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('set-cookie', cookie);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.headers['set-cookie']).to.have.same.members(cookie);
|
|
|
|
});
|
2020-01-14 17:36:08 +00:00
|
|
|
|
2020-03-20 22:56:02 +00:00
|
|
|
it('should not use the sessions cookie store by default', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('x-cookie', `${request.headers.cookie!}`);
|
|
|
|
response.end();
|
|
|
|
});
|
2020-09-29 16:03:33 +00:00
|
|
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
2020-03-20 22:56:02 +00:00
|
|
|
const cookieVal = `${Date.now()}`;
|
|
|
|
await sess.cookies.set({
|
|
|
|
url: serverUrl,
|
|
|
|
name: 'wild_cookie',
|
|
|
|
value: cookieVal
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
2020-07-09 17:18:49 +00:00
|
|
|
expect(response.headers['x-cookie']).to.equal('undefined');
|
2020-03-20 22:56:02 +00:00
|
|
|
});
|
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
for (const extraOptions of [{ useSessionCookies: true }, { credentials: 'include' }] as ClientRequestConstructorOptions[]) {
|
|
|
|
describe(`when ${JSON.stringify(extraOptions)}`, () => {
|
|
|
|
it('should be able to use the sessions cookie store', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('x-cookie', request.headers.cookie!);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
|
|
|
const cookieVal = `${Date.now()}`;
|
|
|
|
await sess.cookies.set({
|
|
|
|
url: serverUrl,
|
|
|
|
name: 'wild_cookie',
|
|
|
|
value: cookieVal
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess,
|
|
|
|
...extraOptions
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.headers['x-cookie']).to.equal(`wild_cookie=${cookieVal}`);
|
2020-03-20 23:39:03 +00:00
|
|
|
});
|
2020-09-29 16:03:33 +00:00
|
|
|
|
|
|
|
it('should be able to use the sessions cookie store with set-cookie', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('set-cookie', 'foo=bar');
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
|
|
|
let cookies = await sess.cookies.get({});
|
|
|
|
expect(cookies).to.have.lengthOf(0);
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess,
|
|
|
|
...extraOptions
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
cookies = await sess.cookies.get({});
|
|
|
|
expect(cookies).to.have.lengthOf(1);
|
|
|
|
expect(cookies[0]).to.deep.equal({
|
|
|
|
name: 'foo',
|
|
|
|
value: 'bar',
|
|
|
|
domain: '127.0.0.1',
|
|
|
|
hostOnly: true,
|
|
|
|
path: '/',
|
|
|
|
secure: false,
|
|
|
|
httpOnly: false,
|
|
|
|
session: true,
|
|
|
|
sameSite: 'unspecified'
|
|
|
|
});
|
2020-03-20 23:39:03 +00:00
|
|
|
});
|
2020-09-29 16:03:33 +00:00
|
|
|
|
|
|
|
['Lax', 'Strict'].forEach((mode) => {
|
|
|
|
it(`should be able to use the sessions cookie store with same-site ${mode} cookies`, async () => {
|
|
|
|
const serverUrl = await respondNTimes.toSingleURL((request, response) => {
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('set-cookie', `same=site; SameSite=${mode}`);
|
|
|
|
response.setHeader('x-cookie', `${request.headers.cookie}`);
|
|
|
|
response.end();
|
|
|
|
}, 2);
|
|
|
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
|
|
|
let cookies = await sess.cookies.get({});
|
|
|
|
expect(cookies).to.have.lengthOf(0);
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess,
|
|
|
|
...extraOptions
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.headers['x-cookie']).to.equal('undefined');
|
|
|
|
await collectStreamBody(response);
|
|
|
|
cookies = await sess.cookies.get({});
|
|
|
|
expect(cookies).to.have.lengthOf(1);
|
|
|
|
expect(cookies[0]).to.deep.equal({
|
|
|
|
name: 'same',
|
|
|
|
value: 'site',
|
|
|
|
domain: '127.0.0.1',
|
|
|
|
hostOnly: true,
|
|
|
|
path: '/',
|
|
|
|
secure: false,
|
|
|
|
httpOnly: false,
|
|
|
|
session: true,
|
|
|
|
sameSite: mode.toLowerCase()
|
|
|
|
});
|
|
|
|
const urlRequest2 = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess,
|
|
|
|
...extraOptions
|
|
|
|
});
|
|
|
|
const response2 = await getResponse(urlRequest2);
|
|
|
|
expect(response2.headers['x-cookie']).to.equal('same=site');
|
|
|
|
});
|
2020-03-20 23:39:03 +00:00
|
|
|
});
|
|
|
|
|
2020-09-29 16:03:33 +00:00
|
|
|
it('should be able to use the sessions cookie store safely across redirects', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
|
|
|
response.statusCode = 302;
|
|
|
|
response.statusMessage = 'Moved';
|
|
|
|
const newUrl = await respondOnce.toSingleURL((req, res) => {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.statusMessage = 'OK';
|
|
|
|
res.setHeader('x-cookie', req.headers.cookie!);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
response.setHeader('x-cookie', request.headers.cookie!);
|
|
|
|
response.setHeader('location', newUrl.replace('127.0.0.1', 'localhost'));
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const sess = session.fromPartition(`cookie-tests-${Math.random()}`);
|
|
|
|
const cookie127Val = `${Date.now()}-127`;
|
|
|
|
const cookieLocalVal = `${Date.now()}-local`;
|
|
|
|
const localhostUrl = serverUrl.replace('127.0.0.1', 'localhost');
|
|
|
|
expect(localhostUrl).to.not.equal(serverUrl);
|
|
|
|
await Promise.all([
|
|
|
|
sess.cookies.set({
|
|
|
|
url: serverUrl,
|
|
|
|
name: 'wild_cookie',
|
|
|
|
value: cookie127Val
|
|
|
|
}), sess.cookies.set({
|
|
|
|
url: localhostUrl,
|
|
|
|
name: 'wild_cookie',
|
|
|
|
value: cookieLocalVal
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
session: sess,
|
|
|
|
...extraOptions
|
|
|
|
});
|
|
|
|
urlRequest.on('redirect', (status, method, url, headers) => {
|
|
|
|
// The initial redirect response should have received the 127 value here
|
|
|
|
expect(headers['x-cookie'][0]).to.equal(`wild_cookie=${cookie127Val}`);
|
|
|
|
urlRequest.followRedirect();
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
// We expect the server to have received the localhost value here
|
|
|
|
// The original request was to a 127.0.0.1 URL
|
|
|
|
// That request would have the cookie127Val cookie attached
|
|
|
|
// The request is then redirect to a localhost URL (different site)
|
|
|
|
// Because we are using the session cookie store it should do the safe / secure thing
|
|
|
|
// and attach the cookies for the new target domain
|
|
|
|
expect(response.headers['x-cookie']).to.equal(`wild_cookie=${cookieLocalVal}`);
|
2020-03-20 22:56:02 +00:00
|
|
|
});
|
|
|
|
});
|
2020-09-29 16:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('when {"credentials":"omit"}', () => {
|
|
|
|
it('should not send cookies');
|
|
|
|
it('should not store cookies');
|
2020-03-20 22:56:02 +00:00
|
|
|
});
|
|
|
|
|
2020-11-17 22:25:41 +00:00
|
|
|
it('should set sec-fetch-site to same-origin for request from same origin', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-site']).to.equal('same-origin');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
origin: serverUrl
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set sec-fetch-site to same-origin for request with the same origin header', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-site']).to.equal('same-origin');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
});
|
|
|
|
urlRequest.setHeader('Origin', serverUrl);
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set sec-fetch-site to cross-site for request from other origin', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-site']).to.equal('cross-site');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
origin: 'https://not-exists.com'
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not send sec-fetch-user header by default', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers).not.to.have.property('sec-fetch-user');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set sec-fetch-user to ?1 if requested', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-user']).to.equal('?1');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
});
|
|
|
|
urlRequest.setHeader('sec-fetch-user', '?1');
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set sec-fetch-mode to no-cors by default', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-mode']).to.equal('no-cors');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
['navigate', 'cors', 'no-cors', 'same-origin'].forEach((mode) => {
|
|
|
|
it(`should set sec-fetch-mode to ${mode} if requested`, async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-mode']).to.equal(mode);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
origin: serverUrl
|
|
|
|
});
|
|
|
|
urlRequest.setHeader('sec-fetch-mode', mode);
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set sec-fetch-dest to empty by default', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-dest']).to.equal('empty');
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
|
|
|
});
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
|
|
|
|
[
|
|
|
|
'empty', 'audio', 'audioworklet', 'document', 'embed', 'font',
|
|
|
|
'frame', 'iframe', 'image', 'manifest', 'object', 'paintworklet',
|
|
|
|
'report', 'script', 'serviceworker', 'style', 'track', 'video',
|
|
|
|
'worker', 'xslt'
|
|
|
|
].forEach((dest) => {
|
|
|
|
it(`should set sec-fetch-dest to ${dest} if requested`, async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers['sec-fetch-dest']).to.equal(dest);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
origin: serverUrl
|
|
|
|
});
|
|
|
|
urlRequest.setHeader('sec-fetch-dest', dest);
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should be able to abort an HTTP request before first write', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end();
|
|
|
|
expect.fail('Unexpected request event');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(serverUrl);
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('response', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('unexpected response event');
|
|
|
|
});
|
|
|
|
const aborted = emittedOnce(urlRequest, 'abort');
|
|
|
|
urlRequest.abort();
|
|
|
|
urlRequest.write('');
|
|
|
|
urlRequest.end();
|
|
|
|
await aborted;
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('it should be able to abort an HTTP request before request end', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestReceivedByServer = false;
|
|
|
|
let urlRequest: ClientRequest | null = null;
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestReceivedByServer = true;
|
|
|
|
urlRequest!.abort();
|
|
|
|
});
|
|
|
|
let requestAbortEventEmitted = false;
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('response', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected response event');
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected finish event');
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('error', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected error event');
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestAbortEventEmitted = true;
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
await emittedOnce(urlRequest, 'close', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest!.chunkedEncoding = true;
|
|
|
|
urlRequest!.write(randomString(kOneKiloByte));
|
|
|
|
});
|
|
|
|
expect(requestReceivedByServer).to.equal(true);
|
|
|
|
expect(requestAbortEventEmitted).to.equal(true);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('it should be able to abort an HTTP request after request end and before response', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestReceivedByServer = false;
|
|
|
|
let urlRequest: ClientRequest | null = null;
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestReceivedByServer = true;
|
|
|
|
urlRequest!.abort();
|
2019-06-14 23:26:07 +00:00
|
|
|
process.nextTick(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let requestFinishEventEmitted = false;
|
|
|
|
|
|
|
|
urlRequest = net.request(serverUrl);
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('response', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected response event');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestFinishEventEmitted = true;
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('error', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected error event');
|
|
|
|
});
|
|
|
|
urlRequest.end(randomString(kOneKiloByte));
|
|
|
|
await emittedOnce(urlRequest, 'abort');
|
|
|
|
expect(requestFinishEventEmitted).to.equal(true);
|
|
|
|
expect(requestReceivedByServer).to.equal(true);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('it should be able to abort an HTTP request after response start', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestReceivedByServer = false;
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestReceivedByServer = true;
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.write(randomString(kOneKiloByte));
|
|
|
|
});
|
|
|
|
let requestFinishEventEmitted = false;
|
|
|
|
let requestResponseEventEmitted = false;
|
|
|
|
let responseCloseEventEmitted = false;
|
|
|
|
|
|
|
|
const urlRequest = net.request(serverUrl);
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('response', (response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestResponseEventEmitted = true;
|
|
|
|
const statusCode = response.statusCode;
|
|
|
|
expect(statusCode).to.equal(200);
|
|
|
|
response.on('data', () => {});
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('end', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected end event');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('error', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected error event');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
response.on('close' as any, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
responseCloseEventEmitted = true;
|
|
|
|
});
|
|
|
|
urlRequest.abort();
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestFinishEventEmitted = true;
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('error', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected error event');
|
|
|
|
});
|
|
|
|
urlRequest.end(randomString(kOneKiloByte));
|
|
|
|
await emittedOnce(urlRequest, 'abort');
|
|
|
|
expect(requestFinishEventEmitted).to.be.true('request should emit "finish" event');
|
|
|
|
expect(requestReceivedByServer).to.be.true('request should be received by the server');
|
|
|
|
expect(requestResponseEventEmitted).to.be.true('"response" event should be emitted');
|
|
|
|
expect(responseCloseEventEmitted).to.be.true('response should emit "close" event');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('abort event should be emitted at most once', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestReceivedByServer = false;
|
|
|
|
let urlRequest: ClientRequest | null = null;
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestReceivedByServer = true;
|
|
|
|
urlRequest!.abort();
|
|
|
|
urlRequest!.abort();
|
|
|
|
});
|
|
|
|
let requestFinishEventEmitted = false;
|
|
|
|
let abortsEmitted = 0;
|
|
|
|
|
|
|
|
urlRequest = net.request(serverUrl);
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('response', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected response event');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('finish', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestFinishEventEmitted = true;
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('error', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Unexpected error event');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('abort', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
abortsEmitted++;
|
|
|
|
});
|
|
|
|
urlRequest.end(randomString(kOneKiloByte));
|
|
|
|
await emittedOnce(urlRequest, 'abort');
|
|
|
|
expect(requestFinishEventEmitted).to.be.true('request should emit "finish" event');
|
|
|
|
expect(requestReceivedByServer).to.be.true('request should be received by server');
|
|
|
|
expect(abortsEmitted).to.equal(1, 'request should emit exactly 1 "abort" event');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 22:34:25 +00:00
|
|
|
it('should allow to read response body from non-2xx response', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bodyData = randomString(kOneKiloByte);
|
2019-11-25 22:34:25 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 404;
|
|
|
|
response.end(bodyData);
|
|
|
|
});
|
2019-11-25 22:34:25 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
const bodyCheckPromise = getResponse(urlRequest).then(r => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(r.statusCode).to.equal(404);
|
|
|
|
return r;
|
2020-03-18 15:59:44 +00:00
|
|
|
}).then(collectStreamBody).then(receivedBodyData => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(receivedBodyData.toString()).to.equal(bodyData);
|
|
|
|
});
|
2019-11-25 22:34:25 +00:00
|
|
|
const eventHandlers = Promise.all([
|
2020-03-18 15:59:44 +00:00
|
|
|
bodyCheckPromise,
|
2019-11-25 22:34:25 +00:00
|
|
|
emittedOnce(urlRequest, 'close')
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-11-25 22:34:25 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
urlRequest.end();
|
2019-11-25 22:34:25 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await eventHandlers;
|
|
|
|
});
|
2019-11-25 22:34:25 +00:00
|
|
|
|
2019-08-27 16:12:33 +00:00
|
|
|
describe('webRequest', () => {
|
2019-06-14 23:26:07 +00:00
|
|
|
afterEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(null);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-07-25 17:20:02 +00:00
|
|
|
it('Should throw when invalid filters are passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
{ urls: ['*://www.googleapis.com'] },
|
2020-03-20 20:28:31 +00:00
|
|
|
(details, callback) => { callback({ cancel: false }); }
|
|
|
|
);
|
|
|
|
}).to.throw('Invalid url pattern *://www.googleapis.com: Empty path.');
|
2019-07-25 17:20:02 +00:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
2020-03-20 15:12:18 +00:00
|
|
|
{ urls: ['*://www.googleapis.com/', '*://blahblah.dev'] },
|
2020-03-20 20:28:31 +00:00
|
|
|
(details, callback) => { callback({ cancel: false }); }
|
|
|
|
);
|
|
|
|
}).to.throw('Invalid url pattern *://blahblah.dev: Empty path.');
|
|
|
|
});
|
2019-07-25 17:20:02 +00:00
|
|
|
|
|
|
|
it('Should not throw when valid filters are passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
{ urls: ['*://www.googleapis.com/'] },
|
2020-03-20 20:28:31 +00:00
|
|
|
(details, callback) => { callback({ cancel: false }); }
|
|
|
|
);
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
2019-07-25 17:20:02 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('Requests should be intercepted by webRequest module', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestUrl = '/requestUrl';
|
|
|
|
const redirectUrl = '/redirectUrl';
|
|
|
|
let requestIsRedirected = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toURL(redirectUrl, (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsRedirected = true;
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
let requestIsIntercepted = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(
|
|
|
|
(details, callback) => {
|
2019-06-14 23:26:07 +00:00
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsIntercepted = true;
|
2019-06-14 23:26:07 +00:00
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(`${serverUrl}${requestUrl}`);
|
|
|
|
const response = await getResponse(urlRequest);
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL');
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module');
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
|
|
|
it('should to able to create and intercept a request using a custom session object', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestUrl = '/requestUrl';
|
|
|
|
const redirectUrl = '/redirectUrl';
|
2020-09-29 16:03:33 +00:00
|
|
|
const customPartitionName = `custom-partition-${Math.random()}`;
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestIsRedirected = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toURL(redirectUrl, (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsRedirected = true;
|
|
|
|
response.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session');
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const customSession = session.fromPartition(customPartitionName, { cache: false });
|
|
|
|
let requestIsIntercepted = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsIntercepted = true;
|
2020-03-18 15:59:44 +00:00
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`,
|
|
|
|
session: customSession
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL');
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should to able to create and intercept a request using a custom partition name', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestUrl = '/requestUrl';
|
|
|
|
const redirectUrl = '/redirectUrl';
|
2020-09-29 16:03:33 +00:00
|
|
|
const customPartitionName = `custom-partition-${Math.random()}`;
|
2020-03-20 20:28:31 +00:00
|
|
|
let requestIsRedirected = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toURL(redirectUrl, (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsRedirected = true;
|
|
|
|
response.end();
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
session.defaultSession.webRequest.onBeforeRequest(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect.fail('Request should not be intercepted by the default session');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const customSession = session.fromPartition(customPartitionName, { cache: false });
|
|
|
|
let requestIsIntercepted = false;
|
2020-03-18 15:59:44 +00:00
|
|
|
customSession.webRequest.onBeforeRequest((details, callback) => {
|
|
|
|
if (details.url === `${serverUrl}${requestUrl}`) {
|
2020-03-20 20:28:31 +00:00
|
|
|
requestIsIntercepted = true;
|
2020-03-18 15:59:44 +00:00
|
|
|
// Disabled due to false positive in StandardJS
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
|
|
|
callback({
|
|
|
|
redirectURL: `${serverUrl}${redirectUrl}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
cancel: false
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`,
|
|
|
|
partition: customPartitionName
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
expect(requestIsRedirected).to.be.true('The server should receive a request to the forward URL');
|
|
|
|
expect(requestIsIntercepted).to.be.true('The request should be intercepted by the webRequest module');
|
|
|
|
});
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
it('should throw when calling getHeader without a name', () => {
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(net.request({ url: 'https://test' }).getHeader as any)();
|
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)/);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
net.request({ url: 'https://test' }).getHeader(null as any);
|
|
|
|
}).to.throw(/`name` is required for getHeader\(name\)/);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
it('should throw when calling removeHeader without a name', () => {
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(net.request({ url: 'https://test' }).removeHeader as any)();
|
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)/);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
net.request({ url: 'https://test' }).removeHeader(null as any);
|
|
|
|
}).to.throw(/`name` is required for removeHeader\(name\)/);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should follow redirect when no redirect handler is provided', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const requestUrl = '/302';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toRoutes({
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/200');
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.end();
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}${requestUrl}`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should follow redirect chain when no redirect handler is provided', async () => {
|
|
|
|
const serverUrl = await respondOnce.toRoutes({
|
2019-06-14 23:26:07 +00:00
|
|
|
'/redirectChain': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/302');
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
},
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/200');
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.end();
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
url: `${serverUrl}/redirectChain`
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should not follow redirect when request is canceled in redirect handler', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/200');
|
|
|
|
response.end();
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
urlRequest.end();
|
|
|
|
urlRequest.on('redirect', () => { urlRequest.abort(); });
|
|
|
|
urlRequest.on('error', () => {});
|
|
|
|
await emittedOnce(urlRequest, 'abort');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
it('should not follow redirect when mode is error', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/200');
|
|
|
|
response.end();
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
const urlRequest = net.request({
|
|
|
|
url: serverUrl,
|
|
|
|
redirect: 'error'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
urlRequest.end();
|
|
|
|
await emittedOnce(urlRequest, 'error');
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should follow redirect when handler calls callback', async () => {
|
|
|
|
const serverUrl = await respondOnce.toRoutes({
|
2019-06-14 23:26:07 +00:00
|
|
|
'/redirectChain': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/302');
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
},
|
2019-11-27 01:01:13 +00:00
|
|
|
'/302': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 302;
|
|
|
|
response.setHeader('Location', '/200');
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
},
|
|
|
|
'/200': (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.end();
|
2019-11-01 20:37:02 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
const urlRequest = net.request({ url: `${serverUrl}/redirectChain`, redirect: 'manual' });
|
|
|
|
const redirects: string[] = [];
|
2019-11-25 20:56:18 +00:00
|
|
|
urlRequest.on('redirect', (status, method, url) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
redirects.push(url);
|
|
|
|
urlRequest.followRedirect();
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
2019-11-25 20:56:18 +00:00
|
|
|
expect(redirects).to.deep.equal([
|
2019-11-27 01:01:13 +00:00
|
|
|
`${serverUrl}/302`,
|
2019-11-25 20:56:18 +00:00
|
|
|
`${serverUrl}/200`
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
it('should throw if given an invalid session option', () => {
|
|
|
|
expect(() => {
|
|
|
|
net.request({
|
|
|
|
url: 'https://foo',
|
2019-08-05 17:45:58 +00:00
|
|
|
session: 1 as any
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
}).to.throw('`session` should be an instance of the Session class');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
it('should throw if given an invalid partition option', () => {
|
|
|
|
expect(() => {
|
|
|
|
net.request({
|
|
|
|
url: 'https://foo',
|
2019-08-05 17:45:58 +00:00
|
|
|
partition: 1 as any
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
}).to.throw('`partition` should be a string');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to create a request with options', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrlUnparsed = await respondOnce.toURL('/', (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(request.method).to.equal('GET');
|
|
|
|
expect(request.headers[customHeaderName.toLowerCase()]).to.equal(customHeaderValue);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const serverUrl = url.parse(serverUrlUnparsed);
|
2020-03-18 15:59:44 +00:00
|
|
|
const options = {
|
|
|
|
port: serverUrl.port ? parseInt(serverUrl.port, 10) : undefined,
|
|
|
|
hostname: '127.0.0.1',
|
|
|
|
headers: { [customHeaderName]: customHeaderValue }
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
const urlRequest = net.request(options);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.be.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should be able to pipe a readable stream into a net request', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bodyData = randomString(kOneMegaByte);
|
|
|
|
let netRequestReceived = false;
|
|
|
|
let netRequestEnded = false;
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
const [nodeServerUrl, netServerUrl] = await Promise.all([
|
2019-06-14 23:26:07 +00:00
|
|
|
respondOnce.toSingleURL((request, response) => response.end(bodyData)),
|
|
|
|
respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
netRequestReceived = true;
|
|
|
|
let receivedBodyData = '';
|
2019-06-14 23:26:07 +00:00
|
|
|
request.on('data', (chunk) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
receivedBodyData += chunk.toString();
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
request.on('end', (chunk: Buffer | undefined) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
netRequestEnded = true;
|
2019-06-14 23:26:07 +00:00
|
|
|
if (chunk) {
|
2020-03-20 20:28:31 +00:00
|
|
|
receivedBodyData += chunk.toString();
|
2019-06-14 23:26:07 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(receivedBodyData).to.be.equal(bodyData);
|
|
|
|
response.end();
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
|
|
|
const nodeRequest = http.request(nodeServerUrl);
|
|
|
|
const nodeResponse = await getResponse(nodeRequest as any) as any as http.ServerResponse;
|
|
|
|
const netRequest = net.request(netServerUrl);
|
|
|
|
const responsePromise = emittedOnce(netRequest, 'response');
|
2020-03-18 15:59:44 +00:00
|
|
|
// TODO(@MarshallOfSound) - FIXME with #22730
|
2020-03-20 20:28:31 +00:00
|
|
|
nodeResponse.pipe(netRequest as any);
|
|
|
|
const [netResponse] = await responsePromise;
|
|
|
|
expect(netResponse.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(netResponse);
|
|
|
|
expect(netRequestReceived).to.be.true('net request received');
|
|
|
|
expect(netRequestEnded).to.be.true('net request ended');
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-12-06 21:14:44 +00:00
|
|
|
it('should report upload progress', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const netRequest = net.request({ url: serverUrl, method: 'POST' });
|
2020-05-18 17:22:48 +00:00
|
|
|
expect(netRequest.getUploadProgress()).to.have.property('active', false);
|
2020-03-20 20:28:31 +00:00
|
|
|
netRequest.end(Buffer.from('hello'));
|
|
|
|
const [position, total] = await emittedOnce(netRequest, 'upload-progress');
|
|
|
|
expect(netRequest.getUploadProgress()).to.deep.equal({ active: true, started: true, current: position, total });
|
|
|
|
});
|
2019-12-06 21:14:44 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should emit error event on server socket destroy', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
request.socket.destroy();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.end();
|
|
|
|
const [error] = await emittedOnce(urlRequest, 'error');
|
|
|
|
expect(error.message).to.equal('net::ERR_EMPTY_RESPONSE');
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
|
|
|
|
it('should emit error event on server request destroy', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
request.destroy();
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.end(randomBuffer(kOneMegaByte));
|
|
|
|
const [error] = await emittedOnce(urlRequest, 'error');
|
|
|
|
expect(error.message).to.be.oneOf(['net::ERR_CONNECTION_RESET', 'net::ERR_CONNECTION_ABORTED']);
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
|
|
|
|
it('should not emit any event after close', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end();
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.end();
|
2019-11-25 20:56:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await emittedOnce(urlRequest, 'close');
|
2019-11-25 20:56:18 +00:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
['finish', 'abort', 'close', 'error'].forEach(evName => {
|
|
|
|
urlRequest.on(evName as any, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
reject(new Error(`Unexpected ${evName} event`));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setTimeout(resolve, 50);
|
|
|
|
});
|
|
|
|
});
|
2020-05-20 17:28:38 +00:00
|
|
|
|
|
|
|
it('should remove the referer header when no referrer url specified', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers.referer).to.equal(undefined);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.end();
|
|
|
|
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set the referer header when a referrer url specified', async () => {
|
|
|
|
const referrerURL = 'https://www.electronjs.org/';
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
expect(request.headers.referer).to.equal(referrerURL);
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.setHeader('referer', referrerURL);
|
|
|
|
urlRequest.end();
|
|
|
|
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
|
|
|
describe('IncomingMessage API', () => {
|
2019-11-25 20:56:18 +00:00
|
|
|
it('response object should implement the IncomingMessage API', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const customHeaderName = 'Some-Custom-Header-Name';
|
|
|
|
const customHeaderValue = 'Some-Customer-Header-Value';
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader(customHeaderName, customHeaderValue);
|
|
|
|
response.end();
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(response.statusMessage).to.equal('OK');
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const headers = response.headers;
|
|
|
|
expect(headers).to.be.an('object');
|
|
|
|
const headerValue = headers[customHeaderName.toLowerCase()];
|
|
|
|
expect(headerValue).to.equal(customHeaderValue);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const httpVersion = response.httpVersion;
|
|
|
|
expect(httpVersion).to.be.a('string').and.to.have.lengthOf.at.least(1);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const httpVersionMajor = response.httpVersionMajor;
|
|
|
|
expect(httpVersionMajor).to.be.a('number').and.to.be.at.least(1);
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const httpVersionMinor = response.httpVersionMinor;
|
|
|
|
expect(httpVersionMinor).to.be.a('number').and.to.be.at.least(0);
|
2019-11-25 20:56:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should discard duplicate headers', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const includedHeader = 'max-forwards';
|
|
|
|
const discardableHeader = 'Max-Forwards';
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const includedHeaderValue = 'max-fwds-val';
|
|
|
|
const discardableHeaderValue = 'max-fwds-val-two';
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader(discardableHeader, discardableHeaderValue);
|
|
|
|
response.setHeader(includedHeader, includedHeaderValue);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(response.statusMessage).to.equal('OK');
|
|
|
|
|
|
|
|
const headers = response.headers;
|
|
|
|
expect(headers).to.be.an('object');
|
|
|
|
|
|
|
|
expect(headers).to.have.property(includedHeader);
|
|
|
|
expect(headers).to.not.have.property(discardableHeader);
|
|
|
|
expect(headers[includedHeader]).to.equal(includedHeaderValue);
|
|
|
|
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-25 20:56:18 +00:00
|
|
|
it('should join repeated non-discardable value with ,', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.statusCode = 200;
|
|
|
|
response.statusMessage = 'OK';
|
|
|
|
response.setHeader('referrer-policy', ['first-text', 'second-text']);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
expect(response.statusCode).to.equal(200);
|
|
|
|
expect(response.statusMessage).to.equal('OK');
|
|
|
|
|
|
|
|
const headers = response.headers;
|
|
|
|
expect(headers).to.be.an('object');
|
|
|
|
expect(headers).to.have.property('referrer-policy');
|
|
|
|
expect(headers['referrer-policy']).to.equal('first-text, second-text');
|
|
|
|
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2019-11-27 01:01:13 +00:00
|
|
|
it('should be able to pipe a net response into a writable stream', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bodyData = randomString(kOneKiloByte);
|
|
|
|
let nodeRequestProcessed = false;
|
2019-11-27 01:01:13 +00:00
|
|
|
const [netServerUrl, nodeServerUrl] = await Promise.all([
|
2019-06-14 23:26:07 +00:00
|
|
|
respondOnce.toSingleURL((request, response) => response.end(bodyData)),
|
2020-03-18 15:59:44 +00:00
|
|
|
respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const receivedBodyData = await collectStreamBody(request);
|
|
|
|
expect(receivedBodyData).to.be.equal(bodyData);
|
|
|
|
nodeRequestProcessed = true;
|
|
|
|
response.end();
|
2019-06-14 23:26:07 +00:00
|
|
|
})
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
|
|
|
const netRequest = net.request(netServerUrl);
|
|
|
|
const netResponse = await getResponse(netRequest);
|
|
|
|
const serverUrl = url.parse(nodeServerUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
const nodeOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
path: serverUrl.path,
|
|
|
|
port: serverUrl.port
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
const nodeRequest = http.request(nodeOptions);
|
2020-03-18 15:59:44 +00:00
|
|
|
const nodeResponsePromise = emittedOnce(nodeRequest, 'response');
|
|
|
|
// TODO(@MarshallOfSound) - FIXME with #22730
|
2020-03-20 20:28:31 +00:00
|
|
|
(netResponse as any).pipe(nodeRequest);
|
|
|
|
const [nodeResponse] = await nodeResponsePromise;
|
|
|
|
netRequest.end();
|
|
|
|
await collectStreamBody(nodeResponse);
|
|
|
|
expect(nodeRequestProcessed).to.equal(true);
|
|
|
|
});
|
2020-10-06 00:47:41 +00:00
|
|
|
|
|
|
|
it('should correctly throttle an incoming stream', async () => {
|
|
|
|
let numChunksSent = 0;
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
|
|
|
const data = randomString(kOneMegaByte);
|
|
|
|
const write = () => {
|
|
|
|
let ok = true;
|
|
|
|
do {
|
|
|
|
numChunksSent++;
|
|
|
|
if (numChunksSent > 30) return;
|
|
|
|
ok = response.write(data);
|
|
|
|
} while (ok);
|
|
|
|
response.once('drain', write);
|
|
|
|
};
|
|
|
|
write();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.on('response', () => {});
|
|
|
|
urlRequest.end();
|
|
|
|
await delay(2000);
|
|
|
|
expect(numChunksSent).to.be.at.most(20);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-10-21 02:55:06 +00:00
|
|
|
describe('net.isOnline', () => {
|
|
|
|
it('getter returns boolean', () => {
|
|
|
|
expect(net.isOnline()).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('property returns boolean', () => {
|
|
|
|
expect(net.online).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-14 23:26:07 +00:00
|
|
|
describe('Stability and performance', () => {
|
|
|
|
it('should free unreferenced, never-started request objects without crash', (done) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
net.request('https://test');
|
2019-06-14 23:26:07 +00:00
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should collect on-going requests without crash', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let finishResponse: (() => void) | null = null;
|
2020-03-18 15:59:44 +00:00
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.write(randomString(kOneKiloByte));
|
2019-06-14 23:26:07 +00:00
|
|
|
finishResponse = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.write(randomString(kOneKiloByte));
|
|
|
|
response.end();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
const response = await getResponse(urlRequest);
|
2020-03-18 15:59:44 +00:00
|
|
|
process.nextTick(() => {
|
|
|
|
// Trigger a garbage collection.
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
finishResponse!();
|
|
|
|
});
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-06-14 23:26:07 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should collect unreferenced, ended requests without crash', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL((request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
});
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2020-03-18 15:59:44 +00:00
|
|
|
|
|
|
|
it('should finish sending data when urlRequest is unreferenced', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received = await collectStreamBodyBuffer(request);
|
|
|
|
expect(received.length).to.equal(kOneMegaByte);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('close', () => {
|
2019-11-27 01:01:13 +00:00
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
urlRequest.write(randomBuffer(kOneMegaByte));
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
await collectStreamBody(response);
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should finish sending data when urlRequest is unreferenced for chunked encoding', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received = await collectStreamBodyBuffer(request);
|
|
|
|
response.end();
|
|
|
|
expect(received.length).to.equal(kOneMegaByte);
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.chunkedEncoding = true;
|
|
|
|
urlRequest.write(randomBuffer(kOneMegaByte));
|
|
|
|
const response = await getResponse(urlRequest);
|
|
|
|
await collectStreamBody(response);
|
2020-03-18 15:59:44 +00:00
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
});
|
|
|
|
});
|
2019-11-27 01:01:13 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should finish sending data when urlRequest is unreferenced before close event for chunked encoding', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received = await collectStreamBodyBuffer(request);
|
|
|
|
response.end();
|
|
|
|
expect(received.length).to.equal(kOneMegaByte);
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
|
|
|
urlRequest.chunkedEncoding = true;
|
|
|
|
urlRequest.write(randomBuffer(kOneMegaByte));
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should finish sending data when urlRequest is unreferenced', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received = await collectStreamBodyBuffer(request);
|
|
|
|
response.end();
|
|
|
|
expect(received.length).to.equal(kOneMegaByte);
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
urlRequest.write(randomBuffer(kOneMegaByte));
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
2019-11-25 20:56:18 +00:00
|
|
|
|
2020-03-18 15:59:44 +00:00
|
|
|
it('should finish sending data when urlRequest is unreferenced for chunked encoding', async () => {
|
|
|
|
const serverUrl = await respondOnce.toSingleURL(async (request, response) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const received = await collectStreamBodyBuffer(request);
|
|
|
|
response.end();
|
|
|
|
expect(received.length).to.equal(kOneMegaByte);
|
|
|
|
});
|
|
|
|
const urlRequest = net.request(serverUrl);
|
2020-03-18 15:59:44 +00:00
|
|
|
urlRequest.on('close', () => {
|
|
|
|
process.nextTick(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
2020-03-20 20:28:31 +00:00
|
|
|
v8Util.requestGarbageCollectionForTesting();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
urlRequest.chunkedEncoding = true;
|
|
|
|
urlRequest.write(randomBuffer(kOneMegaByte));
|
|
|
|
await collectStreamBody(await getResponse(urlRequest));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|