diff --git a/spec/api-protocol-spec.ts b/spec/api-protocol-spec.ts index 122cc4e84f0da..281a50dc55463 100644 --- a/spec/api-protocol-spec.ts +++ b/spec/api-protocol-spec.ts @@ -26,6 +26,7 @@ const interceptStringProtocol = protocol.interceptStringProtocol; const interceptBufferProtocol = protocol.interceptBufferProtocol; const interceptHttpProtocol = protocol.interceptHttpProtocol; const interceptStreamProtocol = protocol.interceptStreamProtocol; +const interceptProtocol: (scheme: string, handler: (req: Electron.ProtocolRequest, callback: (res?: Electron.ProtocolResponse) => void) => void) => boolean = protocol.interceptProtocol; const unregisterProtocol = protocol.unregisterProtocol; const uninterceptProtocol = protocol.uninterceptProtocol; @@ -750,6 +751,78 @@ describe('protocol module', () => { }); }); + describe('protocol.interceptProtocol', () => { + const text = 'Hi, Chaofan!'; + it('callback with null can behave like no intercept', async () => { + const server = http.createServer((req, res) => { + res.end(text); + }); + after(() => server.close()); + const { url } = await listen(server); + interceptProtocol('http', (req, callback) => { + callback(); + }); + await contents.loadURL(url); + expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal(text); + }); + + it('callback with {data} can response directly', async () => { + interceptProtocol('http', (req, callback) => { + callback({ + data: 'hello' + }); + }); + await contents.loadURL('http://foo'); + expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal('hello'); + }); + + it('callback with null can behave like no intercept - redirect case', async () => { + const server = http.createServer((req, res) => { + if (req.url === '/serverRedirect') { + console.log(req.rawHeaders); + res.statusCode = 301; + res.setHeader('Location', `${url}/foo`); + res.end(); + } else { + res.end(text); + } + }); + after(() => server.close()); + const { url } = await listen(server); + interceptProtocol('http', (req, callback) => { + callback(); + }); + await contents.loadURL(`${url}/serverRedirect`); + // Redirect should change the page url if not We may met the situation that returns data from page B to page A. + expect(await contents.getURL()).to.equal(`${url}/foo`); + expect(await contents.executeJavaScript('document.documentElement.textContent')).to.equal(text); + }); + + it('callback with null can behave like no intercept - post case', async () => { + const server = http.createServer((req, res) => { + let body = ''; + req.on('data', (chunk) => { + body += chunk; + }); + req.on('end', () => { + console.log(body); + res.end(body); + }); + }); + after(() => server.close()); + const { url } = await listen(server); + interceptProtocol('http', (req, callback) => { + callback(); + }); + + const r = await ajax(url, { + method: 'POST', + body: text + }); + expect(r.data).to.equal(text); + }); + }); + describe('protocol.uninterceptProtocol', () => { it('returns false when scheme does not exist', () => { expect(uninterceptProtocol('not-exist')).to.equal(false);