Skip to content
Myr

res.send.js

This file

Path

test/res.send.js

530 lines · 11.9 KiB · JavaScript

Last changed by

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

History of this file

Take it

The walk stopped after 200 commits: lines older than a10f695 point at it.

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 1–2why, one step back
1
2var express = require('../')

remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3

line 3why, one step back
3 , request = require('supertest')

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 4–107why, one step back
4 , assert = require('assert');
56describe('res', function(){7 describe('.send(null)', function(){8 it('should set body to ""', function(done){9 var app = express();1011 app.use(function(req, res){12 res.send(null);13 });1415 request(app)16 .get('/')17 .expect('', done);18 })19 })2021 describe('.send(undefined)', function(){22 it('should set body to ""', function(done){23 var app = express();2425 app.use(function(req, res){26 res.send(undefined);27 });2829 request(app)30 .get('/')31 .expect('', done);32 })33 })3435 describe('.send(code)', function(){36 it('should set .statusCode', function(done){37 var app = express();3839 app.use(function(req, res){40 res.send(201).should.equal(res);41 });4243 request(app)44 .get('/')45 .expect('Created')46 .expect(201, done);47 })48 })4950 describe('.send(code, body)', function(){51 it('should set .statusCode and body', function(done){52 var app = express();5354 app.use(function(req, res){55 res.send(201, 'Created :)');56 });5758 request(app)59 .get('/')60 .expect('Created :)')61 .expect(201, done);62 })63 })6465 describe('.send(body, code)', function(){66 it('should be supported for backwards compat', function(done){67 var app = express();6869 app.use(function(req, res){70 res.send('Bad!', 400);71 });7273 request(app)74 .get('/')75 .expect('Bad!')76 .expect(400, done);77 })78 })7980 describe('.send(String)', function(){81 it('should send as html', function(done){82 var app = express();8384 app.use(function(req, res){85 res.send('<p>hey</p>');86 });8788 request(app)89 .get('/')90 .end(function(err, res){91 res.headers.should.have.property('content-type', 'text/html; charset=utf-8');92 res.text.should.equal('<p>hey</p>');93 res.statusCode.should.equal(200);94 done();95 })96 })9798 it('should set ETag', function(done){99 var app = express();100101 app.use(function(req, res){102 var str = Array(1024 * 2).join('-');103 res.send(str);104 });105106 request(app)107 .get('/')

mark res.send ETag as weak and reduce collisionsDouglas Christopher Wilsonbefore adoption8ab96ab

line 108why, one step back
108 .expect('ETag', 'W/"7ff-2796319984"')

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 109–137why, one step back
109 .end(done);
110 })111112 it('should not set ETag for non-GET/HEAD', function(done){113 var app = express();114115 app.use(function(req, res){116 var str = Array(1024 * 2).join('-');117 res.send(str);118 });119120 request(app)121 .post('/')122 .end(function(err, res){123 if (err) return done(err);124 assert(!res.header.etag, 'has an ETag');125 done();126 });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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 138–139why, one step back
138 .expect('Content-Type', 'text/plain; charset=utf-8')
139 .expect(200, 'hey', done);

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

line 140why, one step back
140 })

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 141–166why, one step back
141
142 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 })

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 167–196why, one step back
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(1024 * 2).join('-');192 res.send(new Buffer(str));193 });194195 request(app)196 .get('/')

mark res.send ETag as weak and reduce collisionsDouglas Christopher Wilsonbefore adoption8ab96ab

line 197why, one step back
197 .expect('ETag', 'W/"7ff-2796319984"')

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 198–228why, one step back
198 .end(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');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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 229–230why, one step back
229 .expect('Content-Type', 'application/json; charset=utf-8')
230 .expect(200, '{"name":"tobi"}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 231–289why, one step back
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();

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 290why, one step back
290 var etag = '"asdf"';

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 291–292why, one step back
291
292 app.use(function(req, res, next){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 293why, one step back
293 res.set('ETag', etag);

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 294–298why, one step back
294 res.send('hey');
295 });296297 request(app)298 .get('/')

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 299why, one step back
299 .set('If-None-Match', etag)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 300–312why, one step back
300 .expect(304, done);
301 })302303 it('should respond with 304 Not Modified when fresh', function(done){304 var app = express();305306 app.use(function(req, res){307 var str = Array(1024 * 2).join('-');308 res.send(str);309 });310311 request(app)312 .get('/')

mark res.send ETag as weak and reduce collisionsDouglas Christopher Wilsonbefore adoption8ab96ab

line 313why, one step back
313 .set('If-None-Match', 'W/"7ff-2796319984"')

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 314–318why, one step back
314 .expect(304, done);
315 })316317 it('should not perform freshness check unless 2xx or 304', function(done){318 var app = express();

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 319why, one step back
319 var etag = '"asdf"';

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 320–322why, one step back
320
321 app.use(function(req, res, next){322 res.status(500);

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 323why, one step back
323 res.set('ETag', etag);

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 324–328why, one step back
324 res.send('hey');
325 });326327 request(app)328 .get('/')

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 329why, one step back
329 .set('If-None-Match', etag)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 330–344why, one step back
330 .expect('hey')
331 .expect(500, done);332 })333334 it('should not support jsonp callbacks', function(done){335 var app = express();336337 app.use(function(req, res){338 res.send({ foo: 'bar' });339 });340341 request(app)342 .get('/?callback=foo')343 .expect('{"foo":"bar"}', done);344 })

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 345–347why, one step back
345
346 describe('"etag" setting', function(){347 describe('when enabled', function(){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 348why, one step back
348 it('should send ETag', function(done){

always send ETag when content-length > 0Jonathan Ongbefore adoption55d1a4f

lines 349–354why, one step back
349 var app = express();
350351 app.use(function(req, res){352 res.send('kajdslfkasdf');353 });354

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 355–356why, one step back
355 app.enable('etag');
356

always send ETag when content-length > 0Jonathan Ongbefore adoption55d1a4f

lines 357–358why, one step back
357 request(app)
358 .get('/')

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 359why, one step back
359 .expect('etag', 'W/"c-1525560792"', done)

always send ETag when content-length > 0Jonathan Ongbefore adoption55d1a4f

lines 360–361why, one step back
360 })
361

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 362–375why, one step back
362 it('should send ETag for empty string response', function(done){
363 var app = express()364365 app.use(function(req, res){366 res.send('')367 });368369 app.enable('etag')370371 request(app)372 .get('/')373 .expect('etag', 'W/"0-0"', done)374 })375

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 376why, one step back
376 it('should send ETag for long response', function(done){

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 377–383why, one step back
377 var app = express();
378379 app.use(function(req, res){380 var str = Array(1024 * 2).join('-');381 res.send(str);382 });383

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 384–385why, one step back
384 app.enable('etag');
385

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 386–387why, one step back
386 request(app)
387 .get('/')

mark res.send ETag as weak and reduce collisionsDouglas Christopher Wilsonbefore adoption8ab96ab

line 388why, one step back
388 .expect('etag', 'W/"7ff-2796319984"', done)

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

line 389why, one step back
389 });

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 390–404why, one step back
390
391 it('should not override ETag when manually set', function(done){392 var app = express();393394 app.use(function(req, res){395 res.set('etag', '"asdf"');396 res.send(200);397 });398399 app.enable('etag');400401 request(app)402 .get('/')403 .expect('etag', '"asdf"', done)404 });

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 405–421why, one step back
405
406 it('should not send ETag for res.send()', function(done){407 var app = express()408409 app.use(function(req, res){410 res.send()411 });412413 app.enable('etag')414415 request(app)416 .get('/')417 .end(function(err, res){418 res.headers.should.not.have.property('etag');419 done();420 })421 })

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 422–448why, one step back
422 });
423424 describe('when disabled', function(){425 it('should send no ETag', function(done){426 var app = express();427428 app.use(function(req, res){429 var str = Array(1024 * 2).join('-');430 res.send(str);431 });432433 app.disable('etag');434435 request(app)436 .get('/')437 .end(function(err, res){438 res.headers.should.not.have.property('etag');439 done();440 });441 });442443 it('should send ETag when manually set', function(done){444 var app = express();445446 app.disable('etag');447448 app.use(function(req, res){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 449why, one step back
449 res.set('etag', '"asdf"');

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 450–454why, one step back
450 res.send(200);
451 });452453 request(app)454 .get('/')

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 455why, one step back
455 .expect('etag', '"asdf"', done)

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

lines 456–457why, one step back
456 });
457 });

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 458–528why, one step back
458
459 describe('when "strong"', function(){460 it('should send strong ETag', function(done){461 var app = express()462463 app.set('etag', 'strong');464465 app.use(function(req, res){466 res.send('hello, world!');467 });468469 request(app)470 .get('/')471 .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)472 })473 })474475 describe('when "weak"', function(){476 it('should send weak ETag', function(done){477 var app = express()478479 app.set('etag', 'weak');480481 app.use(function(req, res){482 res.send('hello, world!');483 });484485 request(app)486 .get('/')487 .expect('etag', 'W/"d-1486392595"', done)488 })489 })490491 describe('when a function', function(){492 it('should send custom ETag', function(done){493 var app = express()494495 app.set('etag', function(body, encoding){496 body.should.equal('hello, world!')497 encoding.should.equal('utf8')498 return '"custom"'499 });500501 app.use(function(req, res){502 res.send('hello, world!');503 });504505 request(app)506 .get('/')507 .expect('etag', '"custom"', done)508 })509510 it('should not send falsy ETag', function(done){511 var app = express()512513 app.set('etag', function(body, encoding){514 return undefined515 });516517 app.use(function(req, res){518 res.send('hello, world!');519 });520521 request(app)522 .get('/')523 .end(function(err, res){524 res.headers.should.not.have.property('etag')525 done();526 })527 })528 })

Merge pull request #1694 from kavu/add_disable_etagTJ Holowaychukbefore adoption610e172

line 529why, one step back
529 })

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

line 530why, one step back
530})