Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/lib/get-api-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getApiUrl = (cmdArgs, env = {}) => {
// behaviour as it is connecting to.
// See ./archiving-db.js
instanceUrl = parseLocalUrl(env.COUCH_URL);
if (instanceUrl.hostname !== 'localhost') {
if (instanceUrl.hostname !== 'localhost' && instanceUrl.hostname !== '127.0.0.1') {
throw Error(
`--local was specified but COUCH_URL env var is set to '${instanceUrl.hostname}'. `
+ `Please use --url for remote servers.`
Expand Down Expand Up @@ -55,10 +55,10 @@ const parseLocalUrl = (couchUrl) => {
const doParse = (unparsed) => {
const parsed = new url.URL(unparsed);
parsed.path = parsed.pathname = '';
parsed.host = `${parsed.hostname}:5988`;
parsed.host = parsed.hostname.includes('localhost')? `127.0.0.1:5988`: `${parsed.hostname}:5988`;
return new url.URL(url.format(parsed));
};

if (couchUrl) {
info(`Using local url from COUCH_URL environment variable: ${couchUrl}`);
return doParse(couchUrl);
Expand Down
13 changes: 9 additions & 4 deletions test/lib/get-api-url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ describe('get-api-url', () => {

it('use environment variable', () => {
const actual = apiUrlLib.getApiUrl({ local: true }, { COUCH_URL: 'http://user:pwd@localhost:5984/db' });
expect(actual).to.deep.equal(new url.URL('http://user:pwd@localhost:5988/medic'));
expect(actual).to.deep.equal(new url.URL('http://user:pwd@127.0.0.1:5988/medic'));
});

it('use environment variable 127.0.0.1', () => {
const actual = apiUrlLib.getApiUrl({ local: true }, { COUCH_URL: 'http://user:pwd@127.0.0.1:5984/db' });
expect(actual).to.deep.equal(new url.URL('http://user:pwd@127.0.0.1:5988/medic'));
});

it('warn if environment variable targets remote', () => {
Expand Down Expand Up @@ -68,13 +73,13 @@ describe('get-api-url', () => {
describe('parseLocalUrl', () => {
const parseLocalUrl = apiUrlLib.__get__('parseLocalUrl');
it('basic', () =>
expect(parseLocalUrl('http://admin:pass@localhost:5988/medic').href).to.eq('http://admin:pass@localhost:5988/'));
expect(parseLocalUrl('http://admin:pass@localhost:5988/medic').href).to.eq('http://admin:pass@127.0.0.1:5988/'));

it('updates port', () =>
expect(parseLocalUrl('http://admin:pass@localhost:5984/medic').href).to.eq('http://admin:pass@localhost:5988/'));
expect(parseLocalUrl('http://admin:pass@localhost:5984/medic').href).to.eq('http://admin:pass@127.0.0.1:5988/'));

it('ignores path', () =>
expect(parseLocalUrl('http://admin:pass@localhost:5984/foo').href).to.eq('http://admin:pass@localhost:5988/'));
expect(parseLocalUrl('http://admin:pass@localhost:5984/foo').href).to.eq('http://admin:pass@127.0.0.1:5988/'));
});

describe('isLocalhost', () => {
Expand Down