Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,16 @@ function patch(Response) {
);
}

type = type.split(';')[0];
// Keep any user-supplied content-type parameters (e.g.
// "; version=0.0.4") so they survive on the outgoing header below.
// Only the base type is used for formatter lookup.
var contentTypeParams = '';
var paramIndex = type.indexOf(';');

if (paramIndex !== -1) {
contentTypeParams = type.substring(paramIndex);
type = type.substring(0, paramIndex);
}

if (!self.formatters[type] && type.indexOf('/') === -1) {
type = mime.getType(type);
Expand Down Expand Up @@ -496,6 +505,11 @@ function patch(Response) {

if (self._charSet) {
type = type + '; charset=' + self._charSet;
} else if (contentTypeParams) {
// Restore the parameters the user originally provided, e.g.
// res.contentType = 'text/plain; version=0.0.4' keeps the
// version parameter on the outgoing Content-Type header.
type = type + contentTypeParams;
}

// Update Content-Type header to the one originally set or to the
Expand Down
52 changes: 52 additions & 0 deletions test/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,55 @@ test(module, 'GH-1791: should send NaN as null with application/json', function(
t.end();
});
});

test(
module,
'GH-1589: should preserve content-type parameters set by the user',
function(t) {
SERVER.get('/content-type-params', function(req, res, next) {
res.contentType = 'text/plain; version=0.0.4; charset=utf-8';
res.send('hello');
return next();
});

STRING_CLIENT.get('/content-type-params', function(
err,
req,
res,
data
) {
t.ifError(err);
t.equal(data, 'hello');
t.equal(
res.headers['content-type'],
'text/plain; version=0.0.4; charset=utf-8'
);
t.end();
});
}
);

test(
module,
'GH-1589: res.charSet still appends charset without duplicating params',
function(t) {
SERVER.get('/content-type-charset', function(req, res, next) {
res.contentType = 'text/plain';
res.charSet('utf-8');
res.send('hello');
return next();
});

STRING_CLIENT.get('/content-type-charset', function(
err,
req,
res,
data
) {
t.ifError(err);
t.equal(data, 'hello');
t.equal(res.headers['content-type'], 'text/plain; charset=utf-8');
t.end();
});
}
);