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
7 changes: 7 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ res.sendFile = function sendFile(path, options, callback) {
var res = this;
var next = req.next;
var opts = options || {};

// settings
var app = this.app;

if (!path) {
throw new TypeError('path argument is required to res.sendFile');
Expand All @@ -368,6 +371,10 @@ res.sendFile = function sendFile(path, options, callback) {
done = options;
opts = {};
}

if (app.settings.etag == false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use the setting functions to access settings, not direct access to the object. Direct object access was deprecated in 3.0.

opts.etag = app.settings.etag;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect that if the user explicitly set the etag option to res.sendFile, it would not be overwritten by the setting.

}

if (!opts.root && !pathIsAbsolute(path)) {
throw new TypeError('path must be absolute or specify root to res.sendFile');
Expand Down
20 changes: 20 additions & 0 deletions test/res.sendFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe('res', function(){
.expect('Content-Type', 'application/x-bogus')
.end(done);
})

it('should set etag according to app settings', 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.

Please add a test that checks the behavior when someone sets the etag setting to strong, weak, or a custom function.

var app = express();

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

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


it('should not error if the client aborts', function (done) {
var cb = after(1, done);
Expand Down