Skip to content

Commit

Permalink
tests: use supertest to check response header values
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 10, 2016
1 parent bb84cf9 commit f940b7c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 127 deletions.
19 changes: 7 additions & 12 deletions test/acceptance/cookies.js
@@ -1,6 +1,7 @@

var app = require('../../examples/cookies')
, request = require('supertest');
var utils = require('../support/utils');

describe('cookies', function(){
describe('GET /', function(){
Expand All @@ -13,10 +14,8 @@ describe('cookies', function(){
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){
Expand Down Expand Up @@ -57,20 +56,16 @@ describe('cookies', function(){
.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)
})
})
})
7 changes: 2 additions & 5 deletions test/acceptance/downloads.js
Expand Up @@ -15,11 +15,8 @@ describe('downloads', 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)
})
})

Expand Down
9 changes: 2 additions & 7 deletions test/middleware.basic.js
Expand Up @@ -32,13 +32,8 @@ describe('middleware', function(){
.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)
})
})
})
8 changes: 3 additions & 5 deletions test/res.jsonp.js
Expand Up @@ -2,6 +2,7 @@
var express = require('../')
, request = require('supertest')
, assert = require('assert');
var utils = require('./support/utils');

describe('res', function(){
describe('.jsonp(object)', function(){
Expand Down Expand Up @@ -136,11 +137,8 @@ describe('res', function(){
request(app)
.get('/')
.expect('Content-Type', 'application/vnd.example+json; charset=utf-8')
.expect(200, '{"hello":"world"}', function (err, res) {
if (err) return done(err);
res.headers.should.not.have.property('x-content-type-options');
done();
});
.expect(utils.shouldNotHaveHeader('X-Content-Type-Options'))
.expect(200, '{"hello":"world"}', done);
})

it('should override previous Content-Types with callback', function(done){
Expand Down
6 changes: 2 additions & 4 deletions test/res.location.js
Expand Up @@ -13,10 +13,8 @@ describe('res', function(){

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)
})
})
})
29 changes: 9 additions & 20 deletions test/res.redirect.js
Expand Up @@ -2,6 +2,7 @@
var http = require('http');
var express = require('..');
var request = require('supertest');
var utils = require('./support/utils');

describe('res', function(){
describe('.redirect(url)', function(){
Expand Down Expand Up @@ -29,11 +30,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.statusCode.should.equal(303);
res.headers.should.have.property('location', 'http://google.com');
done();
})
.expect('Location', 'http://google.com')
.expect(303, done)
})
})

Expand All @@ -47,11 +45,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.statusCode.should.equal(303);
res.headers.should.have.property('location', 'http://google.com');
done();
})
.expect('Location', 'http://google.com')
.expect(303, done)
})
})

Expand All @@ -65,11 +60,8 @@ describe('res', function(){

request(app)
.head('/')
.end(function(err, res){
res.headers.should.have.property('location', 'http://google.com');
res.text.should.equal('');
done();
})
.expect('Location', 'http://google.com')
.expect(302, '', done)
})
})

Expand Down Expand Up @@ -182,11 +174,8 @@ describe('res', function(){
.set('Accept', 'application/octet-stream')
.expect('location', 'http://google.com')
.expect('content-length', '0')
.expect(302, '', function(err, res){
if (err) return done(err)
res.headers.should.not.have.property('content-type');
done();
})
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(302, '', done)
})
})
})
59 changes: 18 additions & 41 deletions test/res.send.js
Expand Up @@ -3,6 +3,7 @@ var assert = require('assert');
var express = require('..');
var methods = require('methods');
var request = require('supertest');
var utils = require('./support/utils');

describe('res', function(){
describe('.send()', function(){
Expand Down Expand Up @@ -118,12 +119,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
res.text.should.equal('<p>hey</p>');
res.statusCode.should.equal(200);
done();
})
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, '<p>hey</p>', done);
})

it('should set ETag', function (done) {
Expand Down Expand Up @@ -190,12 +187,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('content-type', 'application/octet-stream');
res.text.should.equal('hello');
res.statusCode.should.equal(200);
done();
})
.expect('Content-Type', 'application/octet-stream')
.expect(200, 'hello', done);
})

it('should set ETag', function (done) {
Expand All @@ -221,12 +214,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');
res.text.should.equal('hey');
res.statusCode.should.equal(200);
done();
})
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, 'hey', done);
})
})

Expand Down Expand Up @@ -269,13 +258,10 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.headers.should.not.have.property('content-type');
res.headers.should.not.have.property('content-length');
res.headers.should.not.have.property('transfer-encoding');
res.text.should.equal('');
done();
})
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect(204, '', done);
})
})

Expand All @@ -289,13 +275,10 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.headers.should.not.have.property('content-type');
res.headers.should.not.have.property('content-length');
res.headers.should.not.have.property('transfer-encoding');
res.text.should.equal('');
done();
})
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect(304, '', done);
})
})

Expand Down Expand Up @@ -451,7 +434,7 @@ describe('res', function(){

request(app)
.get('/')
.expect(shouldNotHaveHeader('ETag'))
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
})
});
Expand All @@ -469,7 +452,7 @@ describe('res', function(){

request(app)
.get('/')
.expect(shouldNotHaveHeader('ETag'))
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
});

Expand Down Expand Up @@ -559,15 +542,9 @@ describe('res', function(){

request(app)
.get('/')
.expect(shouldNotHaveHeader('ETag'))
.expect(utils.shouldNotHaveHeader('ETag'))
.expect(200, done);
})
})
})
})

function shouldNotHaveHeader(header) {
return function (res) {
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
}
}
36 changes: 11 additions & 25 deletions test/res.sendFile.js
Expand Up @@ -7,6 +7,7 @@ var onFinished = require('on-finished');
var path = require('path');
var should = require('should');
var fixtures = path.join(__dirname, 'fixtures');
var utils = require('./support/utils');

describe('res', function(){
describe('.sendFile(path)', function () {
Expand Down Expand Up @@ -154,11 +155,8 @@ describe('res', function(){

request(app)
.get('/')
.expect(404, function (err, res) {
if (err) return done(err);
res.headers.should.not.have.property('x-success');
done();
});
.expect(utils.shouldNotHaveHeader('X-Success'))
.expect(404, done);
});
});

Expand Down Expand Up @@ -509,11 +507,8 @@ describe('res', function(){

request(app)
.get('/')
.expect(404, function (err, res) {
if (err) return done(err);
res.headers.should.not.have.property('x-success');
done();
});
.expect(utils.shouldNotHaveHeader('X-Success'))
.expect(404, done);
})

it('should transfer a file', function (done) {
Expand Down Expand Up @@ -595,11 +590,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.text.should.equal('<p>{{user.name}}</p>');
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
done();
});
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect(200, '<p>{{user.name}}</p>', done);
})
})

Expand All @@ -613,11 +605,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.text.should.equal('<p>{{user.name}}</p>');
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
done();
});
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect(200, '<p>{{user.name}}</p>', done);
})

it('should serve relative to "root"', function(done){
Expand All @@ -629,11 +618,8 @@ describe('res', function(){

request(app)
.get('/')
.end(function(err, res){
res.text.should.equal('<p>{{user.name}}</p>');
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
done();
});
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect(200, '<p>{{user.name}}</p>', done);
})

it('should consider ../ malicious when "root" is not set', function(done){
Expand Down

0 comments on commit f940b7c

Please sign in to comment.