diff --git a/kong/runloop/handler.lua b/kong/runloop/handler.lua index 8b2d82eea9e..196d0e4f97e 100644 --- a/kong/runloop/handler.lua +++ b/kong/runloop/handler.lua @@ -110,10 +110,15 @@ do sort(routes, function(r1, r2) r1, r2 = r1.route, r2.route - if r1.regex_priority == r2.regex_priority then + + local rp1 = r1.regex_priority or 0 + local rp2 = r2.regex_priority or 0 + + if rp1 == rp2 then return r1.created_at < r2.created_at end - return r1.regex_priority > r2.regex_priority + + return rp1 > rp2 end) local err diff --git a/spec/02-integration/05-proxy/02-router_spec.lua b/spec/02-integration/05-proxy/02-router_spec.lua index 0ea525f64bf..7eac039b055 100644 --- a/spec/02-integration/05-proxy/02-router_spec.lua +++ b/spec/02-integration/05-proxy/02-router_spec.lua @@ -49,6 +49,10 @@ local function insert_routes(routes) end local function remove_routes(routes) + if not routes then + return + end + local services = {} for _, route in ipairs(routes) do @@ -1093,7 +1097,34 @@ for _, strategy in helpers.each_strategy() do end end) + describe("router rebuilds", function() + local routes + + lazy_teardown(function() + remove_routes(routes) + end) + it("when Routes have 'regex_priority = nil'", function() + -- Regression test for issue: + -- https://github.com/Kong/kong/issues/4254 + routes = insert_routes { + { + methods = { "GET" }, + regex_priority = 1, + }, + { + methods = { "POST", "PUT" }, + regex_priority = ngx.null, + }, + } + + local res = assert(proxy_client:send { + method = "GET", + }) + + assert.response(res).has_status(200) + end) + end) end) end) end