Skip to content

Commit

Permalink
feat(proxy-wasm): add host properties 'kong.service_name' and 'kong.r…
Browse files Browse the repository at this point in the history
…oute_name'
  • Loading branch information
casimiro committed Oct 31, 2024
1 parent 8190a66 commit 8a3399b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kong/runloop/wasm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,13 @@ local function register_property_handlers()
return ok, value, const
end)

properties.add_getter("kong.route_name", function(_, _, ctx)
local value = ctx.route and ctx.route.name
local ok = value ~= nil
local const = ok
return ok, value, const
end)

properties.add_getter("kong.service.response.status", function(kong)
return true, kong.service.response.get_status(), false
end)
Expand All @@ -789,6 +796,13 @@ local function register_property_handlers()
return ok, value, const
end)

properties.add_getter("kong.service_name", function(_, _, ctx)
local value = ctx.service and ctx.service.name
local ok = value ~= nil
local const = ok
return ok, value, const
end)

properties.add_getter("kong.version", function(kong)
return true, kong.version, true
end)
Expand Down
43 changes: 43 additions & 0 deletions spec/02-integration/20-wasm/04-proxy-wasm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function()
mock_service = assert(bp.services:insert {
host = helpers.mock_upstream_host,
port = helpers.mock_upstream_port,
name = "mock_service",
})

local mock_upstream = assert(bp.upstreams:insert {
Expand All @@ -50,12 +51,14 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function()
})

r_single = assert(bp.routes:insert {
name = "r_single",
paths = { "/single" },
strip_path = true,
service = mock_service,
})

local r_double = assert(bp.routes:insert {
name = "r_double",
paths = { "/double" },
strip_path = true,
service = mock_service,
Expand Down Expand Up @@ -687,6 +690,26 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function()
assert.logfile().has.no.line("[crit]", true, 0)
end)

it("read kong.route_name", function()
local client = helpers.proxy_client()
finally(function() client:close() end)

local res = assert(client:send {
method = "GET",
path = "/single/status/201",
headers = {
[HEADER_NAME_TEST] = "get_kong_property",
[HEADER_NAME_INPUT] = "route_name",
[HEADER_NAME_DISPATCH_ECHO] = "on",
}
})

local body = assert.res_status(200, res)
assert.equal(r_single.name, body)
assert.logfile().has.no.line("[error]", true, 0)
assert.logfile().has.no.line("[crit]", true, 0)
end)

it("read kong.service_id", function()
local client = helpers.proxy_client()
finally(function() client:close() end)
Expand All @@ -707,6 +730,26 @@ describe("proxy-wasm filters (#wasm) (#" .. strategy .. ")", function()
assert.logfile().has.no.line("[crit]", true, 0)
end)

it("read kong.service_name", function()
local client = helpers.proxy_client()
finally(function() client:close() end)

local res = assert(client:send {
method = "GET",
path = "/single/status/201",
headers = {
[HEADER_NAME_TEST] = "get_kong_property",
[HEADER_NAME_INPUT] = "service_name",
[HEADER_NAME_DISPATCH_ECHO] = "on",
}
})

local body = assert.res_status(200, res)
assert.equal(mock_service.name, body)
assert.logfile().has.no.line("[error]", true, 0)
assert.logfile().has.no.line("[crit]", true, 0)
end)

it("read kong.ctx.shared[<attr>]", function()
local client = helpers.proxy_client()
finally(function() client:close() end)
Expand Down

0 comments on commit 8a3399b

Please sign in to comment.