Skip to content

Commit

Permalink
fix(resolver): sort request_path_arr by descending specificity
Browse files Browse the repository at this point in the history
Since request_path_arr is ordered by descending specificity, the first match will always be the most specific and correct one.
  • Loading branch information
Sidhartha Chatterjee authored and subnetmarco committed Apr 14, 2016
1 parent 1f34841 commit c3e0abf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions kong/core/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local constants = require "kong.constants"
local responses = require "kong.tools.responses"

local table_insert = table.insert
local table_sort = table.sort
local string_match = string.match
local string_find = string.find
local string_format = string.format
Expand Down Expand Up @@ -93,6 +94,11 @@ function _M.load_apis_in_memory()
end
end

-- Sort request_path_arr by descending specificity.
table_sort(request_path_arr, function (first, second)
return first.request_path > second.request_path
end)

return {
by_dns = dns_dic,
request_path_arr = request_path_arr, -- all APIs with a request_path
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/core/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe("Resolver", function()
assert.truthy(item.request_path)
assert.truthy(item.api)
end
assert.equal("/mockbin", apis_dics.request_path_arr[1].strip_request_path_pattern)
assert.equal("/mockbin%-with%-dashes", apis_dics.request_path_arr[2].strip_request_path_pattern)
assert.equal("/strip%-me", apis_dics.request_path_arr[1].strip_request_path_pattern)
assert.equal("/strip", apis_dics.request_path_arr[2].strip_request_path_pattern)
end)
it("should return an array of APIs with wildcard request_host", function()
assert.equal("table", type(apis_dics.wildcard_dns_arr))
Expand All @@ -68,13 +68,13 @@ describe("Resolver", function()
end)
describe("strip_request_path()", function()
it("should strip the api's request_path from the requested URI", function()
assert.equal("/status/200", resolver.strip_request_path("/mockbin/status/200", apis_dics.request_path_arr[1].strip_request_path_pattern))
assert.equal("/status/200", resolver.strip_request_path("/mockbin-with-dashes/status/200", apis_dics.request_path_arr[2].strip_request_path_pattern))
assert.equal("/", resolver.strip_request_path("/mockbin", apis_dics.request_path_arr[1].strip_request_path_pattern))
assert.equal("/", resolver.strip_request_path("/mockbin/", apis_dics.request_path_arr[1].strip_request_path_pattern))
assert.equal("/status/200", resolver.strip_request_path("/mockbin/status/200", apis_dics.request_path_arr[7].strip_request_path_pattern))
assert.equal("/status/200", resolver.strip_request_path("/mockbin-with-dashes/status/200", apis_dics.request_path_arr[6].strip_request_path_pattern))
assert.equal("/", resolver.strip_request_path("/mockbin", apis_dics.request_path_arr[7].strip_request_path_pattern))
assert.equal("/", resolver.strip_request_path("/mockbin/", apis_dics.request_path_arr[7].strip_request_path_pattern))
end)
it("should only strip the first pattern", function()
assert.equal("/mockbin/status/200/mockbin", resolver.strip_request_path("/mockbin/mockbin/status/200/mockbin", apis_dics.request_path_arr[1].strip_request_path_pattern))
assert.equal("/mockbin/status/200/mockbin", resolver.strip_request_path("/mockbin/mockbin/status/200/mockbin", apis_dics.request_path_arr[7].strip_request_path_pattern))
end)
it("should not add final slash", function()
assert.equal("hello", resolver.strip_request_path("hello", apis_dics.request_path_arr[3].strip_request_path_pattern, true))
Expand Down

0 comments on commit c3e0abf

Please sign in to comment.