Skip to content

Commit fe0af5b

Browse files
Merge pull request #115 from kuzzleio/kuz-683-allow-changing-host
[MASTER] KUZ-683 allow changing host
2 parents f861454 + ddf8b71 commit fe0af5b

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/kuzzle.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,18 @@ module.exports = Kuzzle = function (host, options, cb) {
109109
},
110110
host: {
111111
value: host,
112+
writable: true,
112113
enumerable: true
113114
},
114115
wsPort: {
115116
value: (options && typeof options.wsPort === 'number') ? options.wsPort : 7513,
116-
enumerable: true
117+
enumerable: true,
118+
writable: true
117119
},
118120
ioPort: {
119121
value: (options && typeof options.ioPort === 'number') ? options.ioPort : 7512,
120-
enumerable: true
122+
enumerable: true,
123+
writable: true
121124
},
122125
sslConnection: {
123126
value: (options && typeof options.sslConnection === 'boolean') ? options.sslConnection : false,
@@ -314,10 +317,12 @@ module.exports = Kuzzle = function (host, options, cb) {
314317
Kuzzle.prototype.connect = function () {
315318
var self = this;
316319

317-
if (!self.network) {
318-
self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection);
320+
if (self.network) {
321+
self.disconnect();
319322
}
320323

324+
self.network = networkWrapper(self.host, self.wsPort, self.ioPort, self.sslConnection);
325+
321326
if (['initializing', 'ready', 'disconnected', 'error', 'offline'].indexOf(this.state) === -1) {
322327
if (self.connectCB) {
323328
self.connectCB(null, self);
@@ -1003,6 +1008,7 @@ Kuzzle.prototype.disconnect = function () {
10031008

10041009
this.state = 'disconnected';
10051010
this.network.close();
1011+
this.network = null;
10061012

10071013
for (collection in this.collections) {
10081014
if (this.collections.hasOwnProperty(collection)) {

test/kuzzle/constructor.test.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ describe('Kuzzle constructor', () => {
6969
should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: false, configurable: false });
7070
should(kuzzle).have.propertyWithDescriptor('jwtToken', { enumerable: true, writable: true, configurable: false });
7171
should(kuzzle).have.propertyWithDescriptor('offlineQueueLoader', { enumerable: true, writable: true, configurable: false });
72-
should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: false, configurable: false });
73-
should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: false, configurable: false });
72+
should(kuzzle).have.propertyWithDescriptor('wsPort', { enumerable: true, writable: true, configurable: false });
73+
should(kuzzle).have.propertyWithDescriptor('ioPort', { enumerable: true, writable: true, configurable: false });
7474
should(kuzzle).have.propertyWithDescriptor('sslConnection', { enumerable: true, writable: false, configurable: false });
7575
});
7676

@@ -291,6 +291,20 @@ describe('Kuzzle constructor', () => {
291291
}, 10);
292292
});
293293

294+
it('should first disconnect if it was connected', function () {
295+
var
296+
kuzzle = new Kuzzle('nowhere', {connect: 'manual'}),
297+
disconnectStub = sinon.stub();
298+
299+
kuzzle.disconnect = disconnectStub;
300+
kuzzle.connect();
301+
302+
should(kuzzle.disconnect.called).be.false();
303+
304+
kuzzle.connect();
305+
should(kuzzle.disconnect.called).be.true();
306+
});
307+
294308
describe('=> on connection error', () => {
295309
beforeEach(function () {
296310
networkStub.onConnectError = function (cb) {
@@ -382,22 +396,25 @@ describe('Kuzzle constructor', () => {
382396

383397
it('should dequeue requests automatically on a connection success', function (done) {
384398
var
385-
dequeued = false,
386-
kuzzle,
387-
KuzzleRewired = rewire(kuzzleSource),
388-
revert = KuzzleRewired.__set__('dequeue', function () { dequeued = true; });
399+
dequeueStub = sinon.stub(),
400+
kuzzle;
389401

390402
this.timeout(500);
391403

392-
kuzzle = new KuzzleRewired('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false}, () => {
393-
should(kuzzle.state).be.exactly('connected');
394-
should(dequeued).be.true();
395-
revert();
396-
done();
397-
});
404+
kuzzle = new Kuzzle('nowhere', {connect: 'manual', autoReplay: false, autoQueue: false});
398405

399-
kuzzle.network = networkStub;
406+
kuzzle.queuing = true;
407+
kuzzle.offlineQueue.push({query: 'foo'});
408+
kuzzle.addListener('offlineQueuePop', dequeueStub);
400409
kuzzle.connect();
410+
411+
setTimeout(function () {
412+
should(dequeueStub.called).be.true();
413+
should(kuzzle.state).be.exactly('connected');
414+
should(kuzzle.queuing).be.false();
415+
should(kuzzle.offlineQueue).be.empty();
416+
done();
417+
}, 20);
401418
});
402419
});
403420

test/kuzzle/methods.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ describe('Kuzzle methods', function () {
399399
kuzzle.collections = { foo: {}, bar: {}, baz: {} };
400400
kuzzle.disconnect();
401401

402-
should(kuzzle.network.close.called).be.true();
402+
should(kuzzle.network).be.null();
403403
should(kuzzle.collections).be.empty();
404404
should(function () { kuzzle.isValid(); }).throw(Error);
405405
});

0 commit comments

Comments
 (0)