Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
31 changes: 31 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down