Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proxy events into instance var and add test script #1661

Merged
merged 2 commits into from May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/server.js
Expand Up @@ -34,14 +34,6 @@ patchRequest(http.IncomingMessage);
///--- Globals

var sprintf = util.format;
var PROXY_EVENTS = [
'clientError',
'close',
'connection',
'error',
'listening',
'secureConnection'
];

///--- API

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});

Expand Down
16 changes: 16 additions & 0 deletions test/server.test.js
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I expect handleUpgrades: true to be 7, and 6 for false (may be related to the && indexOf check above)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the doc

options.handleUpgrades Boolean Hook the upgrade event from the node HTTP server, pushing Connection: Upgrade requests through the regular request handling chain. (optional, default false) (http://restify.com/docs/server-api/).

We are updating proxyEvents array when
options.handleUpgrades:false
size will be 7 and in-case of true. we will skip it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I can't read. Must have been a long day. :D


server = restify.createServer({
handleUpgrades: true
});

t.equal(server.proxyEvents.length, 6);
server.close(function() {
t.end();
});
});