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
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 1why, one step back1Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 2–5why, one step back2var assert = require('assert');3var express = require('..');4var methods = require('methods');5var request = require('supertest');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 6–18why, one step back67describe('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')build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84
line 19why, one step back19 .expect(200, '', done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 20–32why, one step back20 })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('/')build: support io.js 1.xDouglas Christopher Wilsonbefore adoptionc894c84
line 33why, one step back33 .expect(200, '', done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 34–114why, one step back34 })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 })114Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 115why, one step back115 it('should set ETag', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 116–117why, one step back116 var app = express();117Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 118why, one step back118 app.use(function (req, res) {Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 119why, one step back119 var str = Array(1000).join('-');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 120–124why, one step back120 res.send(str);121 });122123 request(app)124 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 125why, one step back125 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 126why, one step back126 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 127–186why, one step back127 })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 })186Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 187why, one step back187 it('should set ETag', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 188–189why, one step back188 var app = express();189Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 190why, one step back190 app.use(function (req, res) {Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 191why, one step back191 var str = Array(1000).join('-');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 192–196why, one step back192 res.send(new Buffer(str));193 });194195 request(app)196 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 197why, one step back197 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 198why, one step back198 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 199–304why, one step back199 })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();Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 305why, one step back305 var etag = '"asdf"';docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 306–307why, one step back306307 app.use(function(req, res){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 308–309why, one step back308 var str = Array(1000).join('-');309 res.set('ETag', etag);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 310–314why, one step back310 res.send(str);311 });312313 request(app)314 .get('/')Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 315why, one step back315 .set('If-None-Match', etag)docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 316–347why, one step back316 .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 })347Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 348–350why, one step back348 describe('"etag" setting', function () {349 describe('when enabled', function () {350 it('should send ETag', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 351–352why, one step back351 var app = express();352Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 353why, one step back353 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 354–360why, one step back354 res.send('kajdslfkasdf');355 });356357 app.enable('etag');358359 request(app)360 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 361why, one step back361 .expect('ETag', 'W/"c-ZUfd0NJ26qwjlKF4r8qb2g"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 362–363why, one step back362 .expect(200, done);363 });docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 364why, one step back364Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 365–366why, one step back365 methods.forEach(function (method) {366 if (method === 'connect') return;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 367why, one step back367Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 368–376why, one step back368 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]('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 377why, one step back377 .expect('ETag', 'W/"c-ZUfd0NJ26qwjlKF4r8qb2g"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 378–386why, one step back378 .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('');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 387–388why, one step back387 });388Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 389why, one step back389 app.enable('etag');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 390–392why, one step back390391 request(app)392 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 393why, one step back393 .expect('ETag', 'W/"0-1B2M2Y8AsgTpgAmY7PhCfg"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 394why, one step back394 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 395–396why, one step back395 })396Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 397why, one step back397 it('should send ETag for long response', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 398–399why, one step back398 var app = express();399Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 400why, one step back400 app.use(function (req, res) {Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 401why, one step back401 var str = Array(1000).join('-');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 402–408why, one step back402 res.send(str);403 });404405 app.enable('etag');406407 request(app)408 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 409why, one step back409 .expect('ETag', 'W/"3e7-VYgCBglFKiDVAcpzPNt4Sg"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 410why, one step back410 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 411–412why, one step back411 });412Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 413why, one step back413 it('should not override ETag when manually set', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 414–415why, one step back414 var app = express();415Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 416why, one step back416 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 417–424why, one step back417 res.set('etag', '"asdf"');418 res.send(200);419 });420421 app.enable('etag');422423 request(app)424 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 425–426why, one step back425 .expect('ETag', '"asdf"')426 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 427–428why, one step back427 });428Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 429–430why, one step back429 it('should not send ETag for res.send()', function (done) {430 var app = express();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 431why, one step back431Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 432–433why, one step back432 app.use(function (req, res) {433 res.send();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 434–435why, one step back434 });435Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 436why, one step back436 app.enable('etag');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 437–439why, one step back437438 request(app)439 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 440–441why, one step back440 .expect(shouldNotHaveHeader('ETag'))441 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 442–444why, one step back442 })443 });444Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 445–446why, one step back445 describe('when disabled', function () {446 it('should send no ETag', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 447–448why, one step back447 var app = express();448Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 449why, one step back449 app.use(function (req, res) {Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 450why, one step back450 var str = Array(1000).join('-');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 451–457why, one step back451 res.send(str);452 });453454 app.disable('etag');455456 request(app)457 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 458–459why, one step back458 .expect(shouldNotHaveHeader('ETag'))459 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 460–461why, one step back460 });461Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 462why, one step back462 it('should send ETag when manually set', function (done) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 463–466why, one step back463 var app = express();464465 app.disable('etag');466Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 467why, one step back467 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 468–473why, one step back468 res.set('etag', '"asdf"');469 res.send(200);470 });471472 request(app)473 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 474–475why, one step back474 .expect('ETag', '"asdf"')475 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 476–478why, one step back476 });477 });478Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 479–481why, one step back479 describe('when "strong"', function () {480 it('should send strong ETag', function (done) {481 var app = express();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 482–484why, one step back482483 app.set('etag', 'strong');484Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 485why, one step back485 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 486–490why, one step back486 res.send('hello, world!');487 });488489 request(app)490 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 491why, one step back491 .expect('ETag', '"d-Otu60XkfuuPskIiUxJY4cA"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 492why, one step back492 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 493–495why, one step back493 })494 })495Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 496–498why, one step back496 describe('when "weak"', function () {497 it('should send weak ETag', function (done) {498 var app = express();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 499–501why, one step back499500 app.set('etag', 'weak');501Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 502why, one step back502 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 503–507why, one step back503 res.send('hello, world!');504 });505506 request(app)507 .get('/')Merge tag '3.21.0'Douglas Christopher Wilsonbefore adoption3d2ecdd
line 508why, one step back508 .expect('ETag', 'W/"d-Otu60XkfuuPskIiUxJY4cA"')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 509why, one step back509 .expect(200, done)docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 510–512why, one step back510 })511 })512Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 513–515why, one step back513 describe('when a function', function () {514 it('should send custom ETag', function (done) {515 var app = express();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 516why, one step back516Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 517why, one step back517 app.set('etag', function (body, encoding) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 518–520why, one step back518 var chunk = !Buffer.isBuffer(body)519 ? new Buffer(body, encoding)520 : body;Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 521–522why, one step back521 chunk.toString().should.equal('hello, world!');522 return '"custom"';docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 523–524why, one step back523 });524Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 525why, one step back525 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 526–530why, one step back526 res.send('hello, world!');527 });528529 request(app)530 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 531–532why, one step back531 .expect('ETag', '"custom"')532 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 533–534why, one step back533 })534Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 535–536why, one step back535 it('should not send falsy ETag', function (done) {536 var app = express();docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 537why, one step back537Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 538–539why, one step back538 app.set('etag', function (body, encoding) {539 return undefined;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 540–541why, one step back540 });541Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 542why, one step back542 app.use(function (req, res) {docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 543–547why, one step back543 res.send('hello, world!');544 });545546 request(app)547 .get('/')Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 548–549why, one step back548 .expect(shouldNotHaveHeader('ETag'))549 .expect(200, done);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 550–553why, one step back550 })551 })552 })553})Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 554–559why, one step back554555function shouldNotHaveHeader(header) {556 return function (res) {557 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)558 }559}