diff --git a/src/index.js b/src/index.js index 923b7ba..4b17803 100644 --- a/src/index.js +++ b/src/index.js @@ -198,6 +198,8 @@ module.exports = (config = {}) => { client.namespace = config.namespace || process.env.VAULT_NAMESPACE; client.kubernetesPath = config.kubernetesPath || 'kubernetes'; + client.endpoint = client.endpoint.replace(/\/$/, ''); + const requestSchema = { type: 'object', properties: { diff --git a/test/unit.js b/test/unit.js index e214419..cc3bb4f 100644 --- a/test/unit.js +++ b/test/unit.js @@ -87,6 +87,64 @@ describe('node-vault', () => { }); }); + describe('client initialization', () => { + it('should not trim endpoint if no trailing slash', () => { + const defaultsStub = sinon.stub(); + const vaultConfig = { + endpoint: 'http://localhost:8200', + 'request-promise': { + defaults: defaultsStub, + }, + }; + const vault = index(vaultConfig); + vault.endpoint.should.equal('http://localhost:8200'); + }); + + it('should trim endpoint if trailing slash', () => { + const defaultsStub = sinon.stub(); + const vaultConfig = { + endpoint: 'http://localhost:8200/', + 'request-promise': { + defaults: defaultsStub, + }, + }; + const vault = index(vaultConfig); + vault.endpoint.should.equal('http://localhost:8200'); + }); + + it('should not produce double slashes in request URI when endpoint has trailing slash', (done) => { + const request = sinon.stub(); + const response = sinon.stub(); + response.statusCode = 200; + request.returns({ + then(fn) { + return fn(response); + }, + catch(fn) { + return fn(); + }, + }); + + const vault = index({ + endpoint: 'http://localhost:8200/', + token: '123', + 'request-promise': { + defaults: () => request, + }, + }); + + vault.read('secret/hello') + .then(() => { + request.should.have.calledOnce(); + const uri = request.firstCall.args[0].uri; + uri.should.equal('http://localhost:8200/v1/secret/hello'); + uri.should.not.contain('//v1'); + done(); + }) + .catch(done); + }); + }); + describe('client', () => { let request = null; let response = null;