Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router) respect strip_uri for APIs with multiple uris #2582

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kong/core/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ function _M.new(apis)

do
local api_t_from_cache = cache:get(cache_key)
if api_t_from_cache then
if api_t_from_cache and match_api(api_t_from_cache, method, uri, host)
then
return api_t_from_cache
end
end
Expand Down
14 changes: 12 additions & 2 deletions spec/01-unit/11-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,18 @@ describe("Router", function()
assert.equal("/", _ngx.var.request_uri)

_ngx = mock_ngx("GET", "/this-api", {})
local api2 = router.exec(_ngx)
assert.same(use_case_apis[1], api2)
api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.request_uri)

_ngx = mock_ngx("GET", "/my-api", {})
api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.request_uri)

_ngx = mock_ngx("GET", "/this-api", {})
api = router.exec(_ngx)
assert.same(use_case_apis[1], api)
assert.equal("/", _ngx.var.request_uri)
end)

Expand Down
60 changes: 60 additions & 0 deletions spec/02-integration/05-proxy/01-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,66 @@ describe("Router", function()
end)
end)

describe("strip_uri", function()

setup(function()
insert_apis {
{
name = "api-strip-uri",
upstream_url = "http://httpbin.org",
uris = { "/x/y/z", "/z/y/x" },
strip_uri = true,
},
}

assert(helpers.start_kong())
end)

teardown(function()
helpers.stop_kong()
end)

describe(" = true", function()
it("strips subsequent calls to an API with different [uris]", function()
local res_uri_1 = assert(client:send {
method = "GET",
path = "/x/y/z/get",
})

local body = assert.res_status(200, res_uri_1)
local json = cjson.decode(body)
assert.matches("httpbin.org/get", json.url, nil, true)

local res_uri_2 = assert(client:send {
method = "GET",
path = "/z/y/x/get",
})

body = assert.res_status(200, res_uri_2)
json = cjson.decode(body)
assert.matches("httpbin.org/get", json.url, nil, true)

local res_2_uri_1 = assert(client:send {
method = "GET",
path = "/x/y/z/get",
})

body = assert.res_status(200, res_2_uri_1)
json = cjson.decode(body)
assert.matches("httpbin.org/get", json.url, nil, true)

local res_2_uri_2 = assert(client:send {
method = "GET",
path = "/x/y/z/get",
})

body = assert.res_status(200, res_2_uri_2)
json = cjson.decode(body)
assert.matches("httpbin.org/get", json.url, nil, true)
end)
end)
end)

describe("preserve_host", function()

setup(function()
Expand Down