Skip to content

Commit

Permalink
fix(router) ensure reducers respect matching priorities
Browse files Browse the repository at this point in the history
This patch ensures that when reducing categories, we always consider
higher-priority categories first.
  • Loading branch information
thibaultcha committed Jul 22, 2019
1 parent c9c4a6a commit 6a03e1b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion kong/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ local MATCH_RULES = {
DST = 0x00000001,
}

local SORTED_MATCH_RULES = {}

for _, v in pairs(MATCH_RULES) do
insert(SORTED_MATCH_RULES, v)
end

sort(SORTED_MATCH_RULES, function(a, b)
return a > b
end)

local MATCH_SUBRULES = {
HAS_REGEX_URI = 0x01,
PLAIN_HOSTS_ONLY = 0x02,
Expand Down Expand Up @@ -851,7 +861,7 @@ do

local reducers_set = {}

for _, bit_match_rule in pairs(MATCH_RULES) do
for _, bit_match_rule in ipairs(SORTED_MATCH_RULES) do
if band(bit_category, bit_match_rule) ~= 0 then
reducers_set[#reducers_set + 1] = reducers[bit_match_rule]
end
Expand Down
48 changes: 48 additions & 0 deletions spec/01-unit/08-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,54 @@ describe("Router", function()
assert.truthy(route_t)
assert.equal(use_case[2].route, route_t.route)
end)

it("more [headers] has priority over longer [paths]", function()
local use_case = {
{
service = service,
route = {
headers = {
version = { "v1" },
},
paths = { "/my-route/hello" },
},
},
{
service = service,
route = {
headers = {
version = { "v1" },
location = { "us-east" },
},
paths = { "/my-route" },
},
},
}

local router = assert(Router.new(use_case))

local match_t = router.select("GET", "/my-route/hello", "domain.org",
nil, nil, nil, nil, nil, {
version = "v1",
location = "us-east",
})
assert.truthy(match_t)
assert.equal(use_case[2].route, match_t.route)
assert.same("/my-route", match_t.matches.uri)
assert.same({ version = "v1", location = "us-east" },
match_t.matches.headers)

local match_t = router.select("GET", "/my-route/hello/world",
"domain.org", nil, nil, nil, nil, nil, {
version = "v1",
location = "us-east",
})
assert.truthy(match_t)
assert.equal(use_case[2].route, match_t.route)
assert.same("/my-route", match_t.matches.uri)
assert.same({ version = "v1", location = "us-east" },
match_t.matches.headers)
end)
end)

describe("misses", function()
Expand Down

0 comments on commit 6a03e1b

Please sign in to comment.