it(`includes the logLevelToken in the authorization payload`, () => { const s = new Socket(); s.open(`ws://example.com`, { forceCloseDelay: mockoptions.forceCloseDelay, pingInterval: mockoptions.pingInterval, pongTimeout: mockoptions.pongTimeout, logger: console, token: `mocktoken`, trackingId: `mocktrackingid`, logLevelToken: `mocklogleveltoken` }).catch((reason) => console.error(reason)); mockWebSocket.readyState = 1; mockWebSocket.emit(`open`); const firstCallArgs = JSON.parse(mockWebSocket.send.firstCall.args[0]); assert.property(firstCallArgs, `id`); assert.equal(firstCallArgs.type, `authorization`); assert.property(firstCallArgs, `data`); assert.property(firstCallArgs.data, `token`); assert.equal(firstCallArgs.data.token, `mocktoken`); assert.equal(firstCallArgs.trackingId, `mocktrackingid`); assert.equal(firstCallArgs.logLevelToken, `mocklogleveltoken`); return s.close(); });
it(`signals closure if no close frame is received within the specified window`, () => { const socket = new Socket(); const promise = socket.open(`ws://example.com`, mockoptions); mockWebSocket.readyState = 1; mockWebSocket.emit(`open`); mockWebSocket.emit(`message`, { data: JSON.stringify({ data: { eventType: `mercury.buffer_state` } }) }); return promise .then(() => { const spy = sinon.spy(); socket.on(`close`, spy); mockWebSocket.close = () => new Promise(() => {/* eslint no-inline-comments: [0] */}); mockWebSocket.removeAllListeners(`close`); const promise = socket.close(); clock.tick(mockoptions.forceCloseDelay); return promise .then(() => { assert.called(spy); assert.calledWith(spy, { code: 1000, reason: `Done (forced)` }); }); }); });
beforeEach(`mock WebSocket and open a Socket`, () => { sinon.stub(Socket, `getWebSocketConstructor`, () => { return function(...args) { mockWebSocket = new MockWebSocket(...args); return mockWebSocket; }; }); sinon.spy(Socket.prototype, `_ping`); socket = new Socket(); const promise = socket.open(`ws://example.com`, mockoptions); mockWebSocket.open(); return promise; });
it(`resolves upon receiving registration status`, () => { const s = new Socket(); const promise = s.open(`ws://example.com`, mockoptions); mockWebSocket.readyState = 1; mockWebSocket.emit(`open`); mockWebSocket.emit(`message`, { data: JSON.stringify({ data: { eventType: `mercury.registration_status` } }) }); return assert.isFulfilled(promise) .then(() => s.close()); });
.then((reason) => { assert.instanceOf(reason, ConnectionError); assert.match(reason.code, 4001); assert.match(reason.reason, /No/); assert.match(reason.message, /No/); return s.close(); });
.then((reason) => { assert.instanceOf(reason, Forbidden); assert.match(reason.code, 4403); assert.match(reason.reason, /Not entitled/); assert.match(reason.message, /Not entitled/); return s.close(); });
.then((reason) => { assert.instanceOf(reason, NotAuthorized); assert.match(reason.code, 4401); assert.match(reason.reason, /Authorization Failed/); assert.match(reason.message, /Authorization Failed/); return s.close(); });
.then((reason) => { assert.instanceOf(reason, BadRequest); assert.match(reason.code, 4400); assert.match(reason.reason, /Service accounts can't use this endpoint/); assert.match(reason.message, /Service accounts can't use this endpoint/); return s.close(); });
.then(() => { const spy = sinon.spy(); socket.on(`close`, spy); mockWebSocket.close = () => new Promise(() => {/* eslint no-inline-comments: [0] */}); mockWebSocket.removeAllListeners(`close`); const promise = socket.close(); clock.tick(mockoptions.forceCloseDelay); return promise .then(() => { assert.called(spy); assert.calledWith(spy, { code: 1000, reason: `Done (forced)` }); }); });
it(`rejects with the close event's reason`, () => { const s = new Socket(); const promise = s.open(`ws://example.com`, mockoptions); mockWebSocket.emit(`close`, { code: 4001, reason: `No` }); return assert.isRejected(promise) .then((reason) => { assert.instanceOf(reason, ConnectionError); assert.match(reason.code, 4001); assert.match(reason.reason, /No/); assert.match(reason.message, /No/); return s.close(); }); });
it(`rejects with a Forbidden`, () => { const s = new Socket(); const promise = s.open(`ws://example.com`, mockoptions); mockWebSocket.readyState = 1; mockWebSocket.emit(`open`); const firstCallArgs = JSON.parse(mockWebSocket.send.firstCall.args[0]); assert.equal(firstCallArgs.type, `authorization`); mockWebSocket.emit(`close`, { code: 4403, reason: `Not entitled` }); return assert.isRejected(promise) .then((reason) => { assert.instanceOf(reason, Forbidden); assert.match(reason.code, 4403); assert.match(reason.reason, /Not entitled/); assert.match(reason.message, /Not entitled/); return s.close(); }); });
it(`rejects with a BadRequest`, () => { const s = new Socket(); const promise = s.open(`ws://example.com`, mockoptions); mockWebSocket.readyState = 1; mockWebSocket.emit(`open`); const firstCallArgs = JSON.parse(mockWebSocket.send.firstCall.args[0]); assert.equal(firstCallArgs.type, `authorization`); mockWebSocket.emit(`close`, { code: 4400, reason: `Service accounts can't use this endpoint` }); return assert.isRejected(promise) .then((reason) => { assert.instanceOf(reason, BadRequest); assert.match(reason.code, 4400); assert.match(reason.reason, /Service accounts can't use this endpoint/); assert.match(reason.message, /Service accounts can't use this endpoint/); return s.close(); }); });
.then(() => s.close());
it(`requires a url parameter`, () => { const s = new Socket(); return assert.isRejected(s.open(), /`url` is required/); });