diff --git a/index.js b/index.js index c2c86b4..b08503f 100644 --- a/index.js +++ b/index.js @@ -105,18 +105,18 @@ function fastifyWebsocket (fastify, opts, next) { } // we always override the route handler so we can close websocket connections to routes to handlers that don't support websocket connections - routeOptions.handler = (request, reply) => { + // This is not an arrow function to fetch the encapsulated this + routeOptions.handler = function (request, reply) { // within the route handler, we check if there has been a connection upgrade by looking at request.raw[kWs]. we need to dispatch the normal HTTP handler if not, and hijack to dispatch the websocket handler if so if (request.raw[kWs]) { reply.hijack() handleUpgrade(request.raw, connection => { let result - try { if (isWebsocketRoute) { - result = wsHandler.call(fastify, connection, request) + result = wsHandler.call(this, connection, request) } else { - result = noHandle.call(fastify, connection, request) + result = noHandle.call(this, connection, request) } } catch (err) { return errorHandler.call(this, err, connection, request, reply) @@ -127,7 +127,7 @@ function fastifyWebsocket (fastify, opts, next) { } }) } else { - return handler.call(fastify, request, reply) + return handler.call(this, request, reply) } } }) diff --git a/test/base.js b/test/base.js index c520c10..f5fd76b 100644 --- a/test/base.js +++ b/test/base.js @@ -508,3 +508,24 @@ test('Should error server if the noServer option is set', (t) => { fastify.register(fastifyWebsocket, { options: { noServer: true } }) t.rejects(fastify.ready()) }) + +test('Should preserve the prefix in non-websocket routes', (t) => { + t.plan(3) + + const fastify = Fastify() + t.teardown(() => fastify.close()) + + fastify.register(fastifyWebsocket) + + fastify.register(async function (fastify) { + t.equal(fastify.prefix, '/hello') + fastify.get('/', function (request, reply) { + t.equal(this.prefix, '/hello') + reply.send('hello') + }) + }, { prefix: '/hello' }) + + fastify.inject('/hello', function (err) { + t.error(err) + }) +}) diff --git a/test/router.js b/test/router.js index 45a544b..d1625cd 100644 --- a/test/router.js +++ b/test/router.js @@ -8,7 +8,7 @@ const WebSocket = require('ws') const get = require('http').get test('Should expose a websocket on prefixed route', t => { - t.plan(3) + t.plan(4) const fastify = Fastify() t.teardown(() => fastify.close()) @@ -16,7 +16,8 @@ test('Should expose a websocket on prefixed route', t => { fastify.register(fastifyWebsocket) fastify.register( function (instance, opts, next) { - instance.get('/echo', { websocket: true }, (conn, request) => { + instance.get('/echo', { websocket: true }, function (conn, request) { + t.equal(this.prefix, '/baz') conn.setEncoding('utf8') conn.write('hello client') t.teardown(conn.destroy.bind(conn)) @@ -274,6 +275,7 @@ test('Should call wildcard route handler on unregistered path', t => { }) test('Should invoke the correct handler depending on the headers', t => { + t.plan(4) const fastify = Fastify() t.teardown(() => fastify.close()) @@ -300,6 +302,7 @@ test('Should invoke the correct handler depending on the headers', t => { httpClient.write('GET / HTTP/1.1\r\n\r\n') httpClient.once('data', data => { t.match(data.toString(), /hi from handler/i) + httpClient.end() }) }) @@ -307,7 +310,7 @@ test('Should invoke the correct handler depending on the headers', t => { wsClient.write('GET / HTTP/1.1\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n') wsClient.once('data', data => { t.match(data.toString(), /hi from wsHandler/i) - wsClient.end(() => { t.end() }) + wsClient.end(() => { t.pass() }) }) }) })