Skip to content

Commit bad55f7

Browse files
committed
move setHeader charset patch to .set
note that application/json no longer adds charset=utf-8. could be a regression. closes #1952 See also: broofa/mime#86
1 parent 3cf7b2e commit bad55f7

8 files changed

+42
-87
lines changed

lib/response.js

+19-36
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
* Module dependencies.
33
*/
44

5-
var http = require('http')
6-
, path = require('path')
7-
, mixin = require('utils-merge')
8-
, escapeHtml = require('escape-html')
9-
, sign = require('cookie-signature').sign
10-
, normalizeType = require('./utils').normalizeType
11-
, normalizeTypes = require('./utils').normalizeTypes
12-
, etag = require('./utils').etag
13-
, statusCodes = http.STATUS_CODES
14-
, cookie = require('cookie')
15-
, send = require('send')
16-
, basename = path.basename
17-
, extname = path.extname
18-
, mime = send.mime;
19-
var ServerResponse = http.ServerResponse;
20-
var setHeader = ServerResponse.prototype.setHeader;
5+
var http = require('http');
6+
var path = require('path');
7+
var mixin = require('utils-merge');
8+
var escapeHtml = require('escape-html');
9+
var sign = require('cookie-signature').sign;
10+
var normalizeType = require('./utils').normalizeType;
11+
var normalizeTypes = require('./utils').normalizeTypes;
12+
var etag = require('./utils').etag;
13+
var statusCodes = http.STATUS_CODES;
14+
var cookie = require('cookie');
15+
var send = require('send');
16+
var basename = path.basename;
17+
var extname = path.extname;
18+
var mime = send.mime;
2119

2220
/**
2321
* Response prototype.
@@ -525,6 +523,11 @@ res.header = function(field, val){
525523
if (2 == arguments.length) {
526524
if (Array.isArray(val)) val = val.map(String);
527525
else val = String(val);
526+
field = field.toLowerCase();
527+
if ('content-type' == field && !/;\s*charset\s*=/.test(val)) {
528+
var charset = mime.charsets.lookup(val.split(';')[0]);
529+
if (charset) val += '; charset=' + charset.toLowerCase();
530+
}
528531
this.setHeader(field, val);
529532
} else {
530533
for (var key in field) {
@@ -780,23 +783,3 @@ res.render = function(view, options, fn){
780783
// render
781784
app.render(view, options, fn);
782785
};
783-
784-
785-
/**
786-
* Set header `field` to `val`, special-casing
787-
* the `Set-Cookie` field for multiple support.
788-
*
789-
* @param {String} field
790-
* @param {String} val
791-
* @api public
792-
*/
793-
794-
res.setHeader = function(field, val){
795-
var key = field.toLowerCase();
796-
797-
if ('content-type' == key && this.charset) {
798-
val += '; charset=' + this.charset;
799-
}
800-
801-
return setHeader.call(this, field, val);
802-
};

test/req.is.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function req(ct) {
1616

1717
describe('req.is()', function(){
1818
it('should ignore charset', function(){
19-
req('application/json; charset=utf-8')
19+
req('application/json')
2020
.is('json')
2121
.should.equal('json');
2222
})

test/res.charset.js

-34
This file was deleted.

test/res.json.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('res', function(){
2828
request(app)
2929
.get('/')
3030
.end(function(err, res){
31-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
31+
res.headers.should.have.property('content-type', 'application/json');
3232
res.text.should.equal('null');
3333
done();
3434
})
@@ -46,7 +46,7 @@ describe('res', function(){
4646
request(app)
4747
.get('/')
4848
.end(function(err, res){
49-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
49+
res.headers.should.have.property('content-type', 'application/json');
5050
res.text.should.equal('["foo","bar","baz"]');
5151
done();
5252
})
@@ -64,7 +64,7 @@ describe('res', function(){
6464
request(app)
6565
.get('/')
6666
.end(function(err, res){
67-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
67+
res.headers.should.have.property('content-type', 'application/json');
6868
res.text.should.equal('{"name":"tobi"}');
6969
done();
7070
})
@@ -131,7 +131,7 @@ describe('res', function(){
131131
.get('/')
132132
.end(function(err, res){
133133
res.statusCode.should.equal(201);
134-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
134+
res.headers.should.have.property('content-type', 'application/json');
135135
res.text.should.equal('{"id":1}');
136136
done();
137137
})
@@ -150,7 +150,7 @@ describe('res', function(){
150150
.get('/')
151151
.end(function(err, res){
152152
res.statusCode.should.equal(201);
153-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
153+
res.headers.should.have.property('content-type', 'application/json');
154154
res.text.should.equal('{"id":1}');
155155
done();
156156
})

test/res.jsonp.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('res', function(){
114114
request(app)
115115
.get('/')
116116
.end(function(err, res){
117-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
117+
res.headers.should.have.property('content-type', 'application/json');
118118
res.text.should.equal('null');
119119
done();
120120
})
@@ -132,7 +132,7 @@ describe('res', function(){
132132
request(app)
133133
.get('/')
134134
.end(function(err, res){
135-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
135+
res.headers.should.have.property('content-type', 'application/json');
136136
res.text.should.equal('["foo","bar","baz"]');
137137
done();
138138
})
@@ -150,7 +150,7 @@ describe('res', function(){
150150
request(app)
151151
.get('/')
152152
.end(function(err, res){
153-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
153+
res.headers.should.have.property('content-type', 'application/json');
154154
res.text.should.equal('{"name":"tobi"}');
155155
done();
156156
})
@@ -217,7 +217,7 @@ describe('res', function(){
217217
.get('/')
218218
.end(function(err, res){
219219
res.statusCode.should.equal(201);
220-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
220+
res.headers.should.have.property('content-type', 'application/json');
221221
res.text.should.equal('{"id":1}');
222222
done();
223223
})
@@ -236,7 +236,7 @@ describe('res', function(){
236236
.get('/')
237237
.end(function(err, res){
238238
res.statusCode.should.equal(201);
239-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
239+
res.headers.should.have.property('content-type', 'application/json');
240240
res.text.should.equal('{"id":1}');
241241
done();
242242
})

test/res.send.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('res', function(){
139139

140140
request(app)
141141
.get('/')
142-
.expect('Content-Type', 'text/plain')
142+
.expect('Content-Type', 'text/plain; charset=utf-8')
143143
.expect('hey')
144144
.expect(200, done);
145145
})
@@ -187,7 +187,7 @@ describe('res', function(){
187187
request(app)
188188
.get('/')
189189
.end(function(err, res){
190-
res.headers.should.have.property('content-type', 'text/plain');
190+
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');
191191
res.text.should.equal('hey');
192192
res.statusCode.should.equal(200);
193193
done();
@@ -206,7 +206,7 @@ describe('res', function(){
206206
request(app)
207207
.get('/')
208208
.end(function(err, res){
209-
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
209+
res.headers.should.have.property('content-type', 'application/json');
210210
res.text.should.equal('{"name":"tobi"}');
211211
done();
212212
})

test/res.sendfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('res', function(){
6767

6868
request(app)
6969
.get('/')
70-
.expect('Content-Type', 'text/plain')
70+
.expect('Content-Type', 'text/plain; charset=utf-8')
7171
.end(done);
7272
})
7373

test/res.set.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ describe('res', function(){
99
var app = express();
1010

1111
app.use(function(req, res){
12-
res.set('Content-Type', 'text/x-foo').end();
12+
res.set('Content-Type', 'text/x-foo; charset=utf-8').end();
1313
});
1414

1515
request(app)
1616
.get('/')
17-
.expect('Content-Type', 'text/x-foo')
17+
.expect('Content-Type', 'text/x-foo; charset=utf-8')
1818
.end(done);
1919
})
2020

@@ -44,6 +44,12 @@ describe('res', function(){
4444
res.set('ETag', [123, 456]);
4545
JSON.stringify(res.get('ETag')).should.equal('["123","456"]');
4646
})
47+
48+
it('should not set a charset of one is already set', function () {
49+
res.headers = {};
50+
res.set('Content-Type', 'text/html; charset=lol');
51+
res.get('content-type').should.equal('text/html; charset=lol');
52+
})
4753
})
4854

4955
describe('.set(object)', function(){

0 commit comments

Comments
 (0)