Skip to content

Commit

Permalink
feat(tests): improve http_mock (#12325)
Browse files Browse the repository at this point in the history
support add dictionaries; fix the behavior when no body is sent
  • Loading branch information
StarlightIbuki authored Jan 10, 2024
1 parent 8731e5f commit 1294513
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/helpers/http_mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function http_mock.new(listens, routes, opts)
listens = listens,
routes = routes,
directives = directives,
dicts = opts.dicts,
init = opts.init,
log_opts = log_opts,
logs = {},
Expand Down
12 changes: 9 additions & 3 deletions spec/helpers/http_mock/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ events {
http {
lua_shared_dict mock_logs $(shm_size);
# for dict, size in pairs(dicts or {}) do
lua_shared_dict $(dict) $(size);
# end
init_by_lua_block {
# if log_opts.err then
-- disable warning of global variable
Expand Down Expand Up @@ -148,18 +152,20 @@ $(init)
# if log_opts.req_body then
-- collect body
body = ngx.req.get_body_data()
# if log_opts.req_large_body then
if not body then
local file = ngx.req.get_body_file()
if file then
# if log_opts.req_large_body then
local f = io.open(file, "r")
if f then
body = f:read("*a")
f:close()
end
# else
body = { "body is too large" }
# end -- if log_opts.req_large_body
end
end
# end -- if log_opts.req_large_body
# end -- if log_opts.req_body
ngx.ctx.req = {
method = method,
Expand Down Expand Up @@ -238,4 +244,4 @@ $(init)
# end -- for location, route in pairs(routes)
}
}
]]
]]

0 comments on commit 1294513

Please sign in to comment.