Skip to content

Commit

Permalink
tests(*): http mock support specifying customized init_by_lua_block c…
Browse files Browse the repository at this point in the history
…ode (#11331)

This commit has two changes:
- Let http mock support specifying customized init_by_lua_block code
- Fix the global patched assert function behavior to keep it same as the
  original one.
  • Loading branch information
windmgc authored Aug 1, 2023
1 parent 444ec83 commit 47df07d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
28 changes: 27 additions & 1 deletion spec/02-integration/01-helpers/03-http_mock_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ for _, tls in ipairs {true, false} do
resp_body = true
}
}))

assert(mock:start())
end)

Expand Down Expand Up @@ -220,6 +220,32 @@ describe("http_mock config", function()

assert(pl_file.access_time(pid_filename) ~= nil, "mocking not in the correct place")
end)

it("init_by_lua_block inject", function ()
local mock = assert(http_mock.new(nil, {
["/test"] = {
access = [[
ngx.print(test_value)
]],
},
}, {
init = [[
-- Test that the mock is injected
test_value = "hello world"
]]
}))
mock:start()
finally(function()
assert(mock:stop())
end)

local client = mock:get_client()
local res = assert(client:send({
path = "/test"
}))
assert.response(res).has.status(200)
assert.same(res:read_body(), "hello world")
end)
end)

local function remove_volatile_headers(req_t)
Expand Down
2 changes: 2 additions & 0 deletions spec/helpers/http_mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ end
-- @tparam[opt=false] bool opts.log_opts.collect_resp whether to log responses
-- @tparam[opt=false] bool opts.log_opts.collect_resp_body whether to log response bodies
-- @tparam[opt=true] bool opts.log_opts.collect_err: whether to log errors
-- @tparam[opt] string opts.init: the lua code injected into the init_by_lua_block
-- @treturn http_mock a mock instance
-- @treturn string the port the mock server listens to
-- @usage
Expand Down Expand Up @@ -160,6 +161,7 @@ function http_mock.new(listens, routes, opts)
listens = listens,
routes = routes,
directives = directives,
init = opts.init,
log_opts = log_opts,
logs = {},
tls = opts.tls,
Expand Down
19 changes: 9 additions & 10 deletions spec/helpers/http_mock/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ events {
http {
lua_shared_dict mock_logs $(shm_size);
# if log_opts.err then
init_by_lua_block {
# if log_opts.err then
-- disable warning of global variable
local g_meta = getmetatable(_G)
setmetatable(_G, nil)
Expand All @@ -41,16 +41,12 @@ http {
table.insert(err_t, {err, debug.traceback("", 3)})
end
function assert(truthy, err) -- luacheck: ignore
if truthy then
return truthy
end
if ngx.ctx then
function assert(truthy, err, ...) -- luacheck: ignore
if not truthy and ngx.ctx then
insert_err(err)
end
return original_assert(truthy, err)
return original_assert(truthy, err, ...)
end
original_error = error -- luacheck: ignore
Expand All @@ -66,9 +62,12 @@ http {
err_patched = true -- luacheck: ignore
setmetatable(_G, g_meta)
# end
# if init then
$(init)
# end
}
# end
server {
listen 0.0.0.0:$(debug.port);
server_name mock_debug;
Expand Down Expand Up @@ -142,7 +141,7 @@ http {
local method = ngx.req.get_method()
local uri = ngx.var.request_uri
local headers = ngx.req.get_headers(nil, true)
ngx.req.read_body()
local body
Expand Down

1 comment on commit 47df07d

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:47df07dad16b04e24141890e82b4f37813febe35
Artifacts available https://github.com/Kong/kong/actions/runs/5724697400

Please sign in to comment.