fix: correct the validation of route.vars (#3124)

Previously only the type is applied, and it is too strict.
And now we should allow 4 items in the vars expression.

Fix #3123

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
This commit is contained in:
罗泽轩 2020-12-27 12:02:31 +08:00 committed by GitHub
parent e5c1c5984a
commit 53777519b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 168 additions and 9 deletions

View File

@ -443,15 +443,9 @@ _M.route = {
items = {
description = "Nginx builtin variable name and value",
type = "array",
items = {
maxItems = 3,
maxItems = 4,
minItems = 2,
anyOf = {
{type = "string",},
{type = "number",},
}
}
}
},
},
filter_func = {
type = "string",

View File

@ -155,3 +155,168 @@ User-Agent: ios
hello world
--- no_error_log
[error]
=== TEST 8: set route(id: 1) with vars(in table)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[=[{
"methods": ["GET"],
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello",
"vars": [["http_user_agent", "IN", ["android", "ios"]]]
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 9: hit routes with user_agent=ios
--- request
GET /hello
--- more_headers
User-Agent: ios
--- response_body
hello world
--- no_error_log
[error]
=== TEST 10: hit routes with user_agent=android
--- request
GET /hello
--- more_headers
User-Agent: android
--- response_body
hello world
--- no_error_log
[error]
=== TEST 11: set route(id: 1) with vars(null)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[=[{
"methods": ["GET"],
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello",
"vars": [["http_user_agent", "==", null]]
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 12: not found because user_agent=android
--- request
GET /hello
--- more_headers
User-Agent: android
--- error_code: 404
--- response_body
{"error_msg":"404 Route Not Found"}
--- no_error_log
[error]
=== TEST 13: hit route
--- request
GET /hello
--- response_body
hello world
--- no_error_log
[error]
=== TEST 14: set route(id: 1) with vars(items are two)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
-- deprecated, will be removed soon
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[=[{
"methods": ["GET"],
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello",
"vars": [["http_user_agent", "ios"]]
}]=]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 15: hit routes with user_agent=ios
--- request
GET /hello
--- more_headers
User-Agent: ios
--- response_body
hello world
--- no_error_log
[error]