tests: use supertest to check response header values
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2983 closes #2992
authored by Douglas Christopher Wilson · committed by Linus Unnebäck · parent 8931b23 · Browse files · 10 files·+81 added−127 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+81 added−127 removed
- tests:+81 added−127 removed
Provenance
- Commit
12bc16e- Tree
47842d7- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Linus Unnebäck <linus@folkdatorn.se>- Parent
8931b23
10 files changed+81 added−127 removed
modified:test/acceptance/cookies.js+7 added-12 removed
123456784 unmodified lines131415161617181917181920212232 unmodified lines55565758606162635960616263646566707172736768697071
var app = require('../../examples/cookies') , request = require('supertest');var utils = require('../support/utils');
describe('cookies', function(){ describe('GET /', function(){ it('should have a form', function(done){4 unmodified lines
it('should respond with no cookies', function(done){ request(app) .get('/') .end(function(err, res){ res.headers.should.not.have.property('set-cookie') done() }) .expect(utils.shouldNotHaveHeader('Set-Cookie')) .expect(200, done) })
it('should respond to cookie', function(done){ request(app)32 unmodified lines request(app) .post('/') .type('urlencoded') .send({ remember: 1 }) .expect(302, function(err, res){ res.headers.should.have.property('set-cookie') done() }) .expect('Set-Cookie', /remember=1/) .expect(302, done) })
it('should no set cookie w/o reminder', function(done){ request(app) .post('/') .send({}) .expect(302, function(err, res){ res.headers.should.not.have.property('set-cookie') done() }) .expect(utils.shouldNotHaveHeader('Set-Cookie')) .expect(302, done) }) })})modified:test/acceptance/downloads.js+2 added-5 removed
13 unmodified lines1415161718192021221819202122237 unmodified lines13 unmodified lines describe('GET /files/amazing.txt', function(){ it('should have a download header', function(done){ request(app) .get('/files/amazing.txt') .end(function(err, res){ res.status.should.equal(200); res.headers.should.have.property('content-disposition', 'attachment; filename="amazing.txt"') done() }) .expect('Content-Disposition', 'attachment; filename="amazing.txt"') .expect(200, done) }) })
describe('GET /files/missing.txt', function(){7 unmodified linesadded:test/support/utils.js+24 added
123456789101112131415161718192021222324
/** * Module dependencies. * @private */var assert = require('assert');
/** * Module exports. * @public */exports.shouldNotHaveHeader = shouldNotHaveHeader;
/** * Assert that a supertest response does not have a header. * * @param {string} header Header name to check * @returns {function} */function shouldNotHaveHeader(header) { return function (res) { assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header); };}modified:test/middleware.basic.js+2 added-7 removed
30 unmodified lines3132333435363738394041353637383930 unmodified lines request(app.listen()) .get('/') .set('Content-Type', 'application/json') .send('{"foo":"bar"}') .end(function(err, res){ if (err) return done(err); res.headers.should.have.property('content-type', 'application/json'); res.statusCode.should.equal(200); res.text.should.equal('{"foo":"bar"}'); done(); }) .expect('Content-Type', 'application/json') .expect(200, '{"foo":"bar"}', done) }) })})modified:test/res.jsonp.js+3 added-5 removed
1 2 var express = require('../')3 , request = require('supertest')4 , assert = require('assert');5+var utils = require('./support/utils');6 7 describe('res', function(){8 describe('.jsonp(object)', function(){9 it('should respond with jsonp', function(done){126 unmodified lines136 137 request(app)138 .get('/')139 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')139- .expect(200, '{"hello":"world"}', function (err, res) {140- if (err) return done(err);141- res.headers.should.not.have.property('x-content-type-options');142- done();143- });140+ .expect(utils.shouldNotHaveHeader('X-Content-Type-Options'))141+ .expect(200, '{"hello":"world"}', done);142 })143 144 it('should override previous Content-Types with callback', function(done){145 var app = express();200 unmodified linesmodified:test/res.location.js+2 added-4 removed
11 unmodified lines1213141516171819161718192011 unmodified lines });
request(app) .get('/') .end(function(err, res){ res.headers.should.have.property('location', 'http://google.com'); done(); }) .expect('Location', 'http://google.com') .expect(200, done) }) })})modified:test/res.redirect.js+9 added-20 removed
1 2 var http = require('http');3 var express = require('..');4 var request = require('supertest');5+var utils = require('./support/utils');6 7 describe('res', function(){8 describe('.redirect(url)', function(){9 it('should default to a 302 redirect', function(done){19 unmodified lines29 });30 31 request(app)32 .get('/')32- .end(function(err, res){33- res.statusCode.should.equal(303);34- res.headers.should.have.property('location', 'http://google.com');35- done();36- })33+ .expect('Location', 'http://google.com')34+ .expect(303, done)35 })36 })37 38 describe('.redirect(url, status)', function(){5 unmodified lines44 });45 46 request(app)47 .get('/')50- .end(function(err, res){51- res.statusCode.should.equal(303);52- res.headers.should.have.property('location', 'http://google.com');53- done();54- })48+ .expect('Location', 'http://google.com')49+ .expect(303, done)50 })51 })52 53 describe('when the request method is HEAD', function(){5 unmodified lines59 });60 61 request(app)62 .head('/')68- .end(function(err, res){69- res.headers.should.have.property('location', 'http://google.com');70- res.text.should.equal('');71- done();72- })63+ .expect('Location', 'http://google.com')64+ .expect(302, '', done)65 })66 })67 68 describe('when accepting html', function(){104 unmodified lines173 .get('/')174 .set('Accept', 'application/octet-stream')175 .expect('location', 'http://google.com')176 .expect('content-length', '0')185- .expect(302, '', function(err, res){186- if (err) return done(err)187- res.headers.should.not.have.property('content-type');188- done();189- })177+ .expect(utils.shouldNotHaveHeader('Content-Type'))178+ .expect(302, '', done)179 })180 })181 })modified:test/res.send.js+18 added-41 removed
1 unmodified line2 var assert = require('assert');3 var express = require('..');4 var methods = require('methods');5 var request = require('supertest');6+var utils = require('./support/utils');7 8 describe('res', function(){9 describe('.send()', function(){10 it('should set body to ""', function(done){107 unmodified lines118 });119 120 request(app)121 .get('/')121- .end(function(err, res){122- res.headers.should.have.property('content-type', 'text/html; charset=utf-8');123- res.text.should.equal('<p>hey</p>');124- res.statusCode.should.equal(200);125- done();126- })122+ .expect('Content-Type', 'text/html; charset=utf-8')123+ .expect(200, '<p>hey</p>', done);124 })125 126 it('should set ETag', function (done) {127 var app = express();58 unmodified lines186 });187 188 request(app)189 .get('/')193- .end(function(err, res){194- res.headers.should.have.property('content-type', 'application/octet-stream');195- res.text.should.equal('hello');196- res.statusCode.should.equal(200);197- done();198- })190+ .expect('Content-Type', 'application/octet-stream')191+ .expect(200, 'hello', done);192 })193 194 it('should set ETag', function (done) {195 var app = express();17 unmodified lines213 });214 215 request(app)216 .get('/')224- .end(function(err, res){225- res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');226- res.text.should.equal('hey');227- res.statusCode.should.equal(200);228- done();229- })217+ .expect('Content-Type', 'text/plain; charset=utf-8')218+ .expect(200, 'hey', done);219 })220 })221 222 describe('.send(Object)', function(){34 unmodified lines257 });258 259 request(app)260 .get('/')272- .end(function(err, res){273- res.headers.should.not.have.property('content-type');274- res.headers.should.not.have.property('content-length');275- res.headers.should.not.have.property('transfer-encoding');276- res.text.should.equal('');277- done();278- })261+ .expect(utils.shouldNotHaveHeader('Content-Type'))262+ .expect(utils.shouldNotHaveHeader('Content-Length'))263+ .expect(utils.shouldNotHaveHeader('Transfer-Encoding'))264+ .expect(204, '', done);265 })266 })267 268 describe('when .statusCode is 304', function(){5 unmodified lines274 });275 276 request(app)277 .get('/')292- .end(function(err, res){293- res.headers.should.not.have.property('content-type');294- res.headers.should.not.have.property('content-length');295- res.headers.should.not.have.property('transfer-encoding');296- res.text.should.equal('');297- done();298- })278+ .expect(utils.shouldNotHaveHeader('Content-Type'))279+ .expect(utils.shouldNotHaveHeader('Content-Length'))280+ .expect(utils.shouldNotHaveHeader('Transfer-Encoding'))281+ .expect(304, '', done);282 })283 })284 285 it('should always check regardless of length', function(done){147 unmodified lines433 app.enable('etag');434 435 request(app)436 .get('/')454- .expect(shouldNotHaveHeader('ETag'))437+ .expect(utils.shouldNotHaveHeader('ETag'))438 .expect(200, done);439 })440 });441 9 unmodified lines451 app.disable('etag');452 453 request(app)454 .get('/')472- .expect(shouldNotHaveHeader('ETag'))455+ .expect(utils.shouldNotHaveHeader('ETag'))456 .expect(200, done);457 });458 459 it('should send ETag when manually set', function (done) {81 unmodified lines541 });542 543 request(app)544 .get('/')562- .expect(shouldNotHaveHeader('ETag'))545+ .expect(utils.shouldNotHaveHeader('ETag'))546 .expect(200, done);547 })548 })549 })550 })568-569-function shouldNotHaveHeader(header) {570- return function (res) {571- assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)572- }573-}modified:test/res.sendFile.js+11 added-25 removed
5 unmodified lines6 var onFinished = require('on-finished');7 var path = require('path');8 var should = require('should');9 var fixtures = path.join(__dirname, 'fixtures');10+var utils = require('./support/utils');11 12 describe('res', function(){13 describe('.sendFile(path)', function () {14 it('should error missing path', function (done) {139 unmodified lines154 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });155 156 request(app)157 .get('/')157- .expect(404, function (err, res) {158- if (err) return done(err);159- res.headers.should.not.have.property('x-success');160- done();161- });158+ .expect(utils.shouldNotHaveHeader('X-Success'))159+ .expect(404, done);160 });161 });162 163 describe('with "root" option', function () {342 unmodified lines506 });507 508 request(app)509 .get('/')512- .expect(404, function (err, res) {513- if (err) return done(err);514- res.headers.should.not.have.property('x-success');515- done();516- });510+ .expect(utils.shouldNotHaveHeader('X-Success'))511+ .expect(404, done);512 })513 514 it('should transfer a file', function (done) {515 var app = express();73 unmodified lines589 });590 591 request(app)592 .get('/')598- .end(function(err, res){599- res.text.should.equal('<p>{{user.name}}</p>');600- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');601- done();602- });593+ .expect('Content-Type', 'text/html; charset=UTF-8')594+ .expect(200, '<p>{{user.name}}</p>', done);595 })596 })597 598 describe('with a relative path', function(){5 unmodified lines604 });605 606 request(app)607 .get('/')616- .end(function(err, res){617- res.text.should.equal('<p>{{user.name}}</p>');618- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');619- done();620- });608+ .expect('Content-Type', 'text/html; charset=UTF-8')609+ .expect(200, '<p>{{user.name}}</p>', done);610 })611 612 it('should serve relative to "root"', function(done){613 var app = express();3 unmodified lines617 });618 619 request(app)620 .get('/')632- .end(function(err, res){633- res.text.should.equal('<p>{{user.name}}</p>');634- res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');635- done();636- });621+ .expect('Content-Type', 'text/html; charset=UTF-8')622+ .expect(200, '<p>{{user.name}}</p>', done);623 })624 625 it('should consider ../ malicious when "root" is not set', function(done){626 var app = express();87 unmodified linesmodified:test/res.vary.js+3 added-8 removed
1 2 var assert = require('assert');3 var express = require('..');4 var request = require('supertest');5+var utils = require('./support/utils');6 7 describe('res.vary()', function(){8 describe('with no arguments', function(){9 it('should not set Vary', function (done) {5 unmodified lines15 });16 17 request(app)18 .get('/')18- .expect(shouldNotHaveHeader('Vary'))19+ .expect(utils.shouldNotHaveHeader('Vary'))20 .expect(200, done);21 })22 })23 7 unmodified lines31 });32 33 request(app)34 .get('/')34- .expect(shouldNotHaveHeader('Vary'))35+ .expect(utils.shouldNotHaveHeader('Vary'))36 .expect(200, done);37 })38 })39 48 unmodified lines88 .expect(200, done);89 })90 })91 })91-92-function shouldNotHaveHeader(header) {93- return function (res) {94- assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);95- };96-}