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 #1565: Return 444 status code for closed and aborted requests #1579

Merged
5 changes: 5 additions & 0 deletions lib/response.js
Expand Up @@ -383,6 +383,11 @@ function patch(Response) {
// Now lets try to derive values for optional arguments that we were not
// provided, otherwise we choose sane defaults.

// Request was aborted or closed. Override the status code
if (self.req.connectionState()) {
code = 444;
}

// If the body is an error object and we were not given a status code,
// try to derive it from the error object, otherwise default to 500
if (!code && body instanceof Error) {
Expand Down
70 changes: 70 additions & 0 deletions test/plugins/audit.test.js
Expand Up @@ -605,4 +605,74 @@ describe('audit logger', function() {
assert.ifError(err);
});
});

it('should log 444 status code for aborted request', function(done) {
SERVER.once(
'after',
restify.plugins.auditLogger({
log: bunyan.createLogger({
name: 'audit',
streams: [
{
level: 'info',
stream: process.stdout
}
]
}),
server: SERVER,
event: 'after'
})
);

SERVER.once('audit', function(data) {
assert.ok(data);
assert.ok(data.req_id);
assert.isNumber(data.latency);
assert.equal(444, data.res.statusCode);
done();
});

SERVER.get('/audit', function(req, res, next) {
req.emit('aborted');
res.send();
next();
});

CLIENT.get('/audit', function(err, req, res) {});
});

it('should log 444 for closed request', function(done) {
SERVER.once(
'after',
restify.plugins.auditLogger({
log: bunyan.createLogger({
name: 'audit',
streams: [
{
level: 'info',
stream: process.stdout
}
]
}),
server: SERVER,
event: 'after'
})
);

SERVER.once('audit', function(data) {
assert.ok(data);
assert.ok(data.req_id);
assert.isNumber(data.latency);
assert.equal(444, data.res.statusCode);
done();
});

SERVER.get('/audit', function(req, res, next) {
req.emit('close');
res.send();
next();
});

CLIENT.get('/audit', function(err, req, res) {});
});
});
5 changes: 2 additions & 3 deletions test/plugins/metrics.test.js
Expand Up @@ -96,7 +96,7 @@ describe('request metrics plugin', function() {
assert.equal(err.name, 'RequestCloseError');

assert.isObject(metrics, 'metrics');
assert.equal(metrics.statusCode, 202);
assert.equal(metrics.statusCode, 444);
assert.isAtLeast(metrics.latency, 200);
assert.equal(metrics.path, '/foo');
assert.equal(metrics.method, 'GET');
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('request metrics plugin', function() {
assert.equal(err.name, 'RequestAbortedError');

assert.isObject(metrics, 'metrics');
assert.equal(metrics.statusCode, 202);
assert.equal(metrics.statusCode, 444);
assert.isAtLeast(metrics.latency, 200);
assert.equal(metrics.path, '/foo');
assert.equal(metrics.method, 'GET');
Expand All @@ -155,7 +155,6 @@ describe('request metrics plugin', function() {
});

CLIENT.get('/foo?a=1', function(err, _, res) {
assert.ifError(err);
return done();
});
});
Expand Down