File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -346,7 +346,7 @@ res.type = function(type){
346
346
* @api public
347
347
*/
348
348
349
- res . format = function ( obj ) {
349
+ res . format = function ( obj , fn ) {
350
350
var keys = Object . keys ( obj )
351
351
, req = this . req
352
352
, next = req . next
@@ -357,6 +357,8 @@ res.format = function(obj){
357
357
if ( key ) {
358
358
this . set ( 'Content-Type' , normalizeType ( key ) ) ;
359
359
obj [ key ] ( req , this , next ) ;
360
+ } else if ( fn ) {
361
+ fn ( ) ;
360
362
} else {
361
363
var err = new Error ( 'Not Acceptable' ) ;
362
364
err . status = 406 ;
@@ -555,11 +557,14 @@ res.redirect = function(url){
555
557
html : function ( ) {
556
558
body = '<p>' + statusCodes [ status ] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>' ;
557
559
}
560
+ } , function ( ) {
561
+ body = '' ;
558
562
} )
559
563
560
564
// Respond
561
565
this . statusCode = status ;
562
566
this . set ( 'Location' , url ) ;
567
+ this . set ( 'Content-Length' , Buffer . byteLength ( body ) ) ;
563
568
this . end ( head ? null : body ) ;
564
569
} ;
565
570
Original file line number Diff line number Diff line change @@ -248,9 +248,32 @@ describe('res', function(){
248
248
. set ( 'Accept' , 'text/plain, */*' )
249
249
. end ( function ( res ) {
250
250
res . headers . should . have . property ( 'location' , 'http://google.com' ) ;
251
+ res . headers . should . have . property ( 'content-length' , '51' ) ;
251
252
res . body . should . equal ( 'Moved Temporarily. Redirecting to http://google.com' ) ;
252
253
done ( ) ;
253
254
} )
254
255
} )
255
256
} )
257
+
258
+ describe ( 'when accepting neither text or html' , function ( ) {
259
+ it ( 'should respond with an empty body' , function ( done ) {
260
+ var app = express ( ) ;
261
+
262
+ app . use ( function ( req , res ) {
263
+ res . redirect ( 'http://google.com' ) ;
264
+ } ) ;
265
+
266
+ request ( app )
267
+ . get ( '/' )
268
+ . set ( 'Accept' , 'foo/bar' )
269
+ . end ( function ( res ) {
270
+ res . should . have . status ( 302 ) ;
271
+ res . headers . should . have . property ( 'location' , 'http://google.com' ) ;
272
+ res . headers . should . not . have . property ( 'content-type' ) ;
273
+ res . headers . should . have . property ( 'content-length' , '0' ) ;
274
+ res . body . should . equal ( '' ) ;
275
+ done ( ) ;
276
+ } )
277
+ } )
278
+ } )
256
279
} )
You can’t perform that action at this time.
0 commit comments