diff --git a/lib/socket.ts b/lib/socket.ts index 90b4840ab..91f2d7862 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -206,6 +206,12 @@ export interface SocketOptions { */ path: string; + /** + * Whether we should add a trailing slash to the request path. + * @default true + */ + addTrailingSlash: boolean; + /** * Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, * so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to @@ -323,6 +329,7 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { upgrade: true, timestampParam: "t", rememberUpgrade: false, + addTrailingSlash: true, rejectUnauthorized: true, perMessageDeflate: { threshold: 1024 @@ -333,7 +340,9 @@ export class Socket extends Emitter<{}, {}, SocketReservedEvents> { opts ); - this.opts.path = this.opts.path.replace(/\/$/, "") + "/"; + this.opts.path = + this.opts.path.replace(/\/$/, "") + + (this.opts.addTrailingSlash ? "/" : ""); if (typeof this.opts.query === "string") { this.opts.query = decode(this.opts.query); diff --git a/test/engine.io-client.js b/test/engine.io-client.js index 1d7677dd0..b4a290043 100644 --- a/test/engine.io-client.js +++ b/test/engine.io-client.js @@ -58,6 +58,12 @@ describe("engine.io-client", () => { expect(client.port).to.be("8080"); }); + it("should properly handle the addTrailingSlash option", () => { + const client = new Socket({ host: "localhost", addTrailingSlash: false }); + expect(client.hostname).to.be("localhost"); + expect(client.opts.path).to.be("/engine.io"); + }); + it("should properly parse an IPv6 uri without port", () => { const client = new Socket("http://[::1]"); expect(client.hostname).to.be("::1");