Skip to content

Commit

Permalink
fix(server): respect sockPath on transportMode: 'ws' (#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-lahtinen committed Nov 9, 2019
1 parent a391b18 commit 6bcddc9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/servers/WebsocketServer.js
Expand Up @@ -10,10 +10,20 @@ module.exports = class WebsocketServer extends BaseServer {
constructor(server) {
super(server);
this.wsServer = new ws.Server({
server: this.server.listeningApp,
noServer: true,
path: this.server.sockPath,
});

this.server.listeningApp.on('upgrade', (req, sock, head) => {
if (!this.wsServer.shouldHandle(req)) {
return;
}

this.wsServer.handleUpgrade(req, sock, head, (connection) => {
this.wsServer.emit('connection', connection, req);
});
});

this.wsServer.on('error', (err) => {
this.server.log.error(err.message);
});
Expand Down
54 changes: 54 additions & 0 deletions test/server/servers/WebsocketServer.test.js
Expand Up @@ -85,6 +85,60 @@ describe('WebsocketServer', () => {
done();
}, 3000);
});

it('should match sockPath', (done) => {
let receivedConnection = false;
socketServer.onConnection((connection) => {
receivedConnection = true;
connection.close(4000);
});

// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server?YEP`);

let receivedError = false;
client.onerror = (e) => {
receivedError = e.error;
};

client.onclose = (e) => {
expect(e.code).toEqual(4000);
};

setTimeout(() => {
expect(receivedConnection).toBeTruthy();
expect(receivedError).toBeFalsy();
done();
}, 3000);
});

it('should ignore other paths', (done) => {
let receivedConnection = false;
socketServer.onConnection((connection) => {
receivedConnection = true;
connection.close(4000);
});

// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server-NOT`, {
handshakeTimeout: 1000,
});

let receivedError = false;
client.onerror = (e) => {
receivedError = e.error;
};

client.onclose = (e) => {
expect(e.code).not.toEqual(4000);
};

setTimeout(() => {
expect(receivedConnection).toBeFalsy();
expect(receivedError).toBeTruthy();
done();
}, 3000);
});
});

afterAll((done) => {
Expand Down

0 comments on commit 6bcddc9

Please sign in to comment.