test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var Koa = require('koa');
  2. var request = require('supertest');
  3. var unless = require('../index');
  4. describe('koa-unless', function() {
  5. var middleware;
  6. beforeEach(function() {
  7. middleware = function(ctx, next) {
  8. ctx.body = {executed: true};
  9. };
  10. middleware.unless = unless;
  11. });
  12. describe('with PATH exception', function() {
  13. it('should not call the middleware when one of the path match', function(done) {
  14. var app = new Koa();
  15. app.use(middleware.unless({ path: ['/foo'] }));
  16. request(app.listen())
  17. .get('/foo')
  18. .expect(404, done);
  19. });
  20. it('should call the middleware when the path doesnt match', function(done) {
  21. var app = new Koa();
  22. app.use(middleware.unless({ path: ['/foo'] }));
  23. request(app.listen())
  24. .get('/bar')
  25. .expect(200, done);
  26. });
  27. });
  28. describe('with PATH (regexp) exception', function() {
  29. it('should not call the middleware when the regex match', function(done) {
  30. var app = new Koa();
  31. app.use(middleware.unless({ path: ['/foo', /ar$/ig] }));
  32. request(app.listen())
  33. .get('/bar')
  34. .expect(404, done);
  35. });
  36. });
  37. describe('with PATH (useOriginalUrl) exception', function() {
  38. it('should not call the middleware when one of the path match ctx.url instead of ctx.originalUrl', function(done) {
  39. var app = new Koa();
  40. app.use(function(ctx, next) {
  41. ctx.url = '/foo';
  42. return next();
  43. });
  44. app.use(middleware.unless({ path: ['/foo'], useOriginalUrl: false }));
  45. request(app.listen())
  46. .get('/orig/foo')
  47. .expect(404, done);
  48. });
  49. it('should call the middleware when the path doesnt match ctx.url even if path matches ctx.originalUrl', function(done) {
  50. var app = new Koa();
  51. app.use(function(ctx, next) {
  52. ctx.originalUrl = '/foo';
  53. return next();
  54. });
  55. app.use(middleware.unless({ path: ['/foo'], useOriginalUrl: false }));
  56. request(app.listen())
  57. .get('/bar')
  58. .expect(200, done);
  59. });
  60. });
  61. describe('with EXT exception', function() {
  62. it('should not call the middleware when the ext match', function(done) {
  63. var app = new Koa();
  64. app.use(middleware.unless({ ext: ['html', 'css'] }));
  65. request(app.listen())
  66. .get('/index.html')
  67. .expect(404, done);
  68. });
  69. it('should call the middleware when the ext doesnt match', function(done) {
  70. var app = new Koa();
  71. app.use(middleware.unless({ ext: ['html', 'css'] }));
  72. request(app.listen())
  73. .get('/index.js')
  74. .expect(200, done);
  75. });
  76. });
  77. describe('with METHOD exception', function() {
  78. it('should not call the middleware when the method match', function(done) {
  79. var app = new Koa();
  80. app.use(middleware.unless({ method: ['GET', 'OPTIONS'] }));
  81. request(app.listen())
  82. .get('/index')
  83. .expect(404, done);
  84. });
  85. it('should call the middleware when the method doesnt match', function(done) {
  86. var app = new Koa();
  87. app.use(middleware.unless({ method: ['GET', 'OPTIONS'] }));
  88. request(app.listen())
  89. .post('/index')
  90. .expect(200, done);
  91. });
  92. });
  93. describe('with custom exception', function() {
  94. it('should not call the middleware when the custom rule match', function(done) {
  95. var app = new Koa();
  96. app.use(middleware.unless(function(ctx) {
  97. return ctx.url === '/index';
  98. }));
  99. request(app.listen())
  100. .get('/index')
  101. .expect(404, done);
  102. });
  103. it('should call the middleware when the custom rule doesnt match', function(done) {
  104. var app = new Koa();
  105. app.use(middleware.unless(function(ctx) {
  106. return ctx.url === '/index';
  107. }));
  108. request(app.listen())
  109. .get('/home')
  110. .expect(200, done);
  111. });
  112. });
  113. });