Skip to content

Commit 0286caf

Browse files
committed
test(routing): add missing tests cases
1 parent ca48ae1 commit 0286caf

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

spec/policy/routing/routing_operation_spec.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,46 @@ describe('RoutingOperation', function()
3131

3232
assert.is_false(operation:evaluate(context))
3333
end)
34+
35+
it('evaluates != correctly when paths differ', function()
36+
local operation = RoutingOperation.new_op_with_path('!=', '/expected')
37+
38+
local context = {
39+
request = { get_uri = function() return '/other' end }
40+
}
41+
42+
assert.is_true(operation:evaluate(context))
43+
end)
44+
45+
it('evaluates != correctly when paths match', function()
46+
local operation = RoutingOperation.new_op_with_path('!=', '/same')
47+
48+
local context = {
49+
request = { get_uri = function() return '/same' end }
50+
}
51+
52+
assert.is_false(operation:evaluate(context))
53+
end)
54+
55+
it('evaluates matches true when regex matches', function()
56+
local operation = RoutingOperation.new_op_with_path('matches', '^/api/.*')
57+
58+
local context = {
59+
request = { get_uri = function() return '/api/users' end }
60+
}
61+
62+
assert.is_true(operation:evaluate(context))
63+
end)
64+
65+
it('evaluates matches false when regex does not match', function()
66+
local operation = RoutingOperation.new_op_with_path('matches', '^/api/.*')
67+
68+
local context = {
69+
request = { get_uri = function() return '/other/path' end }
70+
}
71+
72+
assert.is_false(operation:evaluate(context))
73+
end)
3474
end)
3575

3676
describe('when the operation involves a header', function()
@@ -236,6 +276,12 @@ describe('RoutingOperation', function()
236276

237277
end)
238278

279+
it('raises an error for unsupported operators', function()
280+
assert.has_error(function()
281+
RoutingOperation.new_op_with_path('unsupported_op', '/path')
282+
end)
283+
end)
284+
239285
it('can evaluate the right operand as liquid', function()
240286
-- Stub the available context to avoid depending on ngx.var.*
241287
stub(ngx_variable, 'available_context', function(context) return context end)

spec/policy/routing/routing_spec.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,67 @@ local RoutingPolicy = require('apicast.policy.routing')
22
local UpstreamSelector = require('apicast.policy.routing.upstream_selector')
33
local Request = require('apicast.policy.routing.request')
44
local Upstream = require('apicast.upstream')
5+
local mapping_rules_matcher = require('apicast.mapping_rules_matcher')
56

67
describe('Routing policy', function()
8+
describe('.access', function()
9+
it('assigns route_upstream_usage_cleanup to the context', function()
10+
local routing = RoutingPolicy.new()
11+
local context = {}
12+
routing:access(context)
13+
14+
assert.is_function(context.route_upstream_usage_cleanup)
15+
end)
16+
17+
it('assigns the same function reference on every call', function()
18+
local routing = RoutingPolicy.new()
19+
local ctx1 = {}
20+
local ctx2 = {}
21+
routing:access(ctx1)
22+
routing:access(ctx2)
23+
24+
assert.equals(ctx1.route_upstream_usage_cleanup, ctx2.route_upstream_usage_cleanup)
25+
end)
26+
end)
27+
28+
describe('route_upstream_usage_cleanup', function()
29+
it('is a no-op when route_upstream is nil', function()
30+
local routing = RoutingPolicy.new()
31+
local context = {}
32+
routing:access(context)
33+
34+
assert.has_no_errors(function()
35+
context:route_upstream_usage_cleanup({}, {})
36+
end)
37+
end)
38+
39+
it('calls clean_usage_by_owner_id and merges usage when route_upstream exists', function()
40+
local routing = RoutingPolicy.new()
41+
local context = {}
42+
routing:access(context)
43+
44+
local mock_owner_id = 42
45+
context.route_upstream = {
46+
has_owner_id = function() return mock_owner_id end,
47+
}
48+
49+
local usage_diff = { some_metric = 1 }
50+
stub(mapping_rules_matcher, 'clean_usage_by_owner_id').returns(usage_diff)
51+
52+
local usage = { merge = function() end }
53+
stub(usage, 'merge')
54+
55+
local matched_rules = { 'rule1', 'rule2' }
56+
57+
context:route_upstream_usage_cleanup(usage, matched_rules)
58+
59+
assert.stub(mapping_rules_matcher.clean_usage_by_owner_id).was_called_with(
60+
matched_rules, mock_owner_id
61+
)
62+
assert.stub(usage.merge).was_called_with(usage, usage_diff)
63+
end)
64+
end)
65+
766
describe('.content', function()
867
describe('when there is an upstream that matches', function()
968
local upstream_that_matches = Upstream.new('http://localhost')

0 commit comments

Comments
 (0)