Skip to content

Commit

Permalink
tests: use after to verify callback count
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 26, 2018
1 parent bccff4d commit 61f1e81
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"vary": "^1"
},
"devDependencies": {
"after": "0.8.2",
"eslint": "2.13.1",
"express": "4.16.2",
"mocha": "3.5.3",
Expand Down
54 changes: 32 additions & 22 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict';

var after = require('after')
var assert = require('assert')
var cors = require('..')

Expand Down Expand Up @@ -66,17 +67,18 @@

it('shortcircuits preflight requests', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
res.end = function () {
// assert
assert.equal(res.statusCode, 204)
done();
cb()
};
next = function () {
// assert
done('should not be called');
cb(new Error('should not be called'))
};

// act
Expand All @@ -85,17 +87,18 @@

it('can configure preflight success response status code', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
res.end = function () {
// assert
assert.equal(res.statusCode, 200)
done();
cb()
};
next = function () {
// assert
done('should not be called');
cb(new Error('should not be called'))
};

// act
Expand All @@ -104,16 +107,17 @@

it('doesn\'t shortcircuit preflight requests with preflightContinue option', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
res.end = function () {
// assert
done('should not be called');
cb(new Error('should not be called'))
};
next = function () {
// assert
done();
cb()
};

// act
Expand All @@ -122,17 +126,18 @@

it('normalizes method names', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('options');
res = fakeResponse();
res.end = function () {
// assert
assert.equal(res.statusCode, 204)
done();
cb()
};
next = function () {
// assert
done('should not be called');
cb(new Error('should not be called'))
};

// act
Expand All @@ -141,17 +146,18 @@

it('includes Content-Length response header', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
res.end = function () {
// assert
assert.equal(res.getHeader('Content-Length'), '0')
done();
cb()
};
next = function () {
// assert
done('should not be called');
cb(new Error('should not be called'))
};

// act
Expand All @@ -176,6 +182,7 @@

it('OPTION call with no options enables default CORS to all origins and methods', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
Expand All @@ -184,11 +191,11 @@
assert.equal(res.statusCode, 204)
assert.equal(res.getHeader('Access-Control-Allow-Origin'), '*')
assert.equal(res.getHeader('Access-Control-Allow-Methods'), 'GET,HEAD,PUT,PATCH,POST,DELETE')
done();
cb()
};
next = function () {
// assert
done();
cb(new Error('should not be called'))
};

// act
Expand All @@ -198,6 +205,7 @@
describe('passing static options', function () {
it('overrides defaults', function (done) {
// arrange
var cb = after(1, done)
var req, res, next, options;
options = {
origin: 'example.com',
Expand All @@ -211,16 +219,16 @@
res.end = function () {
// assert
assert.equal(res.statusCode, 204)
done();
};
next = function () {
// assert
assert.equal(res.getHeader('Access-Control-Allow-Origin'), 'example.com')
assert.equal(res.getHeader('Access-Control-Allow-Methods'), 'FOO,bar')
assert.equal(res.getHeader('Access-Control-Allow-Headers'), 'FIZZ,buzz')
assert.equal(res.getHeader('Access-Control-Allow-Credentials'), 'true')
assert.equal(res.getHeader('Access-Control-Max-Age'), '123')
done();
cb()
};
next = function () {
// assert
cb(new Error('should not be called'))
};

// act
Expand Down Expand Up @@ -454,6 +462,7 @@

it('can override methods', function (done) {
// arrange
var cb = after(1, done)
var req, res, next, options;
options = {
methods: ['method1', 'method2']
Expand All @@ -463,12 +472,12 @@
res.end = function () {
// assert
assert.equal(res.statusCode, 204)
done();
assert.equal(res.getHeader('Access-Control-Allow-Methods'), 'method1,method2')
cb()
};
next = function () {
// assert
assert.equal(res.getHeader('Access-Control-Allow-Methods'), 'method1,method2')
done();
cb(new Error('should not be called'))
};

// act
Expand All @@ -477,18 +486,19 @@

it('methods defaults to GET, HEAD, PUT, PATCH, POST, DELETE', function (done) {
// arrange
var cb = after(1, done)
var req, res, next;
req = fakeRequest('OPTIONS');
res = fakeResponse();
res.end = function () {
// assert
assert.equal(res.statusCode, 204)
assert.equal(res.getHeader('Access-Control-Allow-Methods'), 'GET,HEAD,PUT,PATCH,POST,DELETE')
done();
cb()
};
next = function () {
// assert
done();
cb(new Error('should not be called'))
};

// act
Expand Down

0 comments on commit 61f1e81

Please sign in to comment.