Skip to content

Commit

Permalink
test(request-manager): native fetch tests interceptor-whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
karliatto committed Jan 23, 2025
1 parent 6235ff3 commit 5c7df5b
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions packages/request-manager/tests/interceptor-whitelist.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WebSocket from 'ws';
import fetch from 'node-fetch';
import nodeFetch from 'node-fetch';
import net, { Socket } from 'net';
import tls, { TLSSocket } from 'tls';

Expand Down Expand Up @@ -73,42 +73,42 @@ describe('Interceptor', () => {
});

['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach(method => {
it(`Blocks all not whitelisted requests for: ${method}`, async () => {
it(`Blocks all not whitelisted node-fetch requests for: ${method}`, async () => {
await expect(
fetch(`https://${WHITELISTED_DOMAIN}/`, { method }),
nodeFetch(`https://${WHITELISTED_DOMAIN}/`, { method }),
).resolves.toBeDefined();

await expect(fetch(`https://${NOT_WHITELISTED_DOMAIN}/`, { method })).rejects.toThrow(
`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`,
);
await expect(
nodeFetch(`https://${NOT_WHITELISTED_DOMAIN}/`, { method }),
).rejects.toThrow(`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`);
});
});

it('blocks the TCP connection', async () => {
it('Blocks the TCP connection', async () => {
(await openTcpSocket(WHITELISTED_DOMAIN, 80)).end();

await expect(openTcpSocket(NOT_WHITELISTED_DOMAIN, 80)).rejects.toThrow(
`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`,
);
});

it('blocks the TLS connection', async () => {
it('Blocks the TLS connection', async () => {
(await openTlsSocket(WHITELISTED_DOMAIN, 80)).end();

await expect(openTlsSocket(NOT_WHITELISTED_DOMAIN, 80)).rejects.toThrow(
`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`,
);
});

it('blocks net.Socket', async () => {
it('Blocks net.Socket', async () => {
(await openNetSocket(WHITELISTED_DOMAIN, 80)).end();

await expect(openNetSocket(NOT_WHITELISTED_DOMAIN, 80)).rejects.toThrow(
`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`,
);
});

it('blocks net.connect', async () => {
it('Blocks net.connect', async () => {
(await performNetConnect(WHITELISTED_DOMAIN, 80)).end();

try {
Expand All @@ -121,7 +121,7 @@ describe('Interceptor', () => {
}
});

it('blocks tls.connect', async () => {
it('Blocks tls.connect', async () => {
(await performTlsConnect(WHITELISTED_DOMAIN, 80)).end();

try {
Expand All @@ -133,4 +133,21 @@ describe('Interceptor', () => {
);
}
});

['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].forEach(method => {
it(`Blocks all not whitelisted native fetch for: ${method}`, async () => {
await expect(
fetch(`https://${WHITELISTED_DOMAIN}/`, { method }),
).resolves.toBeDefined();

try {
await fetch(`https://${NOT_WHITELISTED_DOMAIN}/`, { method });
expect('').toBe('Should throw an error');
} catch (error) {
expect(error.message).toBe(
`Request blocked, not whitelisted domain: ${NOT_WHITELISTED_DOMAIN}`,
);
}
});
});
});

0 comments on commit 5c7df5b

Please sign in to comment.