diff --git a/spec/02-integration/01-helpers/03-http_mock_spec.lua b/spec/02-integration/01-helpers/03-http_mock_spec.lua index b1250c1febc..6ffb0770cde 100644 --- a/spec/02-integration/01-helpers/03-http_mock_spec.lua +++ b/spec/02-integration/01-helpers/03-http_mock_spec.lua @@ -27,7 +27,7 @@ for _, tls in ipairs {true, false} do resp_body = true } })) - + assert(mock:start()) end) @@ -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) diff --git a/spec/helpers/http_mock.lua b/spec/helpers/http_mock.lua index 5319a06d577..91fc85c6121 100644 --- a/spec/helpers/http_mock.lua +++ b/spec/helpers/http_mock.lua @@ -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 @@ -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, diff --git a/spec/helpers/http_mock/template.lua b/spec/helpers/http_mock/template.lua index 093bc0d334d..510cfad8c8c 100644 --- a/spec/helpers/http_mock/template.lua +++ b/spec/helpers/http_mock/template.lua @@ -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) @@ -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 @@ -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; @@ -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