-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
171 lines (151 loc) · 4.38 KB
/
test.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*!
* koa-ip-filter <https://github.com/tunnckoCore/koa-ip-filter>
*
* Copyright (c) 2015-2016 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
/* jshint asi:true, esnext:true */
'use strict'
var test = require('mukla')
var ipFilter = require('./index')
var request = require('supertest')
var helloWorld = require('koa-hello-world')
var koa = require('koa')
function middleware (fn) {
return koa().use(fn).use(helloWorld()).callback()
}
test('should yield next middleware if no `opts.filter` given', function (done) {
var server = middleware(ipFilter())
request(server)
.get('/')
.expect(200, 'Hello World')
.end(done)
})
test('should have `opts.id` and it should be binded to koa this', function (done) {
var server = middleware(ipFilter({
id: function _id_ (ctx) {
this.set('x-github-username', 'tunnckoCore')
return ctx.request.header['x-koaip']
}
}))
request(server).get('/')
.expect('x-github-username', 'tunnckoCore')
.expect(200)
.expect('Hello World')
.end(done)
})
test('should `403 Forbidden` if not match to `opts.filter`', function (done) {
var server = middleware(ipFilter({
id: function () {
return this.request.header['x-koaip']
},
filter: '1.2.3.*'
}))
request(server).get('/').set('x-koaip', '4.4.8.8')
.expect('403 Forbidden').expect(403, done)
})
test('should `403 Forbidden` if IP is in blacklist', function (done) {
request(middleware(ipFilter({
id: function () {
return this.request.header['x-koaip']
},
filter: ['*', '!89.???.30.*']
})))
.get('/').set('x-koaip', '89.111.30.8')
.expect(403, '403 Forbidden')
.end(done)
})
test('should `200 OK` if not in blacklist range', function (done) {
var mw = ipFilter({
id: function () {
var header = this.request.header['x-koaip']
return header
},
// it can be double star - globstar
// or *.*.*.*
filter: ['*.*.*.*', '!111.??.244.*']
})
request(middleware(mw))
.get('/')
.set('x-koaip', '4.4.8.8')
.expect(200, 'Hello World')
.end(done)
})
test('should support custom message for 403 Forbidden', function (done) {
var server = middleware(ipFilter({
id: function () {
return this.request.header['x-koaip']
},
filter: ['*', '!89.???.30.*'],
forbidden: '403, Get out of here!'
}))
request(server)
.get('/')
.set('x-koaip', '89.111.30.8')
.expect(403, '403, Get out of here!')
.end(done)
})
test('should be able `opts.forbidden` to be function', function (done) {
var server = middleware(ipFilter({
id: function () {
return this.request.header['x-koaip']
},
filter: '123.225.23.120',
forbidden: function (ctx) {
this.set('X-Forbidden', 'Can be function')
ctx.set('X-Seriously', 'yes')
return 'opts.forbidden can be function'
}
}))
request(server)
.get('/')
.set('x-koaip', '55.55.55.55')
.expect('X-Forbidden', 'Can be function')
.expect('X-Seriously', 'yes')
.expect(403, 'opts.forbidden can be function')
.end(done)
})
test('should have `this.filter` and `this.identifier` in next middleware', function (done) {
var ok = false
var server = koa()
.use(ipFilter({
// it can be double star - globstar
// or *.*.*.*
filter: ['**', '!213.15.*']
}))
.use(helloWorld())
.use(function * (next) {
test.ok(this.filter, 'should have `this.filter` in next')
test.ok(this.identifier, 'should have `this.identifier` in next')
test.equal(typeof this.filter, 'function', 'should have `this.filter` method')
test.equal(typeof this.identifier, 'string', 'should have `this.identifier`')
ok = true
yield next
})
request(server.callback())
.get('/')
.set('x-koaip', '7.7.7.7')
.expect(200, 'Hello World')
.end(function (err) {
test.ifError(err)
test.equal(ok, true)
done()
})
})
test('should not have `this.filter` if no `opts.filter` given', function (done) {
var ok = false
var server = koa().use(ipFilter()).use(helloWorld()).use(function * (next) {
test.ok(!this.filter, 'should not have `this.filter` in next')
test.ok(!this.identifier, 'should not have `this.identifier` in next')
ok = true
yield next
}).callback()
request(server)
.get('/')
.expect('Hello World')
.expect(200, function (err) {
test.ifError(err)
test.equal(ok, true)
done()
})
})