Skip to content

Commit

Permalink
feat: Add test for interceptProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchaofan committed Apr 23, 2024
1 parent 0b259b2 commit e051b5d
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions spec/api-protocol-spec.ts
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e051b5d

Please sign in to comment.