UNPKG

koa-except

Version:

Bypass koa middleware under certain conditions

204 lines (167 loc) 5.29 kB
var Koa = require('koa') var request = require('supertest') var except = require('../index') describe('koa-except', function () { var middleware beforeEach(function () { middleware = function (ctx, next) { ctx.body = {executed: true} } middleware.except = except }) describe('with PATH exception', function () { it('should not call the middleware when one of the path match', function (done) { var app = new Koa() app.use(middleware.except({ path: ['/foo'] })) request(app.listen()) .get('/foo') .expect(404, done) }) it('should call the middleware when the path doesnt match', function (done) { var app = new Koa() app.use(middleware.except({ path: ['/foo'] })) request(app.listen()) .get('/bar') .expect(200, done) }) }) describe('with PATH (regexp) exception', function () { it('should not call the middleware when the regex match', function (done) { var app = new Koa() app.use(middleware.except({ path: ['/foo', /ar$/ig] })) request(app.listen()) .get('/bar') .expect(404, done) }) }) describe('with PATH (useOriginalUrl) exception', function () { it('should not call the middleware when one of the path match ctx.url instead of ctx.originalUrl', function (done) { var app = new Koa() app.use(function (ctx, next) { ctx.url = '/foo' return next() }) app.use(middleware.except({ path: ['/foo'], useOriginalUrl: false })) request(app.listen()) .get('/orig/foo') .expect(404, done) }) it('should call the middleware when the path doesnt match ctx.url even if path matches ctx.originalUrl', function (done) { var app = new Koa() app.use(function (ctx, next) { ctx.originalUrl = '/foo' return next() }) app.use(middleware.except({ path: ['/foo'], useOriginalUrl: false })) request(app.listen()) .get('/bar') .expect(200, done) }) }) describe('with EXT exception', function () { it('should not call the middleware when the ext match', function (done) { var app = new Koa() app.use(middleware.except({ ext: ['html', 'css'] })) request(app.listen()) .get('/index.html') .expect(404, done) }) it('should call the middleware when the ext doesnt match', function (done) { var app = new Koa() app.use(middleware.except({ ext: ['html', 'css'] })) request(app.listen()) .get('/index.js') .expect(200, done) }) }) describe('with METHOD exception', function () { it('should not call the middleware when the method match', function (done) { var app = new Koa() app.use(middleware.except({ method: ['GET', 'OPTIONS'] })) request(app.listen()) .get('/index') .expect(404, done) }) it('should call the middleware when the method doesnt match', function (done) { var app = new Koa() app.use(middleware.except({ method: ['GET', 'OPTIONS'] })) request(app.listen()) .post('/index') .expect(200, done) }) }) describe('with custom exception', function () { it('should not call the middleware when the custom rule match', function (done) { var app = new Koa() app.use(middleware.except(function (ctx) { return ctx.url === '/index' })) request(app.listen()) .get('/index') .expect(404, done) }) it('should call the middleware when the custom rule doesnt match', function (done) { var app = new Koa() app.use(middleware.except(function (ctx) { return ctx.url === '/index' })) request(app.listen()) .get('/home') .expect(200, done) }) }) describe('with array of conditionals', function () { var agent beforeEach(function (done) { var app = new Koa().use(middleware.except([{ method: 'POST' }, { path: '/home' }])) agent = request(app.listen()) done() }) it('should skip middleware when method matches', function (done) { agent .post('/index') .expect(404, done) }) it('should skip middleware when path matches', function (done) { agent .options('/home') .expect(404, done) }) it('should not skip middleware when non of the conditionals match', function (done) { agent .get('/index') .expect(200, done) }) }) describe('with \'and\' chaining', function () { var agent beforeEach(function (done) { var app = new Koa().use(middleware.except({ method: 'POST' }).and({ path: '/home' })) agent = request(app.listen()) done() }) it('should skip middleware when method matches', function (done) { agent .post('/index') .expect(404, done) }) it('should skip middleware when path matches', function (done) { agent .options('/home') .expect(404, done) }) it('should not skip middleware when non of the conditionals match', function (done) { agent .get('/index') .expect(200, done) }) }) })