-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathproxy_spec.lua
284 lines (227 loc) · 9.02 KB
/
proxy_spec.lua
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
local http_ng_response = require('resty.http_ng.response')
local lrucache = require('resty.lrucache')
local cjson = require('cjson')
local configuration_store = require 'apicast.configuration_store'
local Service = require 'apicast.configuration.service'
local Usage = require 'apicast.usage'
local test_backend_client = require 'resty.http_ng.backend.test'
local errors = require 'apicast.errors'
describe('Proxy', function()
local configuration, proxy, test_backend
before_each(function()
configuration = configuration_store.new()
proxy = require('apicast.proxy').new(configuration)
test_backend = test_backend_client.new()
proxy.http_ng_backend = test_backend
end)
it('has access function', function()
assert.truthy(proxy.access)
assert.same('function', type(proxy.access))
end)
describe(':rewrite', function()
local service
before_each(function()
-- Replace original ngx.header. Openresty does not allow to modify it when
-- running busted tests.
ngx.header = {}
ngx.var = { backend_endpoint = 'http://localhost:1853', uri = '/a/uri' }
stub(ngx.req, 'get_method', function () return 'GET' end)
service = Service.new({ extract_usage = function() end })
end)
it('works with part of the credentials', function()
service.credentials = { location = 'headers' }
service.backend_version = 2
ngx.var.http_app_key = 'key'
local context = {}
assert.falsy(proxy:rewrite(service, context))
end)
end)
it('has post_action function', function()
assert.truthy(proxy.post_action)
assert.same('function', type(proxy.post_action))
end)
describe('.get_upstream', function()
local get_upstream
before_each(function() get_upstream = proxy.get_upstream end)
it("on invalid api_backend return error", function()
local upstream, err = get_upstream({ api_backend = 'test.com' })
assert.falsy(upstream)
assert.same(err, "invalid upstream")
end)
it("on no api_backend return nil and no error", function()
local upstream, err = get_upstream({})
assert.falsy(upstream)
assert.falsy(err)
end)
it("on no api_backend return empty string and no error", function()
local upstream, err = get_upstream({api_backend = ''})
assert.falsy(upstream)
assert.falsy(err)
end)
it("on no api_backend return null and no error", function()
local upstream, err = get_upstream({api_backend = cjson.null})
assert.falsy(upstream)
assert.falsy(err)
end)
end)
describe('.authorize', function()
local service = { backend_authentication = { value = 'not_baz' }, backend = { endpoint = 'http://0.0.0.0' } }
local context
before_each(function()
context = {
cache_handler = function() end,
publish_backend_auth = function() end
}
end)
it('takes ttl value if sent', function()
local ttl = 80
ngx.var = { cached_key = 'client_id=blah', http_x_3scale_debug='baz', real_url='blah' }
local response = { status = 200 }
stub(test_backend, 'send', function() return response end)
stub(proxy, 'cache_handler').returns(true)
local usage = Usage.new()
usage:add('foo', 0)
proxy:authorize(context, service, usage, { client_id = 'blah' }, ttl)
assert.spy(proxy.cache_handler).was.called_with(
proxy.cache, 'client_id=blah:usage%5Bfoo%5D=0', response, ttl)
end)
it('works with no ttl', function()
ngx.var = { cached_key = "client_id=blah", http_x_3scale_debug='baz', real_url='blah' }
local response = { status = 200 }
stub(test_backend, 'send', function() return response end)
stub(proxy, 'cache_handler').returns(true)
local usage = Usage.new()
usage:add('foo', 0)
proxy:authorize(context, service, usage, { client_id = 'blah' })
assert.spy(proxy.cache_handler).was.called_with(
proxy.cache, 'client_id=blah:usage%5Bfoo%5D=0', response, nil)
end)
it('does not use cached auth if creds are the same but extra authrep params are not', function()
proxy.extra_params_backend_authrep = { referrer = '3scale.net' }
stub(test_backend, 'send', function() return { status = 200 } end)
local usage = Usage.new()
usage:add('hits', 1)
local cache_key = "uk:usage%5Bhits%5D=1" -- Referrer not here
proxy.cache:set(cache_key, 200)
ngx.var = { cached_key = "uk" } -- authorize() expects creds to be set up
proxy:authorize(context, service, usage, { user_key = 'uk' })
-- Calls backend because the call is not cached
assert.stub(test_backend.send).was_called()
end)
it('uses cached auth if creds are the same and authrep params too', function()
proxy.extra_params_backend_authrep = { referrer = '3scale.net' }
stub(test_backend, 'send', function() return { status = 200 } end)
local usage = Usage.new()
usage:add('hits', 1)
local cache_key = "uk:usage%5Bhits%5D=1:referrer=3scale.net" -- Referrer here
proxy.cache:set(cache_key, 200)
ngx.var = { cached_key = "uk" } -- authorize() expects creds to be set up
proxy:authorize(context, service, usage, { user_key = 'uk' })
-- Does not call backend because the call is cached
assert.stub(test_backend.send).was_not_called()
end)
it('returns "limits exceeded" with the "Retry-After" given by the 3scale backend', function()
ngx.header = {}
ngx.var = { cached_key = "uk" } -- authorize() expects creds to be set up
stub(errors, 'limits_exceeded')
local retry_after = 60
local usage = Usage.new()
usage:add('hits', 1)
test_backend.expect({}).respond_with(
{
status = 409,
headers = {
['3scale-limit-reset'] = retry_after,
['3scale-rejection-reason'] = 'limits_exceeded'
}
}
)
proxy:authorize(context, service, usage, { user_key = 'uk' })
assert.stub(errors.limits_exceeded).was_called_with(service, retry_after)
end)
end)
describe('.handle_backend_response', function()
local context
before_each(function()
context = {
cache_handler = function() end,
publish_backend_auth = function() end
}
end)
it('returns a rejection reason when given', function()
local authorized, rejection_reason = proxy:handle_backend_response(
context,
lrucache.new(1),
http_ng_response.new(nil, 403, { ['3scale-rejection-reason'] = 'some_reason' }, ''),
nil)
assert.falsy(authorized)
assert.equal('some_reason', rejection_reason)
end)
it('returns an empty rejection reason instead of "limits exceeded" for disabled metrics', function()
local authorized, rejection_reason = proxy:handle_backend_response(
context,
lrucache.new(1),
http_ng_response.new(
nil,
409,
{
['3scale-rejection-reason'] = 'limits_exceeded',
['3scale-limit-max-value'] = 0,
},
''
),
nil
)
assert.falsy(authorized)
assert.is_nil(rejection_reason)
end)
it('returns limits exceeded for enabled metrics', function()
local authorized, rejection_reason = proxy:handle_backend_response(
context,
lrucache.new(1),
http_ng_response.new(
nil,
409,
{
['3scale-rejection-reason'] = 'limits_exceeded',
['3scale-limit-max-value'] = 100,
},
''
),
nil
)
assert.falsy(authorized)
assert.equal('limits_exceeded', rejection_reason)
end)
describe('when backend is unavailable', function()
local backend_unavailable_statuses = { 0, 499, 502 } -- Not exhaustive
local cache_key = 'a_cache_key'
before_each(function()
-- So we can set the value for the cached auth ensuring that the
-- handler will not modify it.
proxy.cache_handler = function() end
end)
it('returns true when the cached authorization is authorized', function()
proxy.cache:set(cache_key, 200)
for _, status in ipairs(backend_unavailable_statuses) do
local authorized = proxy:handle_backend_response(context, cache_key, { status = status })
assert(authorized)
end
end)
it('returns false when the authorization is not cached', function()
proxy.cache:delete(cache_key)
for _, status in ipairs(backend_unavailable_statuses) do
local authorized = proxy:handle_backend_response(context, cache_key, { status = status })
assert.falsy(authorized)
end
end)
it('returns false when the authorization is cached and denied', function()
proxy.cache:set(cache_key, 429)
for _, status in ipairs(backend_unavailable_statuses) do
local authorized = proxy:handle_backend_response(context, cache_key, { status = status })
assert.falsy(authorized)
end
end)
end)
end)
end)