Skip to content

Commit

Permalink
Fix tests that were relying on ipv4 addresses. Only use localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
David Tanner committed Sep 29, 2022
1 parent 10f9eaf commit 7512178
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ValidationError } from './ValidationError';
import { alphaProxy } from './alphaProxy';
import { AlphaCliConfig } from './types';
import { callAlpha } from './utils';
import { AddressInfo } from 'net';

const config: AlphaCliConfig = {
transformResponse: [(data) => data],
Expand All @@ -32,13 +33,13 @@ const run = async () => {

const {
proxied,
proxyPort,
} = config;

if (proxied) {
alphaProxy(config).on('listening', () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
console.log(`Proxy is listening on port ${proxyPort!}; Press any key to quit;`);
const srv = alphaProxy(config);
srv.on('listening', () => {
const { address, port } = srv.address() as AddressInfo;
console.log(`Proxy is listening on port ${address}:${port}; Press any key to quit;`);
});

// These are only relevant in a terminal, not in tests
Expand Down
2 changes: 1 addition & 1 deletion test/lambdaHandlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe.each([
const process = await spawnProxy('--proxy', '--proxy-port', proxyPort, '--lambda-handler', filePath);

try {
const { stdout, stderr } = await runCommand(`http://127.0.0.1:${proxyPort}/echo/${param}`);
const { stdout, stderr } = await runCommand(`http://localhost:${proxyPort}/echo/${param}`);

expect(stdout).toBe(param);
expect(stderr).toBeFalsy();
Expand Down
8 changes: 4 additions & 4 deletions test/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('The --proxy flag starts a proxy to send commands to alpha', async () => {
const process = await spawnProxy('--proxy', '--proxy-port', proxyPort, context.url);

try {
const { stdout, stderr } = await runCommand('-H', 'Test-Header: header value', `http://127.0.0.1:${proxyPort}/headerTest`);
const { stdout, stderr } = await runCommand('-H', 'Test-Header: header value', `http://localhost:${proxyPort}/headerTest`);
const headers = JSON.parse(stdout) as Record<string, string>;

expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'test-header', 'user-agent']);
Expand All @@ -59,7 +59,7 @@ test('The proxy passes data', async () => {
'--data-binary', '{"message":"hello"}',
'--header', 'Content-Type: text/plain',
'--request', 'POST',
`http://127.0.0.1:${proxyPort}/dataTest`,
`http://localhost:${proxyPort}/dataTest`,
);

expect(stdout).toBe('{"message":"hello"}');
Expand All @@ -78,7 +78,7 @@ test('The proxy handles errors', async () => {
'--data-binary', '{"message":"hello"}',
'--header', 'Content-Type: text/plain',
'--request', 'POST',
`http://127.0.0.1:${proxyPort}/derp`,
`http://localhost:${proxyPort}/derp`,
);
expect(stdout).toMatch(/Error: connect/);
} finally {
Expand All @@ -89,7 +89,7 @@ test('The proxy handles errors', async () => {
test('The proxy ends if the user presses a key', async () => {
const proxyPort = await getPort();

const process = await spawnProxy('--proxy', '--proxy-port', proxyPort, `http://127.0.0.1:${proxyPort}/headerTest`);
const process = await spawnProxy('--proxy', '--proxy-port', proxyPort, `http://localhost:${proxyPort}/headerTest`);
try {
process.stdin.write('q\n');
await new Promise((resolve) => {
Expand Down
16 changes: 11 additions & 5 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ export const spawnProxy = (...args: string[]): Promise<ChildProcessWithoutNullSt
return new Promise((resolve, reject) => {
const child = spawnCli(args);

child.stdout.on('data', () => {
child.stdout.once('data', () => {
resolve(child);
});
child.once('error', reject);
child.stderr.on('data', (chunk: Buffer) => console.log(Buffer.from(chunk).toString('utf-8')));
child.stderr.on('data', (chunk: Buffer) => console.error(Buffer.from(chunk).toString('utf-8')));
child.stdout.on('data', (chunk: Buffer) => console.log(Buffer.from(chunk).toString('utf-8')));
});
};

Expand All @@ -87,10 +88,15 @@ export const getPort = async (): Promise<string> => {
try {
const srv = net.createServer(() => {
});
srv.listen(0, () => {
srv.listen(0, 'localhost', () => {
const { port } = (srv.address() as net.AddressInfo);
srv.close((err) => reject(err));
return resolve(`${port}`);
srv.close((err) => {
if (err) {
reject(err);
} else {
resolve(`${port}`);
}
});
});
} catch (e) {
reject(e);
Expand Down

0 comments on commit 7512178

Please sign in to comment.