move setHeader charset patch to .set
Before adoption: this commit came in with the history the project adopted, so no landing records it.
note that application/json no longer adds charset=utf-8. could be a regression. closes #1952 See also: https://github.com/broofa/node-mime/issues/86
authored by Jonathan Ong · committed by Jonathan Ong · parent 3cf7b2e · Browse files · 8 files·+42 added−87 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+42 added−87 removed
- tests:+23 added−51 removed
- other:+19 added−36 removed
Provenance
- Commit
bad55f7- Tree
c344db4- Author
Jonathan Ong <jonathanrichardong@gmail.com>- Committer
Jonathan Ong <jonathanrichardong@gmail.com>- Parent
3cf7b2e
8 files changed+42 added−87 removed
modified:lib/response.js+19 added-36 removed
1 /**2 * Module dependencies.3 */4 5-var http = require('http')6- , path = require('path')7- , mixin = require('utils-merge')8- , escapeHtml = require('escape-html')9- , sign = require('cookie-signature').sign10- , normalizeType = require('./utils').normalizeType11- , normalizeTypes = require('./utils').normalizeTypes12- , etag = require('./utils').etag13- , statusCodes = http.STATUS_CODES14- , cookie = require('cookie')15- , send = require('send')16- , basename = path.basename17- , extname = path.extname18- , 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;19 20 /**21 * Response prototype.22 */499 unmodified lines522 res.header = function(field, val){523 if (2 == arguments.length) {524 if (Array.isArray(val)) val = val.map(String);525 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+ }531 this.setHeader(field, val);532 } else {533 for (var key in field) {534 this.set(key, field[key]);247 unmodified lines782 783 // render784 app.render(view, options, fn);785 };783-784-785-/**786- * Set header `field` to `val`, special-casing787- * the `Set-Cookie` field for multiple support.788- *789- * @param {String} field790- * @param {String} val791- * @api public792- */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-};modified:test/req.is.js+1 added-1 removed
14 unmodified lines1516171819192021222380 unmodified lines14 unmodified lines}
describe('req.is()', function(){ it('should ignore charset', function(){ req('application/json; charset=utf-8') req('application/json') .is('json') .should.equal('json'); })
80 unmodified linesdeleted:test/res.charset.js-34 removed
12345678910111213141516171819202122232425262728293031323334
var express = require('../') , request = require('supertest');
describe('res', function(){ describe('.charset', function(){ it('should add the charset param to Content-Type', function(done){ var app = express();
app.use(function(req, res){ res.charset = 'utf-8'; res.set('Content-Type', 'text/x-foo'); res.end(res.get('Content-Type')); });
request(app) .get('/') .expect("text/x-foo; charset=utf-8", done); })
it('should take precedence over res.send() defaults', function(done){ var app = express();
app.use(function(req, res){ res.charset = 'whoop'; res.send('hey'); });
request(app) .get('/') .expect('Content-Type', 'text/html; charset=whoop', done); }) })})modified:test/res.json.js+5 added-5 removed
26 unmodified lines27 28 request(app)29 .get('/')30 .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');32 res.text.should.equal('null');33 done();34 })35 })9 unmodified lines45 46 request(app)47 .get('/')48 .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');50 res.text.should.equal('["foo","bar","baz"]');51 done();52 })53 })9 unmodified lines63 64 request(app)65 .get('/')66 .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');68 res.text.should.equal('{"name":"tobi"}');69 done();70 })71 })58 unmodified lines130 request(app)131 .get('/')132 .end(function(err, res){133 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');135 res.text.should.equal('{"id":1}');136 done();137 })138 })10 unmodified lines149 request(app)150 .get('/')151 .end(function(err, res){152 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');154 res.text.should.equal('{"id":1}');155 done();156 })157 })20 unmodified linesmodified:test/res.jsonp.js+5 added-5 removed
112 unmodified lines113 114 request(app)115 .get('/')116 .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');118 res.text.should.equal('null');119 done();120 })121 })9 unmodified lines131 132 request(app)133 .get('/')134 .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');136 res.text.should.equal('["foo","bar","baz"]');137 done();138 })139 })9 unmodified lines149 150 request(app)151 .get('/')152 .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');154 res.text.should.equal('{"name":"tobi"}');155 done();156 })157 })58 unmodified lines216 request(app)217 .get('/')218 .end(function(err, res){219 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');221 res.text.should.equal('{"id":1}');222 done();223 })224 })10 unmodified lines235 request(app)236 .get('/')237 .end(function(err, res){238 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');240 res.text.should.equal('{"id":1}');241 done();242 })243 })2 unmodified linesmodified:test/res.send.js+3 added-3 removed
137 unmodified lines138 });139 140 request(app)141 .get('/')142- .expect('Content-Type', 'text/plain')142+ .expect('Content-Type', 'text/plain; charset=utf-8')143 .expect('hey')144 .expect(200, done);145 })146 })39 unmodified lines186 187 request(app)188 .get('/')189 .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');191 res.text.should.equal('hey');192 res.statusCode.should.equal(200);193 done();194 })10 unmodified lines205 206 request(app)207 .get('/')208 .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');210 res.text.should.equal('{"name":"tobi"}');211 done();212 })213 })185 unmodified linesmodified:test/res.sendfile.js+1 added-1 removed
65 unmodified lines66 });67 68 request(app)69 .get('/')70- .expect('Content-Type', 'text/plain')70+ .expect('Content-Type', 'text/plain; charset=utf-8')71 .end(done);72 })73 74 it('should invoke the callback on 403', function(done){168 unmodified linesmodified:test/res.set.js+8 added-2 removed
7 unmodified lines89101112121314151617171819202121 unmodified lines434445464748495051525354555623 unmodified lines7 unmodified lines it('should set the response header field', function(done){ var app = express();
app.use(function(req, res){ res.set('Content-Type', 'text/x-foo').end(); res.set('Content-Type', 'text/x-foo; charset=utf-8').end(); });
request(app) .get('/') .expect('Content-Type', 'text/x-foo') .expect('Content-Type', 'text/x-foo; charset=utf-8') .end(done); })
it('should coerce to a string', function(){21 unmodified lines res.headers = {}; res.set('ETag', [123, 456]); JSON.stringify(res.get('ETag')).should.equal('["123","456"]'); })
it('should not set a charset of one is already set', function () { res.headers = {}; res.set('Content-Type', 'text/html; charset=lol'); res.get('content-type').should.equal('text/html; charset=lol'); }) })
describe('.set(object)', function(){ it('should set multiple fields', function(done){23 unmodified lines