Skip to content
Myr

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

10 files+81 added−127 removed

modified:test/acceptance/cookies.js+7 added-12 removed
modified:test/acceptance/downloads.js+2 added-5 removed
added:test/support/utils.js+24 added
modified:test/middleware.basic.js+2 added-7 removed
modified:test/res.jsonp.js+3 added-5 removed
12var express = require('../')3 , request = require('supertest')4 , assert = require('assert');5var utils = require('./support/utils');67describe('res', function(){8 describe('.jsonp(object)', function(){9 it('should respond with jsonp', function(done){
126 unmodified lines
136137 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 })143144 it('should override previous Content-Types with callback', function(done){145 var app = express();
200 unmodified lines
modified:test/res.location.js+2 added-4 removed
modified:test/res.redirect.js+9 added-20 removed
12var http = require('http');3var express = require('..');4var request = require('supertest');5var utils = require('./support/utils');67describe('res', function(){8 describe('.redirect(url)', function(){9 it('should default to a 302 redirect', function(done){
19 unmodified lines
29 });3031 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 })3738 describe('.redirect(url, status)', function(){
5 unmodified lines
44 });4546 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 })5253 describe('when the request method is HEAD', function(){
5 unmodified lines
59 });6061 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 })6768 describe('when accepting html', function(){
104 unmodified lines
173 .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 line
2var assert = require('assert');3var express = require('..');4var methods = require('methods');5var request = require('supertest');6var utils = require('./support/utils');78describe('res', function(){9 describe('.send()', function(){10 it('should set body to ""', function(done){
107 unmodified lines
118 });119120 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 })125126 it('should set ETag', function (done) {127 var app = express();
58 unmodified lines
186 });187188 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 })193194 it('should set ETag', function (done) {195 var app = express();
17 unmodified lines
213 });214215 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 })221222 describe('.send(Object)', function(){
34 unmodified lines
257 });258259 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 })267268 describe('when .statusCode is 304', function(){
5 unmodified lines
274 });275276 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 })284285 it('should always check regardless of length', function(done){
147 unmodified lines
433 app.enable('etag');434435 request(app)436 .get('/')454 .expect(shouldNotHaveHeader('ETag'))437 .expect(utils.shouldNotHaveHeader('ETag'))438 .expect(200, done);439 })440 });441
9 unmodified lines
451 app.disable('etag');452453 request(app)454 .get('/')472 .expect(shouldNotHaveHeader('ETag'))455 .expect(utils.shouldNotHaveHeader('ETag'))456 .expect(200, done);457 });458459 it('should send ETag when manually set', function (done) {
81 unmodified lines
541 });542543 request(app)544 .get('/')562 .expect(shouldNotHaveHeader('ETag'))545 .expect(utils.shouldNotHaveHeader('ETag'))546 .expect(200, done);547 })548 })549 })550})568569function 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 lines
6var onFinished = require('on-finished');7var path = require('path');8var should = require('should');9var fixtures = path.join(__dirname, 'fixtures');10var utils = require('./support/utils');1112describe('res', function(){13 describe('.sendFile(path)', function () {14 it('should error missing path', function (done) {
139 unmodified lines
154 var app = createApp(path.resolve(__dirname, 'fixtures/does-not-exist'), { headers: headers });155156 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 });162163 describe('with "root" option', function () {
342 unmodified lines
506 });507508 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 })513514 it('should transfer a file', function (done) {515 var app = express();
73 unmodified lines
589 });590591 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 })597598 describe('with a relative path', function(){
5 unmodified lines
604 });605606 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 })611612 it('should serve relative to "root"', function(done){613 var app = express();
3 unmodified lines
617 });618619 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 })624625 it('should consider ../ malicious when "root" is not set', function(done){626 var app = express();
87 unmodified lines
modified:test/res.vary.js+3 added-8 removed
12var assert = require('assert');3var express = require('..');4var request = require('supertest');5var utils = require('./support/utils');67describe('res.vary()', function(){8 describe('with no arguments', function(){9 it('should not set Vary', function (done) {
5 unmodified lines
15 });1617 request(app)18 .get('/')18 .expect(shouldNotHaveHeader('Vary'))19 .expect(utils.shouldNotHaveHeader('Vary'))20 .expect(200, done);21 })22 })23
7 unmodified lines
31 });3233 request(app)34 .get('/')34 .expect(shouldNotHaveHeader('Vary'))35 .expect(utils.shouldNotHaveHeader('Vary'))36 .expect(200, done);37 })38 })39
48 unmodified lines
88 .expect(200, done);89 })90 })91})9192function shouldNotHaveHeader(header) {93 return function (res) {94 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);95 };96}