ingress-nginx lua plugins documentation
This commit is contained in:
parent
1eeb149b91
commit
b60e25f1db
5 changed files with 62 additions and 7 deletions
|
|
@ -1,14 +1,10 @@
|
|||
local _M = {}
|
||||
|
||||
function _M.rewrite()
|
||||
ngx.req.set_header("x-hello-world", "1")
|
||||
end
|
||||
|
||||
function _M.access()
|
||||
local ua = ngx.var.http_user_agent
|
||||
|
||||
if ua == "hello" then
|
||||
ngx.exit(403)
|
||||
ngx.req.set_header("x-hello-world", "1")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
25
rootfs/etc/nginx/lua/plugins/hello_world/test/main_test.lua
Normal file
25
rootfs/etc/nginx/lua/plugins/hello_world/test/main_test.lua
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
_G._TEST = true
|
||||
|
||||
local main = require("plugins.hello_world.main")
|
||||
|
||||
-- The unit tests are run within a timer phase in a headless Nginx process.
|
||||
-- Since `set_header` and `ngx.var.http_` API are disabled in this phase we have to stub it
|
||||
-- to avoid `API disabled in the current context` error.
|
||||
|
||||
describe("main", function()
|
||||
describe("rewrite", function()
|
||||
it("sets x-hello-world header to 1 when user agent is hello", function()
|
||||
ngx.var = { http_user_agent = "hello" }
|
||||
stub(ngx.req, "set_header")
|
||||
main.rewrite()
|
||||
assert.stub(ngx.req.set_header).was_called_with("x-hello-world", "1")
|
||||
end)
|
||||
|
||||
it("does not set x-hello-world header to 1 when user agent is not hello", function()
|
||||
ngx.var = { http_user_agent = "not-hello" }
|
||||
stub(ngx.req, "set_header")
|
||||
main.rewrite()
|
||||
assert.stub(ngx.req.set_header).was_not_called_with("x-hello-world", "1")
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue