From de72f49eade48cc14dd916916ea86f88d46d3c8a Mon Sep 17 00:00:00 2001 From: Abhishek koserwal Date: Thu, 24 May 2018 12:40:16 +0530 Subject: [PATCH] fix: proxy events into instance var and add test script (#1661) --- lib/server.js | 22 +++++++++++----------- test/server.test.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/server.js b/lib/server.js index 5b7d9dc5d..3aeea5c4f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -34,14 +34,6 @@ patchRequest(http.IncomingMessage); ///--- Globals var sprintf = util.format; -var PROXY_EVENTS = [ - 'clientError', - 'close', - 'connection', - 'error', - 'listening', - 'secureConnection' -]; ///--- API @@ -138,6 +130,14 @@ function Server(options) { var fmt = mergeFormatters(options.formatters); this.acceptable = fmt.acceptable; this.formatters = fmt.formatters; + this.proxyEvents = [ + 'clientError', + 'close', + 'connection', + 'error', + 'listening', + 'secureConnection' + ]; if (options.spdy) { this.spdy = true; @@ -187,10 +187,10 @@ function Server(options) { this.router.on('mount', this.emit.bind(this, 'mount')); - if (!options.handleUpgrades && PROXY_EVENTS.indexOf('upgrade') === -1) { - PROXY_EVENTS.push('upgrade'); + if (!options.handleUpgrades) { + this.proxyEvents.push('upgrade'); } - PROXY_EVENTS.forEach(function forEach(e) { + this.proxyEvents.forEach(function forEach(e) { self.server.on(e, self.emit.bind(self, e)); }); diff --git a/test/server.test.js b/test/server.test.js index d83722cac..4229e5bef 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -2430,3 +2430,19 @@ test('should emit error with multiple next calls with strictNext', function(t) { }); }); }); + +test('should have proxy event handlers as instance', function(t) { + var server = restify.createServer({ + handleUpgrades: false + }); + t.equal(server.proxyEvents.length, 7); + + server = restify.createServer({ + handleUpgrades: true + }); + + t.equal(server.proxyEvents.length, 6); + server.close(function() { + t.end(); + }); +});