diff --git a/lib/server.js b/lib/server.js index 89eaf606d..cc1959db7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -215,8 +215,15 @@ class Server extends EventEmitter { * @param {Object} request object * @api private */ - handshake(transportName, req) { - const id = this.generateId(req); + async handshake(transportName, req) { + let id; + try { + id = await this.generateId(req); + } catch (e) { + debug("error while generating an id"); + sendErrorMessage(req, req.res, Server.errors.BAD_REQUEST); + return; + } debug('handshaking client "%s"', id); diff --git a/test/server.js b/test/server.js index 0f4c6352f..8d66b30ff 100644 --- a/test/server.js +++ b/test/server.js @@ -315,6 +315,36 @@ describe("server", function() { }); }); + it("should register a new client with custom id (with a Promise)", function(done) { + const engine = listen({ allowUpgrades: false }, port => { + const customId = "CustomId" + Date.now(); + + engine.generateId = function() { + return Promise.resolve(customId); + }; + + const socket = new eioc.Socket("ws://localhost:%d".s(port)); + socket.once("open", () => { + expect(socket.id).to.be(customId); + expect(engine.clients[customId].id).to.be(customId); + done(); + }); + }); + }); + + it("should disallow connection that are rejected by `generateId`", function(done) { + const engine = listen({ allowUpgrades: false }, port => { + engine.generateId = () => { + return Promise.reject(new Error("nope")); + }; + + const socket = new eioc.Socket("ws://localhost:%d".s(port)); + socket.on("error", () => { + done(); + }); + }); + }); + it("should exchange handshake data", function(done) { listen({ allowUpgrades: false }, function(port) { var socket = new eioc.Socket("ws://localhost:%d".s(port));