fcs
2025-04-10 0163870d662dc5b7d69ceff977490ede594f7d82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var Koa     = require('koa');
var request = require('supertest');
 
var unless  = require('../index');
 
describe('koa-unless', function() {
  var middleware;
 
  beforeEach(function() {
    middleware = function(ctx, next) {
      ctx.body = {executed: true};
    };
 
    middleware.unless = unless;
  });
 
  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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless({ 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.unless(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.unless(function(ctx) {
        return ctx.url === '/index';
      }));
      request(app.listen())
        .get('/home')
        .expect(200, done);
    });
  });
});