tests: add more etag tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 766b3ae · Browse files · 5 files·+62 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+62 added−40 removed
- tests:+62 added−40 removed
Provenance
- Commit
3e35845- Tree
2e5f78b- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
766b3ae
5 files changed+62 added−40 removed
modified:test/req.fresh.js+6 added-5 removed
4 unmodified lines5678910111112131415161717181920212223242525262728293031313232333435364 unmodified linesdescribe('req', function(){ describe('.fresh', function(){ it('should return true when the resource is not modified', function(done){ var app = express(); var etag = '"12345"';
app.use(function(req, res){ res.set('ETag', '12345'); res.set('ETag', etag); res.send(req.fresh); });
request(app) .get('/') .set('If-None-Match', '12345') .set('If-None-Match', etag) .expect(304, done); })
it('should return false when the resource is modified', function(done){ var app = express();
app.use(function(req, res){ res.set('ETag', '123'); res.set('ETag', '"123"'); res.send(req.fresh); });
request(app) .get('/') .set('If-None-Match', '12345') .expect('false', done); .set('If-None-Match', '"12345"') .expect(200, 'false', done); }) })})modified:test/req.stale.js+6 added-5 removed
4 unmodified lines5678910111112131415161717181920212223242525262728293031313232333435364 unmodified linesdescribe('req', function(){ describe('.stale', function(){ it('should return false when the resource is not modified', function(done){ var app = express(); var etag = '"12345"';
app.use(function(req, res){ res.set('ETag', '12345'); res.set('ETag', etag); res.send(req.stale); });
request(app) .get('/') .set('If-None-Match', '12345') .set('If-None-Match', etag) .expect(304, done); })
it('should return true when the resource is modified', function(done){ var app = express();
app.use(function(req, res){ res.set('ETag', '123'); res.set('ETag', '"123"'); res.send(req.stale); });
request(app) .get('/') .set('If-None-Match', '12345') .expect('true', done); .set('If-None-Match', '"12345"') .expect(200, 'true', done); }) })})modified:test/res.send.js+30 added-15 removed
285 unmodified lines286 })287 288 it('should always check regardless of length', function(done){289 var app = express();290+ var etag = '"asdf"';291 292 app.use(function(req, res, next){292- res.set('ETag', 'asdf');293+ res.set('ETag', etag);294 res.send('hey');295 });296 297 request(app)298 .get('/')298- .set('If-None-Match', 'asdf')299+ .set('If-None-Match', etag)300 .expect(304, done);301 })302 303 it('should respond with 304 Not Modified when fresh', function(done){11 unmodified lines315 })316 317 it('should not perform freshness check unless 2xx or 304', function(done){318 var app = express();319+ var etag = '"asdf"';320 321 app.use(function(req, res, next){322 res.status(500);321- res.set('ETag', 'asdf');323+ res.set('ETag', etag);324 res.send('hey');325 });326 327 request(app)328 .get('/')327- .set('If-None-Match', 'asdf')329+ .set('If-None-Match', etag)330 .expect('hey')331 .expect(500, done);332 })333 10 unmodified lines344 })345 346 describe('"etag" setting', function(){347 describe('when enabled', function(){346- it('should send ETag even when content-length < 1024', function(done){348+ it('should send ETag', function(done){349 var app = express();350 351 app.use(function(req, res){352 res.send('kajdslfkasdf');353 });354 355+ app.enable('etag');356+357 request(app)358 .get('/')355- .end(function(err, res){356- res.headers.should.have.property('etag');357- done();358- });359+ .expect('etag', 'W/"c-1525560792"', done)360 })361 361- it('should send ETag', function(done){362+ it('should send ETag for long response', function(done){363 var app = express();364 365 app.use(function(req, res){366 var str = Array(1024 * 2).join('-');367 res.send(str);368 });369 370+ app.enable('etag');371+372 request(app)373 .get('/')374 .expect('etag', 'W/"7ff-2796319984"', done)375 });376+377+ it('should not override ETag when manually set', function(done){378+ var app = express();379+380+ app.use(function(req, res){381+ res.set('etag', '"asdf"');382+ res.send(200);383+ });384+385+ app.enable('etag');386+387+ request(app)388+ .get('/')389+ .expect('etag', '"asdf"', done)390+ });391 });392 393 describe('when disabled', function(){394 it('should send no ETag', function(done){19 unmodified lines414 415 app.disable('etag');416 417 app.use(function(req, res){400- res.set('etag', 1);418+ res.set('etag', '"asdf"');419 res.send(200);420 });421 422 request(app)423 .get('/')406- .end(function(err, res){407- res.headers.should.have.property('etag');408- done();409- });424+ .expect('etag', '"asdf"', done)425 });426 });427 })428 })modified:test/res.set.js+7 added-6 removed
18 unmodified lines19202122232423242526272811 unmodified lines4041424344454445464748495015 unmodified lines666768696970707172737418 unmodified lines })
it('should coerce to a string', function(){ res.headers = {}; res.set('ETag', 123); res.get('ETag').should.equal('123'); res.set('X-Number', 123); res.get('X-Number').should.equal('123'); }) })
describe('.set(field, values)', function(){11 unmodified lines })
it('should coerce to an array of strings', function(){ res.headers = {}; res.set('ETag', [123, 456]); JSON.stringify(res.get('ETag')).should.equal('["123","456"]'); res.set('X-Numbers', [123, 456]); JSON.stringify(res.get('X-Numbers')) .should.equal('["123","456"]'); }) })
describe('.set(object)', function(){15 unmodified lines })
it('should coerce to a string', function(){ res.headers = {}; res.set({ ETag: 123 }); res.get('ETag').should.equal('123'); res.set({ 'X-Number': 123 }); res.get('X-Number').should.equal('123'); }) })})modified:test/utils.js+13 added-9 removed
1 2 var utils = require('../lib/utils')3 , assert = require('assert');4 5-describe('utils.etag(body)', function(){6-7- var str = 'Hello CRC';8- var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';9-5+describe('utils.etag(body, encoding)', function(){6 it('should support strings', function(){11- utils.etag(str).should.eql('W/"9-2260508953"');7+ utils.etag('express!')8+ .should.eql('W/"8-3098196679"')9 })10 11 it('should support utf8 strings', function(){15- utils.etag(strUTF8, 'utf8').should.eql('W/"4d-1395090196"');12+ utils.etag('express❤', 'utf8')13+ .should.eql('W/"a-1751845617"')14 })15 16 it('should support buffer', function(){19- utils.etag(new Buffer(strUTF8, 'utf8')).should.eql('W/"4d-1395090196"');20- utils.etag(new Buffer(str)).should.eql('W/"9-2260508953"');17+ var buf = new Buffer('express!')18+ utils.etag(buf)19+ .should.eql('W/"8-3098196679"');20 })21 22+ it('should support empty string', function(){23+ var buf = new Buffer('express!')24+ utils.etag('')25+ .should.eql('W/"0-0"');26+ })27 })28 29 describe('utils.isAbsolute()', function(){30 it('should support windows', function(){201 unmodified lines