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

fix(stateConfig): use localhost rather than ip for dev #63

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions __tests__/server/utils/__snapshots__/stateConfig.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

exports[`stateConfig methods stateConfig with dev endpoint config should show dev endpoint supplied config 1`] = `
Object {
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
"someOtherApiUrl": "http://localhost:3002/some-other-api",
}
`;

exports[`stateConfig methods stateConfig with dev endpoint config should show dev endpoint supplied config 2`] = `
Object {
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
"someOtherApiUrl": "http://localhost:3002/some-other-api",
}
`;

Expand Down Expand Up @@ -156,15 +156,15 @@ exports[`stateConfig methods stateConfig with priorities of should prioritize de
Object {
"anotherSetting": "another-fake-setting",
"fakeSetting": "client-fake-setting",
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
"someOtherApiUrl": "http://localhost:3002/some-other-api",
}
`;

exports[`stateConfig methods stateConfig with priorities of should prioritize dev endpoint over env 2`] = `
Object {
"anotherSetting": "another-fake-setting",
"fakeSetting": "server-fake-setting",
"someOtherApiUrl": "http://127.0.0.1:3002/some-other-api",
"someOtherApiUrl": "http://localhost:3002/some-other-api",
}
`;

Expand Down
6 changes: 1 addition & 5 deletions __tests__/server/utils/stateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

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('../../../src/server/utils/envVarAllowList', () => [
'ONE_CLIENT_FAKE_SETTING',
Expand All @@ -33,15 +32,12 @@ describe('stateConfig methods', () => {
let restoreModuleStateConfig;
let backupModuleStateConfig;
let fs;
let ip;

const originalEnvVars = process.env;

const reloadMocks = () => {
fs = require('fs');
ip = require('ip');
jest.spyOn(process, 'cwd').mockImplementation(() => 'fake/path/');
ip.address.mockImplementation(() => '127.0.0.1');
fs.existsSync.mockImplementation(() => false);
process.env.ONE_CONFIG_ENV = 'qa';
provideStateConfig = {
Expand Down Expand Up @@ -209,7 +205,7 @@ describe('stateConfig methods', () => {
getClientStateConfig,
getServerStateConfig,
} = require('../../../src/server/utils/stateConfig'));
expect(getClientStateConfig().leadingSlashApiUrl).toEqual('http://127.0.0.1:3002/leading-slash-api');
expect(getClientStateConfig().leadingSlashApiUrl).toEqual('http://localhost:3002/leading-slash-api');
});
});
describe('with env vars', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/server/utils/stateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* permissions and limitations under the License.
*/

import ip from 'ip';
import fs from 'fs';
import path from 'path';
import url from 'url';
Expand Down Expand Up @@ -72,14 +71,13 @@ const pathToDevEndpoints = path.join(process.cwd(), '.dev', 'endpoints', 'index.
const stateConfigFromDevEndpoints = {};

if (process.env.NODE_ENV === 'development' && fs.existsSync(pathToDevEndpoints)) {
const ipAddress = ip.address();
const { SERVICES_PORT = 3002 } = process.env;
// eslint-disable-next-line global-require,import/no-dynamic-require
const devEndpoints = require(pathToDevEndpoints)();
Object.entries(devEndpoints).forEach(([configName, { devProxyPath }]) => {
const value = url.format({
protocol: 'http',
hostname: ipAddress,
hostname: 'localhost',
port: SERVICES_PORT,
pathname: devProxyPath,
});
Expand Down