Skip to content

Commit 1311f2a

Browse files
committed
Fixed res.redirect() 406. Closes expressjs#1154
1 parent 82a9817 commit 1311f2a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/response.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ res.type = function(type){
346346
* @api public
347347
*/
348348

349-
res.format = function(obj){
349+
res.format = function(obj, fn){
350350
var keys = Object.keys(obj)
351351
, req = this.req
352352
, next = req.next
@@ -357,6 +357,8 @@ res.format = function(obj){
357357
if (key) {
358358
this.set('Content-Type', normalizeType(key));
359359
obj[key](req, this, next);
360+
} else if (fn) {
361+
fn();
360362
} else {
361363
var err = new Error('Not Acceptable');
362364
err.status = 406;
@@ -555,11 +557,14 @@ res.redirect = function(url){
555557
html: function(){
556558
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
557559
}
560+
}, function(){
561+
body = '';
558562
})
559563

560564
// Respond
561565
this.statusCode = status;
562566
this.set('Location', url);
567+
this.set('Content-Length', Buffer.byteLength(body));
563568
this.end(head ? null : body);
564569
};
565570

test/res.redirect.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,32 @@ describe('res', function(){
248248
.set('Accept', 'text/plain, */*')
249249
.end(function(res){
250250
res.headers.should.have.property('location', 'http://google.com');
251+
res.headers.should.have.property('content-length', '51');
251252
res.body.should.equal('Moved Temporarily. Redirecting to http://google.com');
252253
done();
253254
})
254255
})
255256
})
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+
})
256279
})

0 commit comments

Comments
 (0)