Skip to content

Commit

Permalink
Supports simple slash in request_path
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed May 18, 2016
1 parent fa5baf2 commit 767cb8d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion kong/core/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function _M.find_api_by_request_path(uri, request_path_arr)
end

for _, item in ipairs(request_path_arr) do
local m, err = ngx.re.match(uri, "^"..item.request_path.."/")
local m, err = ngx.re.match(uri, "^"..(item.request_path == "/" and "/" or item.request_path.."/"))
if err then
ngx.log(ngx.ERR, "[resolver] error matching requested request_path: "..err)
elseif m then
Expand Down
6 changes: 2 additions & 4 deletions kong/dao/schemas/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ local function check_request_path(request_path, api_t)
end

if request_path ~= nil and request_path ~= "" then
if request_path == "/" then
return false, "cannot be an empty path: '/'"
elseif sub(request_path, 1, 1) ~= "/" then
if sub(request_path, 1, 1) ~= "/" then
return false, fmt("must be prefixed with slash: '%s'", request_path)
elseif match(request_path, "//+") then
-- Check for empty segments (/status//123)
Expand All @@ -95,7 +93,7 @@ local function check_request_path(request_path, api_t)

-- From now on, the request_path is considered valid.
-- Remove trailing slash
if sub(request_path, -1) == "/" then
if request_path ~= "/" and sub(request_path, -1) == "/" then
api_t.request_path = sub(request_path, 1, -2)
end
end
Expand Down
17 changes: 16 additions & 1 deletion spec/integration/05-proxy/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ describe("Resolver", function()
{name = "tests-trailing-slash-path4", request_path = "/test-trailing-slash4", strip_request_path = true, upstream_url = "http://www.mockbin.org/"},
{name = "tests-deep-path", request_path = "/hello/world", strip_request_path = true, upstream_url = "http://mockbin.com"},
{name = "tests-deep-path-two", request_path = "/hello/world/wot", strip_request_path = true, upstream_url = "http://httpbin.org"},
{name = "tests-request_path-resolver2", upstream_url = "http://mockbin.com", request_path = "/headers"}
{name = "tests-request_path-resolver2", upstream_url = "http://mockbin.com", request_path = "/headers"},
{name = "tests-root-path", upstream_url = "http://httpbin.org", request_path = "/"},
{name = "tests-root-path2", upstream_url = "http://mockbin.com", request_path = "/noroot", strip_request_path = true},
},
plugin = {
{name = "key-auth", config = {key_names = {"apikey"} }, __api = 2}
Expand Down Expand Up @@ -337,4 +339,17 @@ describe("Resolver", function()
assert.equal("http://mockbin-uri.com/request?foo=abc%7cdef%2c%20world", cjson.decode(response).url)
end)
end)

describe("Priority in request_path resolutions", function()
it("should work for root request_path", function()
local response, status = http_client.get(PROXY_URL.."/get", {})
assert.equal(200, status)
assert.equal("http://httpbin.org/get", cjson.decode(response).url)
end)
it("should work for non root request_path", function()
local response, status = http_client.get(PROXY_URL.."/noroot/request", {})
assert.equal(200, status)
assert.equal("http://mockbin.com/request", cjson.decode(response).url)
end)
end)
end)
6 changes: 3 additions & 3 deletions spec/unit/11-entities_schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ describe("Entities Schemas", function()
assert.equal("must be prefixed with slash: '"..v.."'", errors.request_path)
end
end)
it("should not accept root (no prefix slash)", function()
it("should accept root (prefix slash)", function()
local valid, errors = validate_entity({
name = "mockbin",
request_path = "/",
upstream_url = "http://mockbin.com"
}, api_schema)
assert.False(valid)
assert.equal("cannot be an empty path: '/'", errors.request_path)
assert.falsy(errors)
assert.True(valid)
end)
it("should not accept invalid URI", function()
local invalids = {"//status", "/status//123", "/status/123//"}
Expand Down

0 comments on commit 767cb8d

Please sign in to comment.