refactor: implement ajax() in tests using native fetch instead of jQuery (#32579)
This commit is contained in:
parent
7032be660d
commit
9d054755d6
13 changed files with 116 additions and 160 deletions
|
@ -16,7 +16,7 @@ const DEPOT_TOOLS = path.resolve(SOURCE_ROOT, 'third_party', 'depot_tools');
|
||||||
const IGNORELIST = new Set([
|
const IGNORELIST = new Set([
|
||||||
['shell', 'browser', 'resources', 'win', 'resource.h'],
|
['shell', 'browser', 'resources', 'win', 'resource.h'],
|
||||||
['shell', 'common', 'node_includes.h'],
|
['shell', 'common', 'node_includes.h'],
|
||||||
['spec', 'static', 'jquery-2.0.3.min.js'],
|
['spec-main', 'fixtures', 'pages', 'jquery-3.6.0.min.js'],
|
||||||
['spec', 'ts-smoke', 'electron', 'main.ts'],
|
['spec', 'ts-smoke', 'electron', 'main.ts'],
|
||||||
['spec', 'ts-smoke', 'electron', 'renderer.ts'],
|
['spec', 'ts-smoke', 'electron', 'renderer.ts'],
|
||||||
['spec', 'ts-smoke', 'runner.js']
|
['spec', 'ts-smoke', 'runner.js']
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
"env": {
|
"env": {
|
||||||
"browser": true,
|
"browser": true,
|
||||||
"mocha": true,
|
"mocha": true,
|
||||||
"jquery": true,
|
|
||||||
"serviceworker": true
|
"serviceworker": true
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
|
|
|
@ -29,7 +29,7 @@ const unregisterProtocol = protocol.unregisterProtocol;
|
||||||
const uninterceptProtocol = protocol.uninterceptProtocol;
|
const uninterceptProtocol = protocol.uninterceptProtocol;
|
||||||
|
|
||||||
const text = 'valar morghulis';
|
const text = 'valar morghulis';
|
||||||
const protocolName = 'sp';
|
const protocolName = 'no-cors';
|
||||||
const postData = {
|
const postData = {
|
||||||
name: 'post test',
|
name: 'post test',
|
||||||
type: 'string'
|
type: 'string'
|
||||||
|
@ -80,7 +80,7 @@ describe('protocol module', () => {
|
||||||
// Note that we need to do navigation every time after a protocol is
|
// Note that we need to do navigation every time after a protocol is
|
||||||
// registered or unregistered, otherwise the new protocol won't be
|
// registered or unregistered, otherwise the new protocol won't be
|
||||||
// recognized by current page when NetworkService is used.
|
// 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'));
|
||||||
return contents.executeJavaScript(`ajax("${url}", ${JSON.stringify(options)})`);
|
return contents.executeJavaScript(`ajax("${url}", ${JSON.stringify(options)})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ describe('protocol module', () => {
|
||||||
|
|
||||||
it('sends error when callback is called with nothing', async () => {
|
it('sends error when callback is called with nothing', async () => {
|
||||||
registerBufferProtocol(protocolName, (req, cb: any) => cb());
|
registerBufferProtocol(protocolName, (req, cb: any) => cb());
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.eventually.be.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.eventually.be.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not crash when callback is called in next tick', async () => {
|
it('does not crash when callback is called in next tick', async () => {
|
||||||
|
@ -157,7 +157,7 @@ describe('protocol module', () => {
|
||||||
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(text);
|
expect(r.data).to.equal(text);
|
||||||
expect(r.headers).to.include('access-control-allow-origin: *');
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('sends object as response', async () => {
|
||||||
|
@ -174,7 +174,7 @@ describe('protocol module', () => {
|
||||||
it('fails when sending object other than string', async () => {
|
it('fails when sending object other than string', async () => {
|
||||||
const notAString = () => {};
|
const notAString = () => {};
|
||||||
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ describe('protocol module', () => {
|
||||||
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(text);
|
expect(r.data).to.equal(text);
|
||||||
expect(r.headers).to.include('access-control-allow-origin: *');
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('sends object as response', async () => {
|
||||||
|
@ -206,7 +206,7 @@ describe('protocol module', () => {
|
||||||
|
|
||||||
it('fails when sending string', async () => {
|
it('fails when sending string', async () => {
|
||||||
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -226,7 +226,7 @@ describe('protocol module', () => {
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(String(fileContent));
|
expect(r.data).to.equal(String(fileContent));
|
||||||
expect(r.headers).to.include('access-control-allow-origin: *');
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets custom headers', async () => {
|
it('sets custom headers', async () => {
|
||||||
|
@ -236,7 +236,7 @@ describe('protocol module', () => {
|
||||||
}));
|
}));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(String(fileContent));
|
expect(r.data).to.equal(String(fileContent));
|
||||||
expect(r.headers).to.include('x-great-header: sogreat');
|
expect(r.headers).to.have.property('x-great-header', 'sogreat');
|
||||||
});
|
});
|
||||||
|
|
||||||
it.skip('throws an error when custom headers are invalid', (done) => {
|
it.skip('throws an error when custom headers are invalid', (done) => {
|
||||||
|
@ -247,7 +247,7 @@ describe('protocol module', () => {
|
||||||
})).to.throw(Error, 'Value of \'X-Great-Header\' header has to be a string');
|
})).to.throw(Error, 'Value of \'X-Great-Header\' header has to be a string');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
ajax(protocolName + '://fake-host');
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('sends object as response', async () => {
|
||||||
|
@ -265,12 +265,12 @@ describe('protocol module', () => {
|
||||||
it('fails when sending unexist-file', async () => {
|
it('fails when sending unexist-file', async () => {
|
||||||
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(fakeFilePath));
|
registerFileProtocol(protocolName, (request, callback) => callback(fakeFilePath));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails when sending unsupported content', async () => {
|
it('fails when sending unsupported content', async () => {
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -292,12 +292,12 @@ describe('protocol module', () => {
|
||||||
|
|
||||||
it('fails when sending invalid url', async () => {
|
it('fails when sending invalid url', async () => {
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fails when sending unsupported content', async () => {
|
it('fails when sending unsupported content', async () => {
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('works when target URL redirects', async () => {
|
it('works when target URL redirects', async () => {
|
||||||
|
@ -331,7 +331,7 @@ describe('protocol module', () => {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ajax(protocolName + '://fake-host');
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -359,7 +359,7 @@ describe('protocol module', () => {
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(text);
|
expect(r.data).to.equal(text);
|
||||||
expect(r.status).to.equal(200);
|
expect(r.status).to.equal(200);
|
||||||
expect(r.headers).to.include('x-electron: a, b');
|
expect(r.headers).to.have.property('x-electron', 'a, b');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends custom status code', async () => {
|
it('sends custom status code', async () => {
|
||||||
|
@ -368,7 +368,7 @@ describe('protocol module', () => {
|
||||||
data: null as any
|
data: null as any
|
||||||
}));
|
}));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.be.undefined('data');
|
expect(r.data).to.be.empty('data');
|
||||||
expect(r.status).to.equal(204);
|
expect(r.status).to.equal(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -382,7 +382,7 @@ describe('protocol module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
||||||
expect(r.data['x-return-headers']).to.equal('yes');
|
expect(JSON.parse(r.data)['x-return-headers']).to.equal('yes');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns response multiple response headers with the same name', async () => {
|
it('returns response multiple response headers with the same name', async () => {
|
||||||
|
@ -399,7 +399,8 @@ describe('protocol module', () => {
|
||||||
// SUBTLE: when the response headers have multiple values it
|
// SUBTLE: when the response headers have multiple values it
|
||||||
// separates values by ", ". When the response headers are incorrectly
|
// separates values by ", ". When the response headers are incorrectly
|
||||||
// converting an array to a string it separates values by ",".
|
// converting an array to a string it separates values by ",".
|
||||||
expect(r.headers).to.equal('header1: value1, value2\r\nheader2: value3\r\n');
|
expect(r.headers).to.have.property('header1', 'value1, value2');
|
||||||
|
expect(r.headers).to.have.property('header2', 'value3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can handle large responses', async () => {
|
it('can handle large responses', async () => {
|
||||||
|
@ -456,7 +457,7 @@ describe('protocol module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const hasEndedPromise = emittedOnce(events, 'end');
|
const hasEndedPromise = emittedOnce(events, 'end');
|
||||||
ajax(protocolName + '://fake-host');
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
await hasEndedPromise;
|
await hasEndedPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -478,9 +479,9 @@ describe('protocol module', () => {
|
||||||
|
|
||||||
const hasRespondedPromise = emittedOnce(events, 'respond');
|
const hasRespondedPromise = emittedOnce(events, 'respond');
|
||||||
const hasClosedPromise = emittedOnce(events, 'close');
|
const hasClosedPromise = emittedOnce(events, 'close');
|
||||||
ajax(protocolName + '://fake-host');
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
await hasRespondedPromise;
|
await hasRespondedPromise;
|
||||||
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'jquery.html'));
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
|
||||||
await hasClosedPromise;
|
await hasClosedPromise;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -527,7 +528,7 @@ describe('protocol module', () => {
|
||||||
|
|
||||||
it('sends error when callback is called with nothing', async () => {
|
it('sends error when callback is called with nothing', async () => {
|
||||||
interceptStringProtocol('http', (request, callback: any) => callback());
|
interceptStringProtocol('http', (request, callback: any) => callback());
|
||||||
await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error, '404');
|
await expect(ajax('http://fake-host')).to.be.eventually.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -546,8 +547,7 @@ describe('protocol module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const r = await ajax('http://fake-host');
|
const r = await ajax('http://fake-host');
|
||||||
expect(r.data).to.be.an('object');
|
expect(JSON.parse(r.data)).to.have.property('value').that.is.equal(1);
|
||||||
expect(r.data).to.have.property('value').that.is.equal(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can set content-type with charset', async () => {
|
it('can set content-type with charset', async () => {
|
||||||
|
@ -558,8 +558,7 @@ describe('protocol module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const r = await ajax('http://fake-host');
|
const r = await ajax('http://fake-host');
|
||||||
expect(r.data).to.be.an('object');
|
expect(JSON.parse(r.data)).to.have.property('value').that.is.equal(1);
|
||||||
expect(r.data).to.have.property('value').that.is.equal(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can receive post data', async () => {
|
it('can receive post data', async () => {
|
||||||
|
@ -567,7 +566,7 @@ describe('protocol module', () => {
|
||||||
const uploadData = request.uploadData![0].bytes.toString();
|
const uploadData = request.uploadData![0].bytes.toString();
|
||||||
callback({ data: uploadData });
|
callback({ data: uploadData });
|
||||||
});
|
});
|
||||||
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
||||||
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -584,8 +583,8 @@ describe('protocol module', () => {
|
||||||
const uploadData = request.uploadData![0].bytes;
|
const uploadData = request.uploadData![0].bytes;
|
||||||
callback(uploadData);
|
callback(uploadData);
|
||||||
});
|
});
|
||||||
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
||||||
expect(r.data).to.equal('name=post+test&type=string');
|
expect(qs.parse(r.data)).to.deep.equal({ name: 'post test', type: 'string' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -651,7 +650,7 @@ describe('protocol module', () => {
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ajax('http://fake-host');
|
ajax('http://fake-host').catch(() => {});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -666,7 +665,7 @@ describe('protocol module', () => {
|
||||||
interceptStreamProtocol('http', (request, callback) => {
|
interceptStreamProtocol('http', (request, callback) => {
|
||||||
callback(getStream(3, request.uploadData![0].bytes.toString()));
|
callback(getStream(3, request.uploadData![0].bytes.toString()));
|
||||||
});
|
});
|
||||||
const r = await ajax('http://fake-host', { type: 'POST', data: postData });
|
const r = await ajax('http://fake-host', { method: 'POST', body: qs.stringify(postData) });
|
||||||
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
expect({ ...qs.parse(r.data) }).to.deep.equal(postData);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ describe('webRequest module', () => {
|
||||||
let defaultURL: string;
|
let defaultURL: string;
|
||||||
|
|
||||||
before((done) => {
|
before((done) => {
|
||||||
protocol.registerStringProtocol('neworigin', (req, cb) => cb(''));
|
protocol.registerStringProtocol('cors', (req, cb) => cb(''));
|
||||||
server.listen(0, '127.0.0.1', () => {
|
server.listen(0, '127.0.0.1', () => {
|
||||||
const port = (server.address() as AddressInfo).port;
|
const port = (server.address() as AddressInfo).port;
|
||||||
defaultURL = `http://127.0.0.1:${port}/`;
|
defaultURL = `http://127.0.0.1:${port}/`;
|
||||||
|
@ -46,14 +46,14 @@ describe('webRequest module', () => {
|
||||||
|
|
||||||
after(() => {
|
after(() => {
|
||||||
server.close();
|
server.close();
|
||||||
protocol.unregisterProtocol('neworigin');
|
protocol.unregisterProtocol('cors');
|
||||||
});
|
});
|
||||||
|
|
||||||
let contents: WebContents = null as unknown as WebContents;
|
let contents: WebContents = null as unknown as WebContents;
|
||||||
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
|
// NB. sandbox: true is used because it makes navigations much (~8x) faster.
|
||||||
before(async () => {
|
before(async () => {
|
||||||
contents = (webContents as any).create({ sandbox: true });
|
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());
|
after(() => (contents as any).destroy());
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ describe('webRequest module', () => {
|
||||||
cancel: true
|
cancel: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await expect(ajax(defaultURL)).to.eventually.be.rejectedWith('404');
|
await expect(ajax(defaultURL)).to.eventually.be.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can filter URLs', async () => {
|
it('can filter URLs', async () => {
|
||||||
|
@ -82,7 +82,7 @@ describe('webRequest module', () => {
|
||||||
});
|
});
|
||||||
const { data } = await ajax(`${defaultURL}nofilter/test`);
|
const { data } = await ajax(`${defaultURL}nofilter/test`);
|
||||||
expect(data).to.equal('/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 () => {
|
it('receives details object', async () => {
|
||||||
|
@ -117,9 +117,9 @@ describe('webRequest module', () => {
|
||||||
callback({ cancel: true });
|
callback({ cancel: true });
|
||||||
});
|
});
|
||||||
await expect(ajax(defaultURL, {
|
await expect(ajax(defaultURL, {
|
||||||
type: 'POST',
|
method: 'POST',
|
||||||
data: postData
|
body: qs.stringify(postData)
|
||||||
})).to.eventually.be.rejectedWith('404');
|
})).to.eventually.be.rejected();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can redirect the request', async () => {
|
it('can redirect the request', async () => {
|
||||||
|
@ -151,7 +151,7 @@ describe('webRequest module', () => {
|
||||||
protocol: 'file',
|
protocol: 'file',
|
||||||
slashes: true
|
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 () => {
|
it('can change the request headers on a custom protocol redirect', async () => {
|
||||||
protocol.registerStringProtocol('custom-scheme', (req, callback) => {
|
protocol.registerStringProtocol('no-cors', (req, callback) => {
|
||||||
if (req.url === 'custom-scheme://fake-host/redirect') {
|
if (req.url === 'no-cors://fake-host/redirect') {
|
||||||
callback({
|
callback({
|
||||||
statusCode: 302,
|
statusCode: 302,
|
||||||
headers: {
|
headers: {
|
||||||
Location: 'custom-scheme://fake-host'
|
Location: 'no-cors://fake-host'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -202,7 +202,7 @@ describe('webRequest module', () => {
|
||||||
// Note that we need to do navigation every time after a protocol is
|
// Note that we need to do navigation every time after a protocol is
|
||||||
// registered or unregistered, otherwise the new protocol won't be
|
// registered or unregistered, otherwise the new protocol won't be
|
||||||
// recognized by current page when NetworkService is used.
|
// 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 {
|
try {
|
||||||
ses.webRequest.onBeforeSendHeaders((details, callback) => {
|
ses.webRequest.onBeforeSendHeaders((details, callback) => {
|
||||||
|
@ -210,10 +210,10 @@ describe('webRequest module', () => {
|
||||||
requestHeaders.Accept = '*/*;test/header';
|
requestHeaders.Accept = '*/*;test/header';
|
||||||
callback({ requestHeaders: requestHeaders });
|
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');
|
expect(data).to.equal('header-received');
|
||||||
} finally {
|
} finally {
|
||||||
protocol.unregisterProtocol('custom-scheme');
|
protocol.unregisterProtocol('no-cors');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -233,7 +233,7 @@ describe('webRequest module', () => {
|
||||||
called = true;
|
called = true;
|
||||||
callback({ requestHeaders: details.requestHeaders });
|
callback({ requestHeaders: details.requestHeaders });
|
||||||
});
|
});
|
||||||
await ajax('neworigin://host');
|
await ajax('cors://host');
|
||||||
expect(called).to.be.true();
|
expect(called).to.be.true();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -320,7 +320,7 @@ describe('webRequest module', () => {
|
||||||
callback({ responseHeaders: responseHeaders });
|
callback({ responseHeaders: responseHeaders });
|
||||||
});
|
});
|
||||||
const { headers } = await ajax(defaultURL);
|
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 () => {
|
it('can change response origin', async () => {
|
||||||
|
@ -330,7 +330,7 @@ describe('webRequest module', () => {
|
||||||
callback({ responseHeaders: responseHeaders });
|
callback({ responseHeaders: responseHeaders });
|
||||||
});
|
});
|
||||||
const { headers } = await ajax(defaultURL);
|
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 () => {
|
it('can change headers of CORS responses', async () => {
|
||||||
|
@ -339,8 +339,8 @@ describe('webRequest module', () => {
|
||||||
responseHeaders.Custom = ['Changed'] as any;
|
responseHeaders.Custom = ['Changed'] as any;
|
||||||
callback({ responseHeaders: responseHeaders });
|
callback({ responseHeaders: responseHeaders });
|
||||||
});
|
});
|
||||||
const { headers } = await ajax('neworigin://host');
|
const { headers } = await ajax('cors://host');
|
||||||
expect(headers).to.match(/^custom: Changed$/m);
|
expect(headers).to.to.have.property('custom', 'Changed');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not change header by default', async () => {
|
it('does not change header by default', async () => {
|
||||||
|
@ -348,7 +348,7 @@ describe('webRequest module', () => {
|
||||||
callback({});
|
callback({});
|
||||||
});
|
});
|
||||||
const { data, headers } = await ajax(defaultURL);
|
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('/');
|
expect(data).to.equal('/');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -358,7 +358,7 @@ describe('webRequest module', () => {
|
||||||
callback({});
|
callback({});
|
||||||
});
|
});
|
||||||
const { data, headers } = await ajax(defaultURL + 'contentDisposition');
|
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');
|
expect(data).to.equal('/contentDisposition');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -368,7 +368,7 @@ describe('webRequest module', () => {
|
||||||
callback({ responseHeaders: responseHeaders });
|
callback({ responseHeaders: responseHeaders });
|
||||||
});
|
});
|
||||||
const { headers } = await ajax(defaultURL + 'serverRedirect');
|
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 () => {
|
it('can change the header status', async () => {
|
||||||
|
@ -379,19 +379,8 @@ describe('webRequest module', () => {
|
||||||
statusLine: 'HTTP/1.1 404 Not Found'
|
statusLine: 'HTTP/1.1 404 Not Found'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const { headers } = await contents.executeJavaScript(`new Promise((resolve, reject) => {
|
const { headers } = await ajax(defaultURL);
|
||||||
const options = {
|
expect(headers).to.to.have.property('custom', 'Header');
|
||||||
...${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);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -408,7 +397,7 @@ describe('webRequest module', () => {
|
||||||
expect(details.responseHeaders!.Custom).to.deep.equal(['Header']);
|
expect(details.responseHeaders!.Custom).to.deep.equal(['Header']);
|
||||||
});
|
});
|
||||||
const { data, headers } = await ajax(defaultURL);
|
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('/');
|
expect(data).to.equal('/');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -468,7 +457,7 @@ describe('webRequest module', () => {
|
||||||
ses.webRequest.onErrorOccurred((details) => {
|
ses.webRequest.onErrorOccurred((details) => {
|
||||||
expect(details.error).to.equal('net::ERR_BLOCKED_BY_CLIENT');
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
15
spec-main/fixtures/pages/fetch.html
Normal file
15
spec-main/fixtures/pages/fetch.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
window.ajax = async (url, options) => {
|
||||||
|
const response = await fetch(url, options);
|
||||||
|
const data = await response.text();
|
||||||
|
const headers = {};
|
||||||
|
for (const [key, value] of response.headers.entries()) {
|
||||||
|
headers[key] = value;
|
||||||
|
}
|
||||||
|
return {data, status: response.status, headers};
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
2
spec-main/fixtures/pages/jquery-3.6.0.min.js
vendored
Normal file
2
spec-main/fixtures/pages/jquery-3.6.0.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,21 +1,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<script src="../../../spec/static/jquery-2.0.3.min.js"></script>
|
<script src="jquery-3.6.0.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script>
|
|
||||||
window.ajax = (url, options) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
options.url = url
|
|
||||||
options.success = (data, status, request) => {
|
|
||||||
resolve({data, status: request.status, headers: request.getAllResponseHeaders()})
|
|
||||||
}
|
|
||||||
options.error = (xhr, errorType, error) => {
|
|
||||||
reject(error ? error : new Error(`${xhr.status}`))
|
|
||||||
}
|
|
||||||
$.ajax(options)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
"env": {
|
"env": {
|
||||||
"browser": true,
|
"browser": true,
|
||||||
"mocha": true,
|
"mocha": true,
|
||||||
"jquery": true,
|
|
||||||
"serviceworker": true
|
"serviceworker": true
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
|
|
|
@ -1599,67 +1599,42 @@ describe('asar package', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('asar protocol', function () {
|
describe('asar protocol', function () {
|
||||||
it('can request a file in package', function (done) {
|
it('can request a file in package', async function () {
|
||||||
const p = path.resolve(asarDir, 'a.asar', 'file1');
|
const p = path.resolve(asarDir, 'a.asar', 'file1');
|
||||||
$.get('file://' + p, function (data) {
|
const response = await fetch('file://' + p);
|
||||||
try {
|
const data = await response.text();
|
||||||
expect(data.trim()).to.equal('file1');
|
expect(data.trim()).to.equal('file1');
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can request a file in package with unpacked files', function (done) {
|
it('can request a file in package with unpacked files', async function () {
|
||||||
const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
|
const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
|
||||||
$.get('file://' + p, function (data) {
|
const response = await fetch('file://' + p);
|
||||||
try {
|
const data = await response.text();
|
||||||
expect(data.trim()).to.equal('a');
|
expect(data.trim()).to.equal('a');
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can request a linked file in package', function (done) {
|
it('can request a linked file in package', async function () {
|
||||||
const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
|
const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
|
||||||
$.get('file://' + p, function (data) {
|
const response = await fetch('file://' + p);
|
||||||
try {
|
const data = await response.text();
|
||||||
expect(data.trim()).to.equal('file1');
|
expect(data.trim()).to.equal('file1');
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can request a file in filesystem', function (done) {
|
it('can request a file in filesystem', async function () {
|
||||||
const p = path.resolve(asarDir, 'file');
|
const p = path.resolve(asarDir, 'file');
|
||||||
$.get('file://' + p, function (data) {
|
const response = await fetch('file://' + p);
|
||||||
try {
|
const data = await response.text();
|
||||||
expect(data.trim()).to.equal('file');
|
expect(data.trim()).to.equal('file');
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('gets 404 when file is not found', function (done) {
|
it('gets error when file is not found', async function () {
|
||||||
const p = path.resolve(asarDir, 'a.asar', 'no-exist');
|
const p = path.resolve(asarDir, 'a.asar', 'no-exist');
|
||||||
$.ajax({
|
|
||||||
url: 'file://' + p,
|
|
||||||
error: function (err) {
|
|
||||||
try {
|
try {
|
||||||
expect(err.status).to.equal(404);
|
const response = await fetch('file://' + p);
|
||||||
done();
|
expect(response.status).to.equal(404);
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
done(e);
|
expect(error.message).to.equal('Failed to fetch');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
27
spec/fixtures/pages/basic-auth.html
vendored
27
spec/fixtures/pages/basic-auth.html
vendored
|
@ -1,22 +1,21 @@
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script src="../../static/jquery-2.0.3.min.js"></script>
|
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
var ipcRenderer = require('electron').ipcRenderer;
|
const { ipcRenderer } = require('electron');
|
||||||
var port = location.search.substr("?port=".length);
|
const url = new URL(location.href);
|
||||||
$.ajax({
|
const port = url.searchParams.get('port');
|
||||||
type: "GET",
|
(async () => {
|
||||||
url: "http://127.0.0.1:" + port,
|
try {
|
||||||
|
const response = await fetch(`http://127.0.0.1:${port}`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": "Basic " + btoa("test:test")
|
'Authorization': `Basic ${btoa('test:test')}`
|
||||||
},
|
}
|
||||||
success: function(result) {
|
|
||||||
ipcRenderer.sendToHost(result);
|
|
||||||
},
|
|
||||||
error: function(xhr, status, error) {
|
|
||||||
ipcRenderer.sendToHost(error);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
ipcRenderer.sendToHost(await response.text());
|
||||||
|
} catch (error) {
|
||||||
|
ipcRenderer.sendToHost(error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
4
spec/fixtures/pages/dom-ready.html
vendored
4
spec/fixtures/pages/dom-ready.html
vendored
|
@ -1,8 +1,8 @@
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<script src="../../static/jquery-2.0.3.min.js"></script>
|
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
var port = location.search.substr("?port=".length);
|
const url = new URL(location.href);
|
||||||
|
const port = url.searchParams.get('port');
|
||||||
document.write("<img src='http://127.0.0.1:" + port + "/logo.png' />");
|
document.write("<img src='http://127.0.0.1:" + port + "/logo.png' />");
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<body>
|
<body>
|
||||||
<script src="jquery-2.0.3.min.js"></script>
|
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
(async function() {
|
(async function() {
|
||||||
// Deprecated APIs are still supported and should be tested.
|
// Deprecated APIs are still supported and should be tested.
|
||||||
|
|
6
spec/static/jquery-2.0.3.min.js
vendored
6
spec/static/jquery-2.0.3.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue