Skip to content

Commit

Permalink
fix(amplify-provider-awscloudformation): specify 127.0.0.1 as hostname (
Browse files Browse the repository at this point in the history
#6780)

* fix(amplify-provider-awscloudformation): specify 0.0.0.0 as hostname for adminui login server to force listening on ipv4 instead of ipv6 when available.
  • Loading branch information
watilde committed Mar 3, 2021
1 parent 249d0cf commit db8b557
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AdminLoginServer } from '../../utils/admin-login-server';
import { $TSContext } from 'amplify-cli-core';
import { FunctionRuntimeLifecycleManager } from 'amplify-function-plugin-interface';

const runtimePlugin_stub = ({
checkDependencies: jest.fn().mockResolvedValue({ hasRequiredDependencies: true }),
build: jest.fn().mockResolvedValue({ rebuilt: true }),
} as unknown) as jest.Mocked<FunctionRuntimeLifecycleManager>;

const context_stub = ({
amplify: {
readBreadcrumbs: jest.fn().mockReturnValue({ pluginId: 'testPluginId' }),
loadRuntimePlugin: jest.fn().mockResolvedValue(runtimePlugin_stub),
updateamplifyMetaAfterBuild: jest.fn(),
},
} as unknown) as jest.Mocked<$TSContext>;

const useMock = jest.fn();
const postMock = jest.fn(async () => {});
const listenMock = jest.fn();

jest.mock('express', () => {
return () => {
return {
use: useMock,
post: postMock,
listen: listenMock,
};
};
});

describe('AdminLoginServer', () => {
test('run server with 0.0.0.0', async () => {
const adminLoginServer = new AdminLoginServer('appId', 'http://example.com', context_stub.print);

await new Promise<void>(resolve => {
adminLoginServer.startServer(() => {});
resolve();
});
expect(useMock).toBeCalled();
expect(postMock).toBeCalled();
expect(listenMock).toBeCalledWith(4242, '0.0.0.0');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class AdminLoginServer {
private port = 4242; // placeholder
private server: http.Server;
private print: $TSContext['print'];
private host = '0.0.0.0'; // using this ip address for the host forces express to listen on IPV4 even if IPV6 is available

private corsOptions: {
origin: string[];
Expand All @@ -38,7 +39,12 @@ export class AdminLoginServer {

public async startServer(callback: () => void) {
await this.setupRoute(callback);
this.server = this.app.listen(this.getPort());
// Need to specify hostname for WSL
this.server = this.app.listen(this.getPort(), this.getHost());
}

private getHost() {
return this.host;
}

// TODO: scan for available ports across a range like mock
Expand Down

0 comments on commit db8b557

Please sign in to comment.