mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: protocol.handle not intercepting file protocol (#39064)
fix: protocol.handle not intercepting file protocol Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
@@ -9,7 +9,7 @@ const { registerSchemesAsPrivileged, getStandardSchemes, Protocol } = process._l
|
||||
const ERR_FAILED = -2;
|
||||
const ERR_UNEXPECTED = -9;
|
||||
|
||||
const isBuiltInScheme = (scheme: string) => scheme === 'http' || scheme === 'https';
|
||||
const isBuiltInScheme = (scheme: string) => ['http', 'https', 'file'].includes(scheme);
|
||||
|
||||
function makeStreamFromPipe (pipe: any): ReadableStream {
|
||||
const buf = new Uint8Array(1024 * 1024 /* 1 MB */);
|
||||
|
||||
@@ -1127,13 +1127,34 @@ describe('protocol module', () => {
|
||||
await expect(net.fetch('test-scheme://foo')).to.eventually.be.rejectedWith(/ERR_UNKNOWN_URL_SCHEME/);
|
||||
});
|
||||
|
||||
it('receives requests to an existing scheme', async () => {
|
||||
it('receives requests to the existing https scheme', async () => {
|
||||
protocol.handle('https', (req) => new Response('hello ' + req.url));
|
||||
defer(() => { protocol.unhandle('https'); });
|
||||
const body = await net.fetch('https://foo').then(r => r.text());
|
||||
expect(body).to.equal('hello https://foo/');
|
||||
});
|
||||
|
||||
it('receives requests to the existing file scheme', (done) => {
|
||||
const filePath = path.join(__dirname, 'fixtures', 'pages', 'a.html');
|
||||
|
||||
protocol.handle('file', (req) => {
|
||||
let file;
|
||||
if (process.platform === 'win32') {
|
||||
file = `file:///${filePath.replace(/\\/g, '/')}`;
|
||||
} else {
|
||||
file = `file://${filePath}`;
|
||||
}
|
||||
|
||||
if (req.url === file) done();
|
||||
return new Response(req.url);
|
||||
});
|
||||
|
||||
defer(() => { protocol.unhandle('file'); });
|
||||
|
||||
const w = new BrowserWindow();
|
||||
w.loadFile(filePath);
|
||||
});
|
||||
|
||||
it('receives requests to an existing scheme when navigating', async () => {
|
||||
protocol.handle('https', (req) => new Response('hello ' + req.url));
|
||||
defer(() => { protocol.unhandle('https'); });
|
||||
|
||||
Reference in New Issue
Block a user