Skip to content

Commit

Permalink
Fix ?metadata when a 304 is served and secondGuessContentType is enab…
Browse files Browse the repository at this point in the history
…led.
  • Loading branch information
papandreou committed Nov 11, 2016
1 parent d15f15f commit 711cb2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ module.exports = function (options) {

var contentType = res.getHeader('Content-Type');

if (isMetadataRequest || (contentType && (options.allowedImageSourceContentTypes ? options.allowedImageSourceContentTypes.indexOf(contentType) !== -1 : contentType.indexOf('image/') === 0))) {
if (res.statusCode === 304) {
res.unhijack();
} else if (isMetadataRequest || (contentType && (options.allowedImageSourceContentTypes ? options.allowedImageSourceContentTypes.indexOf(contentType) !== -1 : contentType.indexOf('image/') === 0))) {
sourceMetadata = {
contentType: contentType,
filesize: contentLengthHeaderValue && parseInt(contentLengthHeaderValue, 10),
Expand Down
21 changes: 21 additions & 0 deletions test/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,27 @@ describe('express-processimage', function () {
});
});

// Regression test
it('should not break when serving a 304 to a ?metadata request when secondGuessSourceContentType is enabled', function () {
config.secondGuessSourceContentType = true;
return expect(
express()
.use(processImage(config))
.use(function (req, res, next) {
res.status(304).end();
}),
'to yield exchange', {
request: {
url: 'GET /turtle.jpg?metadata',
headers: {
'If-None-Match': '"foobar"'
}
},
response: 304
}
);
});

it('should allow retrieving the metadata of a non-image file with a non-image extension', function () {
return expect('GET /something.txt?metadata', 'to yield response', {
body: {
Expand Down

0 comments on commit 711cb2e

Please sign in to comment.