Support the combination of nginx variables and text value for annotation upstream-hash-by.

This commit is contained in:
agile6v 2020-05-19 11:51:30 +08:00
parent b18ea267a8
commit c035a144f8
6 changed files with 95 additions and 22 deletions

View file

@ -1,4 +1,6 @@
local original_ngx = ngx
local util = require("util")
local function reset_ngx()
_G.ngx = original_ngx
end
@ -9,28 +11,43 @@ local function mock_ngx(mock)
_G.ngx = _ngx
end
describe("lua_ngx_var", function()
local util = require("util")
describe("utility", function()
after_each(function()
reset_ngx()
end)
describe("lua_ngx_var", function()
describe("ngx_complex_value", function()
before_each(function()
mock_ngx({ var = { remote_addr = "192.168.1.1", [1] = "nginx/regexp/1/group/capturing" } })
end)
local ngx_complex_value = function(data)
local ret, err = util.parse_complex_value(data)
if err ~= nil then
return ""
end
return util.generate_var_value(ret)
end
it("returns value of nginx var by key", function()
assert.equal("192.168.1.1", util.lua_ngx_var("$remote_addr"))
assert.equal("192.168.1.1", ngx_complex_value("$remote_addr"))
end)
it("returns value of nginx var when key is number", function()
assert.equal("nginx/regexp/1/group/capturing", util.lua_ngx_var("$1"))
assert.equal("nginx/regexp/1/group/capturing", ngx_complex_value("$1"))
end)
it("returns nil when variable is not defined", function()
assert.equal(nil, util.lua_ngx_var("$foo_bar"))
it("returns value of nginx var by multiple variables", function()
assert.equal("192.168.1.1nginx/regexp/1/group/capturing", ngx_complex_value("$remote_addr$1"))
end)
it("returns value by the combination of variable and text value", function()
assert.equal("192.168.1.1-text-value", ngx_complex_value("${remote_addr}-text-value"))
end)
it("returns empty when variable is not defined", function()
assert.equal("", ngx_complex_value("$foo_bar"))
end)
end)