diff --git a/src/protocols/abstract/common.js b/src/protocols/abstract/common.js index 8eec74705..fec780fd9 100644 --- a/src/protocols/abstract/common.js +++ b/src/protocols/abstract/common.js @@ -12,7 +12,8 @@ class AbstractWrapper extends KuzzleEventEmitter { this._pendingRequests = new Map(); this._host = host; - this._port = typeof options.port === 'number' ? options.port : 7512; + const port = parseInt(options.port, 10); + this._port = isNaN(port) ? 7512 : port; this._ssl = typeof options.sslConnection === 'boolean' ? options.sslConnection : false; this.id = uuidv4(); diff --git a/test/protocol/common.test.js b/test/protocol/common.test.js index 329eb2414..4994d6149 100644 --- a/test/protocol/common.test.js +++ b/test/protocol/common.test.js @@ -18,6 +18,30 @@ describe('Common Protocol', () => { sendSpy = sinon.spy(protocol, 'send'); }); + describe('#constructor', () => { + it('should accept string as port', () => { + protocol = new AbstractWrapper('somewhere', { port: '443' }); + + should(protocol.port).be.eql(443); + }); + + it('should use 7512 when no port is given or when port is not a parseable number', () => { + protocol = new AbstractWrapper('somewhere', { port: 'foobar' }); + + should(protocol.port).be.eql(7512); + + protocol = new AbstractWrapper('somewhere'); + + should(protocol.port).be.eql(7512); + }); + + it('should accept number as port', () => { + protocol = new AbstractWrapper('somewhere', { port: 443 }); + + should(protocol.port).be.eql(443); + }); + }); + describe('#connected', () => { it('should return true if the protocol state is "connected"', () => { protocol.state = 'connected';