diff --git a/lib/response.js b/lib/response.js index b4755a5c060..f7a64337c5a 100644 --- a/lib/response.js +++ b/lib/response.js @@ -152,6 +152,11 @@ res.send = function send(body) { if (!this.get('Content-Type')) { this.type('bin'); } + } else if (chunk instanceof ArrayBuffer || (chunk && chunk.constructor && chunk.constructor.name === 'ArrayBuffer')) { + chunk = Buffer.from(chunk); + if (!this.get('Content-Type')) { + this.type('bin'); + } } else { return this.json(chunk); } diff --git a/test/res.send.js b/test/res.send.js index f38ffde9776..912db6be34f 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -231,6 +231,37 @@ describe('res', function(){ }) }) + describe('.send(ArrayBuffer)', function(){ + it('should send as octet-stream', function(done){ + var app = express(); + + app.use(function(req, res){ + res.send(new ArrayBuffer(8)) + }); + + request(app) + .get('/') + .expect(200) + .expect('Content-Type', 'application/octet-stream') + .expect('Content-Length', '8') + .expect(utils.shouldHaveBody(Buffer.alloc(8, 0))) + .end(done) + }) + + it('should not override Content-Type', function(done){ + var app = express(); + + app.use(function(req, res){ + res.set('Content-Type', 'text/plain').send(new ArrayBuffer(8)) + }); + + request(app) + .get('/') + .expect('Content-Type', 'text/plain; charset=utf-8') + .expect(200, done); + }) + }) + describe('.send(Object)', function(){ it('should send as application/json', function(done){ var app = express();