Skip to content

Commit cb19f04

Browse files
Upgrade content-type (#7234)
* Upgrade content-type * Bump type-is * test: add tests for content-type handling in res.send() --------- Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>
1 parent a08da78 commit cb19f04

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

History.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
app.render('index', null, callback); // now works as expected
1111
```
1212

13+
* Upgrade `content-type` to `^2.0.0`, bringing a faster parser (~1.5x quicker `Content-Type` parsing/formatting in `res.send()`) along with a behavior change: `res.send()` now keeps any existing parameters when adding the charset and no longer throws on a `Content-Type` that fails to parse - by [@blakeembrey](https://github.com/blakeembrey) in [#7234](https://github.com/expressjs/express/pull/7234)
14+
15+
```js
16+
res.set('Content-Type', 'text/plain; foo=bar').send('hey');
17+
// -> Content-Type: text/plain; foo=bar; charset=utf-8
18+
```
19+
1320
## ⚡ Performance
1421

1522
* Avoid duplicate Content-Type header processing in `res.send()` when sending string responses without an explicit Content-Type header - by [@bjohansebas](https://github.com/bjohansebas) in [#6991](https://github.com/expressjs/express/pull/6991)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"accepts": "^2.0.0",
3636
"body-parser": "^2.2.1",
3737
"content-disposition": "^1.0.0",
38-
"content-type": "^1.0.5",
38+
"content-type": "^2.0.0",
3939
"cookie": "^0.7.1",
4040
"cookie-signature": "^1.2.1",
4141
"debug": "^4.4.0",
@@ -58,7 +58,7 @@
5858
"send": "^1.1.0",
5959
"serve-static": "^2.2.0",
6060
"statuses": "^2.0.1",
61-
"type-is": "^2.0.1",
61+
"type-is": "^2.1.0",
6262
"vary": "^1.1.2"
6363
},
6464
"devDependencies": {

test/res.send.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ describe('res', function(){
122122
.expect(200, 'hey', done);
123123
})
124124

125+
it('should preserve existing parameters when adding charset', function(done){
126+
var app = express();
127+
128+
app.use(function(req, res){
129+
res.set('Content-Type', 'text/plain; foo=bar').send('hey');
130+
});
131+
132+
request(app)
133+
.get('/')
134+
.expect('Content-Type', 'text/plain; foo=bar; charset=utf-8')
135+
.expect(200, 'hey', done);
136+
})
137+
138+
it('should not throw on a Content-Type that fails to parse', function(done){
139+
var app = express();
140+
141+
app.use(function(req, res){
142+
res.set('Content-Type', 'text/plain; foo').send('hey');
143+
});
144+
145+
request(app)
146+
.get('/')
147+
.expect('Content-Type', 'text/plain; charset=utf-8')
148+
.expect(200, 'hey', done);
149+
})
150+
125151
it('should keep charset in Content-Type for Buffers', function(done){
126152
var app = express();
127153

0 commit comments

Comments
 (0)