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

feat(plugin) Adding path prefix support in request transformer #1426

Closed
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
25 changes: 25 additions & 0 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,35 @@ local function transform_body(conf)
end
end

local function transform_path(conf)
local uri = ngx.var.request_uri

-- Remove path_prefix(es)
if #conf.remove.path_prefix > 0 then
for _, name, value in iter(conf.remove.path_prefix) do
if (uri:sub(0, value:len()) == value) then
uri = uri:sub(value:len() + 1)
end
end
end

-- Add (prepend) path_prefix(es)
if #conf.add.path_prefix > 0 then
for _, name, value in iter(conf.remove.path_prefix) do
uri = value .. uri
end
end

-- Update the URI after our changes
ngx.req.set_uri(uri)

end

function _M.execute(conf)
transform_body(conf)
transform_headers(conf)
transform_querystrings(conf)
transform_path(conf)
end

return _M
6 changes: 4 additions & 2 deletions kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ return {
fields = {
body = {type = "array", default = {}}, -- does not need colons
headers = {type = "array", default = {}}, -- does not need colons
querystring = {type = "array", default = {}} -- does not need colons
querystring = {type = "array", default = {}}, -- does not need colons
path_prefix = {type = "array", default = {}} -- does not need colons
}
}
},
Expand All @@ -38,7 +39,8 @@ return {
fields = {
body = {type = "array", default = {}, func = check_for_value},
headers = {type = "array", default = {}, func = check_for_value},
querystring = {type = "array", default = {}, func = check_for_value}
querystring = {type = "array", default = {}, func = check_for_value},
path_prefix = {type = "array", default = {}, func = check_for_value}
}
}
},
Expand Down
41 changes: 41 additions & 0 deletions spec/03-plugins/request-transformer/01-access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Plugin: request-transformer (access)", function()
local client
Expand All @@ -10,6 +11,8 @@ describe("Plugin: request-transformer (access)", function()
local api4 = assert(helpers.dao.apis:insert {request_host = "test4.com", upstream_url = "http://mockbin.com"})
local api5 = assert(helpers.dao.apis:insert {request_host = "test5.com", upstream_url = "http://mockbin.com"})
local api6 = assert(helpers.dao.apis:insert {request_host = "test6.com", upstream_url = "http://mockbin.com"})
local api7 = assert(helpers.dao.apis:insert {request_host = "test7.com", upstream_url = "http://mockbin.com"})
local api8 = assert(helpers.dao.apis:insert {request_host = "test8.com", upstream_url = "http://mockbin.com"})

-- plugin config 1
assert(helpers.dao.plugins:insert {
Expand Down Expand Up @@ -93,6 +96,26 @@ describe("Plugin: request-transformer (access)", function()
}
}
})
-- plugin config 7
assert(helpers.dao.plugins:insert {
api_id = api7.id,
name = "request-transformer",
config = {
remove = {
path_prefix = {"/example"}
}
}
})
-- plugin config 8
assert(helpers.dao.plugins:insert {
api_id = api8.id,
name = "request-transformer",
config = {
add = {
path_prefix = {"/another"}
}
}
})

helpers.prepare_prefix()
assert(helpers.start_kong())
Expand Down Expand Up @@ -446,6 +469,15 @@ describe("Plugin: request-transformer (access)", function()
local value = assert.request(r).has.queryparam("q2")
assert.equals("v2", value)
end)
it("removes a matched path prefix", function()
local r = assert(client:send {
method = "GET",
path = "/example/page",
})
local body = assert.res_status(200, r)
local json = cjson.decode(body)
assert.equal("http://mockbin.com/page", json.url)
end)
end)

describe("add", function()
Expand Down Expand Up @@ -646,6 +678,15 @@ describe("Plugin: request-transformer (access)", function()
local value = assert.has.header("host", json)
assert.equals("httpbin.org", value)
end)
it("adds a path prefix", function()
local r = assert(client:send {
method = "GET",
path = "/page",
})
local body = assert.res_status(200, r)
local json = cjson.decode(body)
assert.equal("http://mockbin.com/another/page", json.url)
end)
end)

describe("append ", function()
Expand Down