Skip to content

Commit

Permalink
more error response testing for expressjs#26
Browse files Browse the repository at this point in the history
  • Loading branch information
troygoode committed Jul 9, 2014
1 parent ae72c86 commit f975892
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
24 changes: 11 additions & 13 deletions test/basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@
/* -------------------------------------------------------------------------- */

describe('basic auth', function () {
describe('', function () {
it('POST works', function (done) {
supertest(app)
.post('/')
.auth('username', 'password')
.expect(200)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
res.text.should.eql('hello world');
done();
});
});
it('POST works', function (done) {
supertest(app)
.post('/')
.auth('username', 'password')
.expect(200)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
res.text.should.eql('hello world');
done();
});
});
});

Expand Down
70 changes: 57 additions & 13 deletions test/error-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,71 @@

app = express();
app.use(cors());

/*jslint unparam: true*/ // `req` is part of the signature, but not used in these routes
app.post('/', function (req, res, next) {
app.post('/five-hundred', function (req, res, next) {
next(new Error('nope'));
});
/*jslint unparam: false*/

/*jslint unparam: true*/ // `req` is part of the signature, but not used in these routes
app.post('/four-oh-one', function (req, res, next) {
next(new Error('401'));
});
/*jslint unparam: false*/

/*jslint unparam: true*/ // `req` is part of the signature, but not used in these routes
app.post('/four-oh-four', function (req, res, next) {
next();
});
/*jslint unparam: false*/

/*jslint unparam: true*/ // `req` is part of the signature, but not used in these routes
app.use(function (err, req, res, next) {
if (err.message === '401') {
res.send(401, 'unauthorized');
} else {
next(err);
}
});
/*jslint unparam: false*/

/* -------------------------------------------------------------------------- */

describe('error response', function () {
describe('', function () {
it('POST works', function (done) {
supertest(app)
.post('/')
.expect(500)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
res.text.should.startWith('Error: nope');
done();
});
});
it('500', function (done) {
supertest(app)
.post('/five-hundred')
.expect(500)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
res.text.should.startWith('Error: nope');
done();
});
});

it('401', function (done) {
supertest(app)
.post('/four-oh-one')
.expect(401)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
res.text.should.eql('unauthorized');
done();
});
});

it('404', function (done) {
supertest(app)
.post('/four-oh-four')
.expect(404)
.end(function (err, res) {
should.not.exist(err);
res.headers['access-control-allow-origin'].should.eql('*');
done();
});
});
});

Expand Down

0 comments on commit f975892

Please sign in to comment.