refactor: implement ajax() in tests using native fetch instead of jQuery (#32579)

This commit is contained in:
Milan Burda 2022-01-24 10:34:23 +01:00 committed by GitHub
parent 7032be660d
commit 9d054755d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 116 additions and 160 deletions

View file

@ -36,7 +36,7 @@ describe('webRequest module', () => {
let defaultURL: string;
before((done) => {
protocol.registerStringProtocol('neworigin', (req, cb) => cb(''));
protocol.registerStringProtocol('cors', (req, cb) => cb(''));
server.listen(0, '127.0.0.1', () => {
const port = (server.address() as AddressInfo).port;
defaultURL = `http://127.0.0.1:${port}/`;
@ -46,14 +46,14 @@ describe('webRequest module', () => {
after(() => {
server.close();
protocol.unregisterProtocol('neworigin');
protocol.unregisterProtocol('cors');
});
let contents: WebContents = null as unknown as WebContents;
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
before(async () => {
contents = (webContents as any).create({ sandbox: true });
await contents.loadFile(path.join(fixturesPath, 'pages', 'jquery.html'));
await contents.loadFile(path.join(fixturesPath, 'pages', 'fetch.html'));
});
after(() => (contents as any).destroy());
@ -72,7 +72,7 @@ describe('webRequest module', () => {
cancel: true
});
});
await expect(ajax(defaultURL)).to.eventually.be.rejectedWith('404');
await expect(ajax(defaultURL)).to.eventually.be.rejected();
});
it('can filter URLs', async () => {
@ -82,7 +82,7 @@ describe('webRequest module', () => {
});
const { data } = await ajax(`${defaultURL}nofilter/test`);
expect(data).to.equal('/nofilter/test');
await expect(ajax(`${defaultURL}filter/test`)).to.eventually.be.rejectedWith('404');
await expect(ajax(`${defaultURL}filter/test`)).to.eventually.be.rejected();
});
it('receives details object', async () => {
@ -117,9 +117,9 @@ describe('webRequest module', () => {
callback({ cancel: true });
});
await expect(ajax(defaultURL, {
type: 'POST',
data: postData
})).to.eventually.be.rejectedWith('404');
method: 'POST',
body: qs.stringify(postData)
})).to.eventually.be.rejected();
});
it('can redirect the request', async () => {
@ -151,7 +151,7 @@ describe('webRequest module', () => {
protocol: 'file',
slashes: true
});
await expect(ajax(fileURL)).to.eventually.be.rejectedWith('404');
await expect(ajax(fileURL)).to.eventually.be.rejected();
});
});
@ -182,12 +182,12 @@ describe('webRequest module', () => {
});
it('can change the request headers on a custom protocol redirect', async () => {
protocol.registerStringProtocol('custom-scheme', (req, callback) => {
if (req.url === 'custom-scheme://fake-host/redirect') {
protocol.registerStringProtocol('no-cors', (req, callback) => {
if (req.url === 'no-cors://fake-host/redirect') {
callback({
statusCode: 302,
headers: {
Location: 'custom-scheme://fake-host'
Location: 'no-cors://fake-host'
}
});
} else {
@ -202,7 +202,7 @@ describe('webRequest module', () => {
// 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.
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'jquery.html'));
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
try {
ses.webRequest.onBeforeSendHeaders((details, callback) => {
@ -210,10 +210,10 @@ describe('webRequest module', () => {
requestHeaders.Accept = '*/*;test/header';
callback({ requestHeaders: requestHeaders });
});
const { data } = await ajax('custom-scheme://fake-host/redirect');
const { data } = await ajax('no-cors://fake-host/redirect');
expect(data).to.equal('header-received');
} finally {
protocol.unregisterProtocol('custom-scheme');
protocol.unregisterProtocol('no-cors');
}
});
@ -233,7 +233,7 @@ describe('webRequest module', () => {
called = true;
callback({ requestHeaders: details.requestHeaders });
});
await ajax('neworigin://host');
await ajax('cors://host');
expect(called).to.be.true();
});
@ -320,7 +320,7 @@ describe('webRequest module', () => {
callback({ responseHeaders: responseHeaders });
});
const { headers } = await ajax(defaultURL);
expect(headers).to.match(/^custom: Changed$/m);
expect(headers).to.to.have.property('custom', 'Changed');
});
it('can change response origin', async () => {
@ -330,7 +330,7 @@ describe('webRequest module', () => {
callback({ responseHeaders: responseHeaders });
});
const { headers } = await ajax(defaultURL);
expect(headers).to.match(/^access-control-allow-origin: http:\/\/new-origin$/m);
expect(headers).to.to.have.property('access-control-allow-origin', 'http://new-origin');
});
it('can change headers of CORS responses', async () => {
@ -339,8 +339,8 @@ describe('webRequest module', () => {
responseHeaders.Custom = ['Changed'] as any;
callback({ responseHeaders: responseHeaders });
});
const { headers } = await ajax('neworigin://host');
expect(headers).to.match(/^custom: Changed$/m);
const { headers } = await ajax('cors://host');
expect(headers).to.to.have.property('custom', 'Changed');
});
it('does not change header by default', async () => {
@ -348,7 +348,7 @@ describe('webRequest module', () => {
callback({});
});
const { data, headers } = await ajax(defaultURL);
expect(headers).to.match(/^custom: Header$/m);
expect(headers).to.to.have.property('custom', 'Header');
expect(data).to.equal('/');
});
@ -358,7 +358,7 @@ describe('webRequest module', () => {
callback({});
});
const { data, headers } = await ajax(defaultURL + 'contentDisposition');
expect(headers).to.match(/^content-disposition: attachment; filename=aa%E4%B8%ADaa.txt$/m);
expect(headers).to.to.have.property('content-disposition', 'attachment; filename=aa%E4%B8%ADaa.txt');
expect(data).to.equal('/contentDisposition');
});
@ -368,7 +368,7 @@ describe('webRequest module', () => {
callback({ responseHeaders: responseHeaders });
});
const { headers } = await ajax(defaultURL + 'serverRedirect');
expect(headers).to.match(/^custom: Header$/m);
expect(headers).to.to.have.property('custom', 'Header');
});
it('can change the header status', async () => {
@ -379,19 +379,8 @@ describe('webRequest module', () => {
statusLine: 'HTTP/1.1 404 Not Found'
});
});
const { headers } = await contents.executeJavaScript(`new Promise((resolve, reject) => {
const options = {
...${JSON.stringify({ url: defaultURL })},
success: (data, status, request) => {
reject(new Error('expected failure'))
},
error: (xhr) => {
resolve({ headers: xhr.getAllResponseHeaders() })
}
}
$.ajax(options)
})`);
expect(headers).to.match(/^custom: Header$/m);
const { headers } = await ajax(defaultURL);
expect(headers).to.to.have.property('custom', 'Header');
});
});
@ -408,7 +397,7 @@ describe('webRequest module', () => {
expect(details.responseHeaders!.Custom).to.deep.equal(['Header']);
});
const { data, headers } = await ajax(defaultURL);
expect(headers).to.match(/^custom: Header$/m);
expect(headers).to.to.have.property('custom', 'Header');
expect(data).to.equal('/');
});
});
@ -468,7 +457,7 @@ describe('webRequest module', () => {
ses.webRequest.onErrorOccurred((details) => {
expect(details.error).to.equal('net::ERR_BLOCKED_BY_CLIENT');
});
await expect(ajax(defaultURL)).to.eventually.be.rejectedWith('404');
await expect(ajax(defaultURL)).to.eventually.be.rejected();
});
});