-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow self signed certs by default for localhost-like
- Loading branch information
1 parent
cc56b48
commit 08a9b88
Showing
4 changed files
with
63 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
const sinon = require('sinon'); | ||
const rewire = require('rewire'); | ||
const { expect } = require('chai'); | ||
const url = require('url'); | ||
|
||
const getApiUrl = rewire('../../src/lib/get-api-url'); | ||
const apiUrlLib = rewire('../../src/lib/get-api-url'); | ||
const userPrompt = rewire('../../src/lib/user-prompt'); | ||
|
||
describe('get-api-url', () => { | ||
|
@@ -13,59 +14,59 @@ describe('get-api-url', () => { | |
keyInYN: sinon.stub().throws('unexpected'), | ||
}; | ||
userPrompt.__set__('readline', readline); | ||
getApiUrl.__set__('userPrompt', userPrompt); | ||
apiUrlLib.__set__('userPrompt', userPrompt); | ||
}); | ||
|
||
it('multiple destinations yields error', () => { | ||
const actual = () => getApiUrl({ local: true, instance: 'demo' }); | ||
const actual = () => apiUrlLib.getApiUrl({ local: true, instance: 'demo' }); | ||
expect(actual).to.throw('One of these'); | ||
}); | ||
|
||
it('no destination yields error', () => { | ||
const actual = () => getApiUrl({}); | ||
const actual = () => apiUrlLib.getApiUrl({}); | ||
expect(actual).to.throw('One of these'); | ||
}); | ||
|
||
describe('--local', () => { | ||
it('no environment variable has a default', () => { | ||
const actual = getApiUrl({ local: true }); | ||
expect(actual).to.eq('http://admin:pass@localhost:5988/medic'); | ||
const actual = apiUrlLib.getApiUrl({ local: true }); | ||
expect(actual).to.deep.equal(new url.URL('http://admin:pass@localhost:5988/medic')); | ||
}); | ||
|
||
it('use environment variable', () => { | ||
const actual = getApiUrl({ local: true }, { COUCH_URL: 'http://user:pwd@localhost:5984/db' }); | ||
expect(actual).to.eq('http://user:pwd@localhost:5988/medic'); | ||
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')); | ||
}); | ||
|
||
it('warn if environment variable targets remote', () => { | ||
const actual = () => getApiUrl({ local: true }, { COUCH_URL: 'http://user:pwd@remote:5984/db' }); | ||
const actual = () => apiUrlLib.getApiUrl({ local: true }, { COUCH_URL: 'http://user:pwd@remote:5984/db' }); | ||
expect(actual).to.throw('remote'); | ||
}); | ||
}); | ||
|
||
describe('--instance', () => { | ||
it('with default user', () => { | ||
readline.question.returns('entered'); | ||
const actual = getApiUrl({ instance: 'inst' }); | ||
expect(actual).to.eq('https://admin:[email protected]/medic'); | ||
const actual = apiUrlLib.getApiUrl({ instance: 'inst' }); | ||
expect(actual).to.deep.equal(new url.URL('https://admin:[email protected]/medic')); | ||
}); | ||
|
||
it('with --user', () => { | ||
readline.question.returns('entered'); | ||
const actual = getApiUrl({ instance: 'inst', user: 'user' }); | ||
expect(actual).to.eq('https://user:[email protected]/medic'); | ||
const actual = apiUrlLib.getApiUrl({ instance: 'inst', user: 'user' }); | ||
expect(actual).to.deep.equal(new url.URL('https://user:[email protected]/medic')); | ||
}); | ||
}); | ||
|
||
describe('--url', () => { | ||
it('basic', () => { | ||
const actual = getApiUrl({ url: 'https://admin:[email protected]/' }); | ||
expect(actual).to.eq('https://admin:[email protected]/medic'); | ||
const actual = apiUrlLib.getApiUrl({ url: 'https://admin:[email protected]/' }); | ||
expect(actual).to.deep.equal(new url.URL('https://admin:[email protected]/medic')); | ||
}); | ||
}); | ||
|
||
describe('parseLocalUrl', () => { | ||
const parseLocalUrl = getApiUrl.__get__('parseLocalUrl'); | ||
const parseLocalUrl = apiUrlLib.__get__('parseLocalUrl'); | ||
it('basic', () => | ||
expect(parseLocalUrl('http://admin:pass@localhost:5988/medic').href).to.eq('http://admin:pass@localhost:5988/')); | ||
|
||
|
@@ -75,4 +76,30 @@ describe('get-api-url', () => { | |
it('ignores path', () => | ||
expect(parseLocalUrl('http://admin:pass@localhost:5984/foo').href).to.eq('http://admin:pass@localhost:5988/')); | ||
}); | ||
|
||
describe('isLocalhost', () => { | ||
it('should return true for localhost', () => { | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:pass@localhost/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:pass@localhost:5988/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('https://admin:pass@localhost/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('https://admin:pass@localhost/whatever'))).to.be.true; | ||
}); | ||
|
||
it('should return true for 127.0.0.x', () => { | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]:5988/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]:5988/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('https://admin:[email protected]/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('https://admin:[email protected]/medic'))).to.be.true; | ||
expect(apiUrlLib.isLocalhost(new url.URL('https://admin:[email protected]/whatever'))).to.be.true; | ||
}); | ||
|
||
it('should return false for anything else', () => { | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:pass@host/medic'))).to.be.false; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:pass@notlocalhost/medic'))).to.be.false; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]/medic'))).to.be.false; | ||
expect(apiUrlLib.isLocalhost(new url.URL('http://admin:[email protected]/medic'))).to.be.false; | ||
}); | ||
}); | ||
}); |