diff --git a/src/cli.ts b/src/cli.ts index ce23431..5023d0f 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -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], @@ -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 diff --git a/test/lambdaHandlers.test.ts b/test/lambdaHandlers.test.ts index eec40b4..d587369 100644 --- a/test/lambdaHandlers.test.ts +++ b/test/lambdaHandlers.test.ts @@ -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(); diff --git a/test/proxy.test.ts b/test/proxy.test.ts index 0472d22..3170d5c 100644 --- a/test/proxy.test.ts +++ b/test/proxy.test.ts @@ -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; expect(Object.keys(headers).sort()).toEqual(['accept', 'connection', 'host', 'test-header', 'user-agent']); @@ -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"}'); @@ -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 { @@ -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) => { diff --git a/test/utils.ts b/test/utils.ts index 12ffe6b..97c940a 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -56,11 +56,12 @@ export const spawnProxy = (...args: string[]): Promise { 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'))); }); }; @@ -87,10 +88,15 @@ export const getPort = async (): Promise => { 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);