-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
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
Changes from 6 commits
3bd3121
8251963
4846c91
f45395c
4086446
82408a8
d0acf61
1ad5b2b
b93130b
cb65985
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -368,6 +369,10 @@ res.sendFile = function sendFile(path, options, callback) { | |
done = options; | ||
opts = {}; | ||
} | ||
|
||
if(! ('etag' in opts) ) { | ||
opts.etag = app.set('etag'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
}) | ||
}) | ||
}) | ||
|
||
|
There was a problem hiding this comment.
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.