Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
fix(stateConfig): use ip when useHost flag is passed (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
infoxicator authored Oct 6, 2020
1 parent 729879b commit 4ca50c4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions __tests__/server/middleware/pwa/offline.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { configurePWA } from '../../../../src/server/middleware/pwa/config';

import createOfflineMiddleware from '../../../../src/server/middleware/pwa/offline';

jest.mock('yargs', () => ({ argv: {} })); // we are importing yargs in stateConfig and needs to be mocked
jest.mock('../../../../src/server/middleware/sendHtml', () => jest.fn());
jest.mock('fs', () => ({
existsSync: () => false,
Expand Down
12 changes: 12 additions & 0 deletions __tests__/server/utils/__snapshots__/stateConfig.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stateConfig methods stateConfig with dev endpoint config dev endpoints should use ip instead of localhost when useHost is passed as arg 1`] = `
Object {
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
}
`;

exports[`stateConfig methods stateConfig with dev endpoint config dev endpoints should use ip instead of localhost when useHost is passed as arg 2`] = `
Object {
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
}
`;

exports[`stateConfig methods stateConfig with dev endpoint config should show dev endpoint supplied config 1`] = `
Object {
"someOtherApiUrl": "http://localhost:3002/some-other-api",
Expand Down
26 changes: 26 additions & 0 deletions __tests__/server/utils/stateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

jest.mock('fs', () => ({ existsSync: jest.fn() }));
jest.mock('ip', () => ({ address: jest.fn() }));
jest.mock('fake/path/.dev/endpoints/index.js', () => jest.fn(), { virtual: true });
jest.mock('yargs', () => ({ argv: {} }));
jest.mock('../../../src/server/utils/envVarAllowList', () => [
'ONE_CLIENT_FAKE_SETTING',
'ONE_FAKE_SETTING',
Expand All @@ -32,12 +34,18 @@ describe('stateConfig methods', () => {
let restoreModuleStateConfig;
let backupModuleStateConfig;
let fs;
let ip;
let yargs;

const originalEnvVars = process.env;

const reloadMocks = () => {
fs = require('fs');
ip = require('ip');
yargs = require('yargs');
jest.spyOn(process, 'cwd').mockImplementation(() => 'fake/path/');
ip.address.mockImplementation(() => '127.0.0.1');
yargs.argv = {};
fs.existsSync.mockImplementation(() => false);
process.env.ONE_CONFIG_ENV = 'qa';
provideStateConfig = {
Expand Down Expand Up @@ -191,6 +199,24 @@ describe('stateConfig methods', () => {
expect(getClientStateConfig()).toMatchSnapshot();
expect(getServerStateConfig()).toMatchSnapshot();
});
it('dev endpoints should use ip instead of localhost when useHost is passed as arg', () => {
process.env.NODE_ENV = 'development';
yargs.argv = { useHost: true };
// eslint-disable-next-line unicorn/import-index, import/no-unresolved
require('fake/path/.dev/endpoints/index.js').mockImplementation(() => ({
someOtherApiUrl: {
devProxyPath: 'some-other-api',
destination: 'https://intranet-origin-dev.example.com/some-other-api/v1',
},
}));
({
setStateConfig,
getClientStateConfig,
getServerStateConfig,
} = require('../../../src/server/utils/stateConfig'));
expect(getClientStateConfig()).toMatchSnapshot();
expect(getServerStateConfig()).toMatchSnapshot();
});
it('dev endpoint should not have doubled slash in path', () => {
process.env.NODE_ENV = 'development';
// eslint-disable-next-line unicorn/import-index, import/no-unresolved
Expand Down
4 changes: 3 additions & 1 deletion src/server/utils/stateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import fs from 'fs';
import path from 'path';
import url from 'url';
import ip from 'ip';
import { argv } from 'yargs';
import envVarAllowList from './envVarAllowList';
import snakeCaseToCamelCase from './snakeCaseToCamelCase';

Expand Down Expand Up @@ -77,7 +79,7 @@ if (process.env.NODE_ENV === 'development' && fs.existsSync(pathToDevEndpoints))
Object.entries(devEndpoints).forEach(([configName, { devProxyPath }]) => {
const value = url.format({
protocol: 'http',
hostname: 'localhost',
hostname: argv.useHost ? `${ip.address()}` : 'localhost',
port: HTTP_ONE_APP_DEV_PROXY_SERVER_PORT,
pathname: devProxyPath,
});
Expand Down

0 comments on commit 4ca50c4

Please sign in to comment.