res.send.js
This file
Path
test/res.send.js
559 lines · 12.9 KiB · JavaScript
Last changed by
Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
History of this fileTake it
12var assert = require('assert');3var express = require('..');4var methods = require('methods');5var request = require('supertest');67describe('res', function(){8 describe('.send(null)', function(){9 it('should set body to ""', function(done){10 var app = express();1112 app.use(function(req, res){13 res.send(null);14 });1516 request(app)17 .get('/')18 .expect('Content-Length', '0')19 .expect(200, '', done);20 })21 })2223 describe('.send(undefined)', function(){24 it('should set body to ""', function(done){25 var app = express();2627 app.use(function(req, res){28 res.send(undefined);29 });3031 request(app)32 .get('/')33 .expect(200, '', done);34 })35 })3637 describe('.send(code)', function(){38 it('should set .statusCode', function(done){39 var app = express();4041 app.use(function(req, res){42 res.send(201).should.equal(res);43 });4445 request(app)46 .get('/')47 .expect('Created')48 .expect(201, done);49 })50 })5152 describe('.send(code, body)', function(){53 it('should set .statusCode and body', function(done){54 var app = express();5556 app.use(function(req, res){57 res.send(201, 'Created :)');58 });5960 request(app)61 .get('/')62 .expect('Created :)')63 .expect(201, done);64 })65 })6667 describe('.send(body, code)', function(){68 it('should be supported for backwards compat', function(done){69 var app = express();7071 app.use(function(req, res){72 res.send('Bad!', 400);73 });7475 request(app)76 .get('/')77 .expect('Bad!')78 .expect(400, done);79 })80 })8182 describe('.send(code, number)', function(){83 it('should send number as json', function(done){84 var app = express();8586 app.use(function(req, res){87 res.send(200, 0.123);88 });8990 request(app)91 .get('/')92 .expect('Content-Type', 'application/json; charset=utf-8')93 .expect(200, '0.123', done);94 })95 })9697 describe('.send(String)', function(){98 it('should send as html', function(done){99 var app = express();100101 app.use(function(req, res){102 res.send('<p>hey</p>');103 });104105 request(app)106 .get('/')107 .end(function(err, res){108 res.headers.should.have.property('content-type', 'text/html; charset=utf-8');109 res.text.should.equal('<p>hey</p>');110 res.statusCode.should.equal(200);111 done();112 })113 })114115 it('should set ETag', function (done) {116 var app = express();117118 app.use(function (req, res) {119 var str = Array(1000).join('-');120 res.send(str);121 });122123 request(app)124 .get('/')125 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')126 .expect(200, done);127 })128129 it('should not override Content-Type', function(done){130 var app = express();131132 app.use(function(req, res){133 res.set('Content-Type', 'text/plain').send('hey');134 });135136 request(app)137 .get('/')138 .expect('Content-Type', 'text/plain; charset=utf-8')139 .expect(200, 'hey', done);140 })141142 it('should override charset in Content-Type', function(done){143 var app = express();144145 app.use(function(req, res){146 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');147 });148149 request(app)150 .get('/')151 .expect('Content-Type', 'text/plain; charset=utf-8')152 .expect(200, 'hey', done);153 })154155 it('should keep charset in Content-Type for Buffers', function(done){156 var app = express();157158 app.use(function(req, res){159 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));160 });161162 request(app)163 .get('/')164 .expect('Content-Type', 'text/plain; charset=iso-8859-1')165 .expect(200, 'hi', done);166 })167 })168169 describe('.send(Buffer)', function(){170 it('should send as octet-stream', function(done){171 var app = express();172173 app.use(function(req, res){174 res.send(new Buffer('hello'));175 });176177 request(app)178 .get('/')179 .end(function(err, res){180 res.headers.should.have.property('content-type', 'application/octet-stream');181 res.text.should.equal('hello');182 res.statusCode.should.equal(200);183 done();184 })185 })186187 it('should set ETag', function (done) {188 var app = express();189190 app.use(function (req, res) {191 var str = Array(1000).join('-');192 res.send(new Buffer(str));193 });194195 request(app)196 .get('/')197 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')198 .expect(200, done);199 })200201 it('should not override Content-Type', function(done){202 var app = express();203204 app.use(function(req, res){205 res.set('Content-Type', 'text/plain').send(new Buffer('hey'));206 });207208 request(app)209 .get('/')210 .end(function(err, res){211 res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');212 res.text.should.equal('hey');213 res.statusCode.should.equal(200);214 done();215 })216 })217 })218219 describe('.send(Object)', function(){220 it('should send as application/json', function(done){221 var app = express();222223 app.use(function(req, res){224 res.send({ name: 'tobi' });225 });226227 request(app)228 .get('/')229 .expect('Content-Type', 'application/json; charset=utf-8')230 .expect(200, '{"name":"tobi"}', done)231 })232 })233234 describe('when the request method is HEAD', function(){235 it('should ignore the body', function(done){236 var app = express();237238 app.use(function(req, res){239 res.send('yay');240 });241242 request(app)243 .head('/')244 .expect('', done);245 })246 })247248 describe('when .statusCode is 204', function(){249 it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){250 var app = express();251252 app.use(function(req, res){253 res.status(204).set('Transfer-Encoding', 'chunked').send('foo');254 });255256 request(app)257 .get('/')258 .end(function(err, res){259 res.headers.should.not.have.property('content-type');260 res.headers.should.not.have.property('content-length');261 res.headers.should.not.have.property('transfer-encoding');262 res.text.should.equal('');263 done();264 })265 })266 })267268 describe('when .statusCode is 304', function(){269 it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){270 var app = express();271272 app.use(function(req, res){273 res.status(304).set('Transfer-Encoding', 'chunked').send('foo');274 });275276 request(app)277 .get('/')278 .end(function(err, res){279 res.headers.should.not.have.property('content-type');280 res.headers.should.not.have.property('content-length');281 res.headers.should.not.have.property('transfer-encoding');282 res.text.should.equal('');283 done();284 })285 })286 })287288 it('should always check regardless of length', function(done){289 var app = express();290 var etag = '"asdf"';291292 app.use(function(req, res, next){293 res.set('ETag', etag);294 res.send('hey');295 });296297 request(app)298 .get('/')299 .set('If-None-Match', etag)300 .expect(304, done);301 })302303 it('should respond with 304 Not Modified when fresh', function(done){304 var app = express();305 var etag = '"asdf"';306307 app.use(function(req, res){308 var str = Array(1000).join('-');309 res.set('ETag', etag);310 res.send(str);311 });312313 request(app)314 .get('/')315 .set('If-None-Match', etag)316 .expect(304, done);317 })318319 it('should not perform freshness check unless 2xx or 304', function(done){320 var app = express();321 var etag = '"asdf"';322323 app.use(function(req, res, next){324 res.status(500);325 res.set('ETag', etag);326 res.send('hey');327 });328329 request(app)330 .get('/')331 .set('If-None-Match', etag)332 .expect('hey')333 .expect(500, done);334 })335336 it('should not support jsonp callbacks', function(done){337 var app = express();338339 app.use(function(req, res){340 res.send({ foo: 'bar' });341 });342343 request(app)344 .get('/?callback=foo')345 .expect('{"foo":"bar"}', done);346 })347348 describe('"etag" setting', function () {349 describe('when enabled', function () {350 it('should send ETag', function (done) {351 var app = express();352353 app.use(function (req, res) {354 res.send('kajdslfkasdf');355 });356357 app.enable('etag');358359 request(app)360 .get('/')361 .expect('ETag', 'W/"c-ZUfd0NJ26qwjlKF4r8qb2g"')362 .expect(200, done);363 });364365 methods.forEach(function (method) {366 if (method === 'connect') return;367368 it('should send ETag in response to ' + method.toUpperCase() + ' request', function (done) {369 var app = express();370371 app[method]('/', function (req, res) {372 res.send('kajdslfkasdf');373 });374375 request(app)376 [method]('/')377 .expect('ETag', 'W/"c-ZUfd0NJ26qwjlKF4r8qb2g"')378 .expect(200, done);379 })380 });381382 it('should send ETag for empty string response', function (done) {383 var app = express();384385 app.use(function (req, res) {386 res.send('');387 });388389 app.enable('etag');390391 request(app)392 .get('/')393 .expect('ETag', 'W/"0-1B2M2Y8AsgTpgAmY7PhCfg"')394 .expect(200, done);395 })396397 it('should send ETag for long response', function (done) {398 var app = express();399400 app.use(function (req, res) {401 var str = Array(1000).join('-');402 res.send(str);403 });404405 app.enable('etag');406407 request(app)408 .get('/')409 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')410 .expect(200, done);411 });412413 it('should not override ETag when manually set', function (done) {414 var app = express();415416 app.use(function (req, res) {417 res.set('etag', '"asdf"');418 res.send(200);419 });420421 app.enable('etag');422423 request(app)424 .get('/')425 .expect('ETag', '"asdf"')426 .expect(200, done);427 });428429 it('should not send ETag for res.send()', function (done) {430 var app = express();431432 app.use(function (req, res) {433 res.send();434 });435436 app.enable('etag');437438 request(app)439 .get('/')440 .expect(shouldNotHaveHeader('ETag'))441 .expect(200, done);442 })443 });444445 describe('when disabled', function () {446 it('should send no ETag', function (done) {447 var app = express();448449 app.use(function (req, res) {450 var str = Array(1000).join('-');451 res.send(str);452 });453454 app.disable('etag');455456 request(app)457 .get('/')458 .expect(shouldNotHaveHeader('ETag'))459 .expect(200, done);460 });461462 it('should send ETag when manually set', function (done) {463 var app = express();464465 app.disable('etag');466467 app.use(function (req, res) {468 res.set('etag', '"asdf"');469 res.send(200);470 });471472 request(app)473 .get('/')474 .expect('ETag', '"asdf"')475 .expect(200, done);476 });477 });478479 describe('when "strong"', function () {480 it('should send strong ETag', function (done) {481 var app = express();482483 app.set('etag', 'strong');484485 app.use(function (req, res) {486 res.send('hello, world!');487 });488489 request(app)490 .get('/')491 .expect('ETag', '"d-Otu60XkfuuPskIiUxJY4cA"')492 .expect(200, done);493 })494 })495496 describe('when "weak"', function () {497 it('should send weak ETag', function (done) {498 var app = express();499500 app.set('etag', 'weak');501502 app.use(function (req, res) {503 res.send('hello, world!');504 });505506 request(app)507 .get('/')508 .expect('ETag', 'W/"d-Otu60XkfuuPskIiUxJY4cA"')509 .expect(200, done)510 })511 })512513 describe('when a function', function () {514 it('should send custom ETag', function (done) {515 var app = express();516517 app.set('etag', function (body, encoding) {518 var chunk = !Buffer.isBuffer(body)519 ? new Buffer(body, encoding)520 : body;521 chunk.toString().should.equal('hello, world!');522 return '"custom"';523 });524525 app.use(function (req, res) {526 res.send('hello, world!');527 });528529 request(app)530 .get('/')531 .expect('ETag', '"custom"')532 .expect(200, done);533 })534535 it('should not send falsy ETag', function (done) {536 var app = express();537538 app.set('etag', function (body, encoding) {539 return undefined;540 });541542 app.use(function (req, res) {543 res.send('hello, world!');544 });545546 request(app)547 .get('/')548 .expect(shouldNotHaveHeader('ETag'))549 .expect(200, done);550 })551 })552 })553})554555function shouldNotHaveHeader(header) {556 return function (res) {557 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)558 }559}