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

res.sendFile: inherit etag setting from app #2936

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ res.sendStatus = function sendStatus(statusCode) {
*/

res.sendFile = function sendFile(path, options, callback) {
var app = this.app;
var done = callback;
var req = this.req;
var res = this;
Expand All @@ -368,6 +369,10 @@ res.sendFile = function sendFile(path, options, callback) {
done = options;
opts = {};
}

if(! ('etag' in opts) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pleas do not use in for option detection. This makes the interface really confusing because you have to ensure that whatever you are passing in does not have that key anywhere in the prototype chain.

opts.etag = app.set('etag');
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not pass in non-booleans to the sub module, because that is just asking for undefined behavior later. The etag option only accepts true or false.

}

if (!opts.root && !pathIsAbsolute(path)) {
throw new TypeError('path must be absolute or specify root to res.sendFile');
Expand Down
76 changes: 76 additions & 0 deletions test/res.sendFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,82 @@ describe('res', function(){
.get('/')
.expect(200, 'got it', done);
})

it('should set etag according to app settings', function (done) {
var app = express();

app.disable('etag');
app.use(function (req, res) {
res.sendFile(path.resolve(fixtures, 'name.txt'));
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Whitespace issue.


request(app)
.get('/')
.end(function(err, res) {
if (err) {
return done(err);
}
assert.equal('etag' in res.headers, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a supertest header assertion, rather than doing it manually in the callback.

done();
});
})

it('should set etag according to app settings, with input args priority', function (done) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This test should be in the group with the above test, not in the base res group.

var app = express();

app.disable('etag');
app.use(function (req, res) {
res.sendFile(path.resolve(fixtures, 'name.txt'),{etag:'weak'});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Whitespace issue on this line.


request(app)
.get('/')
.expect('ETag', /^(?:W\/)"[^"]+"$/, done);
})

it('should contain etag weak', function (done) {
var app = express();

app.set('etag', 'weak');
app.use(function (req, res) {
res.sendFile(path.resolve(fixtures, 'name.txt'));
});

request(app)
.get('/')
.expect('ETag', /^(?:W\/)"[^"]+"$/, done);
})

it('should contain etag custom', function (done) {
var app = express();

app.set('etag', function (body, encoding) {
return '"custom"';
});

app.use(function (req, res) {
res.sendFile(path.resolve(fixtures, 'name.txt'));
});

request(app)
.get('/')
.expect('ETag', '"custom"')
.expect(200, done);
})

it('should contain etag strong', function (done) {
var app = express();

app.set('etag', 'strong');
app.use(function (req, res) {
res.sendFile(path.resolve(fixtures, 'name.txt'));
});

request(app)
.get('/')
.expect('ETag', /^"[^"]+"$/, done);

})
})
})

Expand Down