mirror of
https://gitee.com/iresty/apisix.git
synced 2024-12-04 21:17:36 +08:00
feat(request-validation): add custom rejected_code (#5553)
This commit is contained in:
parent
3fa0c33534
commit
2262e1c93a
@ -26,6 +26,7 @@ local schema = {
|
||||
properties = {
|
||||
header_schema = {type = "object"},
|
||||
body_schema = {type = "object"},
|
||||
rejected_code = {type = "integer", minimum = 200, maximum = 599},
|
||||
rejected_msg = {type = "string", minLength = 1, maxLength = 256}
|
||||
},
|
||||
anyOf = {
|
||||
@ -75,7 +76,7 @@ function _M.rewrite(conf)
|
||||
local ok, err = core.schema.check(conf.header_schema, headers)
|
||||
if not ok then
|
||||
core.log.error("req schema validation failed", err)
|
||||
return 400, conf.rejected_msg or err
|
||||
return conf.rejected_code or 400, conf.rejected_msg or err
|
||||
end
|
||||
end
|
||||
|
||||
@ -87,11 +88,11 @@ function _M.rewrite(conf)
|
||||
if not body then
|
||||
local filename = ngx.req.get_body_file()
|
||||
if not filename then
|
||||
return 500, conf.rejected_msg
|
||||
return conf.rejected_code or 500, conf.rejected_msg
|
||||
end
|
||||
local fd = io.open(filename, 'rb')
|
||||
if not fd then
|
||||
return 500, conf.rejected_msg
|
||||
return conf.rejected_code or 500, conf.rejected_msg
|
||||
end
|
||||
body = fd:read('*a')
|
||||
end
|
||||
@ -104,13 +105,13 @@ function _M.rewrite(conf)
|
||||
|
||||
if not req_body then
|
||||
core.log.error('failed to decode the req body', error)
|
||||
return 400, conf.rejected_msg or error
|
||||
return conf.rejected_code or 400, conf.rejected_msg or error
|
||||
end
|
||||
|
||||
local ok, err = core.schema.check(conf.body_schema, req_body)
|
||||
if not ok then
|
||||
core.log.error("req schema validation failed", err)
|
||||
return 400, conf.rejected_msg or err
|
||||
return conf.rejected_code or 400, conf.rejected_msg or err
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -45,6 +45,7 @@ For more information on schema, refer to [JSON schema](https://github.com/api7/j
|
||||
| ---------------- | ------ | ----------- | ------- | ----- | -------------------------- |
|
||||
| header_schema | object | optional | | | schema for the header data |
|
||||
| body_schema | object | optional | | | schema for the body data |
|
||||
| rejected_code | integer | optional | | [200,...,599] | the custom rejected code |
|
||||
| rejected_msg | string | optional | | | the custom rejected message |
|
||||
|
||||
## How To Enable
|
||||
|
@ -44,6 +44,7 @@ title: request-validation
|
||||
| ---------------- | ------ | ----------- | ------- | ----- | --------------------------------- |
|
||||
| header_schema | object | 可选 | | | `header` 数据的 `schema` 数据结构 |
|
||||
| body_schema | object | 可选 | | | `body` 数据的 `schema` 数据结构 |
|
||||
| rejected_code | integer | 可选 | | [200,...,599] | 自定义拒绝状态码 |
|
||||
| rejected_msg | string | 可选 | | | 自定义拒绝信息 |
|
||||
|
||||
## 如何启用
|
||||
|
163
t/plugin/request-validation.t
vendored
163
t/plugin/request-validation.t
vendored
@ -1658,3 +1658,166 @@ qr/object matches none of the requireds/
|
||||
400
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 45: add route (test request validation `body_schema.required` success with custom reject code)
|
||||
--- 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,
|
||||
[[{
|
||||
"plugins": {
|
||||
"request-validation": {
|
||||
"body_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"test": {
|
||||
"type": "string",
|
||||
"enum": ["a", "b", "c"]
|
||||
}
|
||||
},
|
||||
"required": ["test"]
|
||||
},
|
||||
"rejected_code": 505
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1982": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/opentracing"
|
||||
}]])
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body
|
||||
passed
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 46: use empty body to hit custom rejected code rule
|
||||
--- request
|
||||
GET /opentracing
|
||||
--- error_code: 505
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 47: use bad body value to hit custom rejected code rule
|
||||
--- request
|
||||
POST /opentracing
|
||||
{"test":"abc"}
|
||||
--- error_code: 505
|
||||
--- error_log eval
|
||||
qr/schema validation failed/
|
||||
|
||||
|
||||
|
||||
=== TEST 48: pass custom rejected code rule
|
||||
--- request
|
||||
POST /opentracing
|
||||
{"test":"a"}
|
||||
--- error_code: 200
|
||||
--- response_body eval
|
||||
qr/opentracing/
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 49: add route (test request validation `header_schema.required` failure with custom reject code)
|
||||
--- 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,
|
||||
[[{
|
||||
"plugins": {
|
||||
"request-validation": {
|
||||
"header_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"test": {
|
||||
"type": "string",
|
||||
"enum": ["a", "b", "c"]
|
||||
}
|
||||
},
|
||||
"required": ["test"]
|
||||
},
|
||||
"rejected_code": 10000
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1982": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/plugin/request/validation"
|
||||
}]])
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body_like eval
|
||||
qr/expected 10000 to be smaller than 599/
|
||||
--- error_code chomp
|
||||
400
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
||||
|
||||
|
||||
=== TEST 50: add route (test request validation schema with custom reject code only)
|
||||
--- 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,
|
||||
[[{
|
||||
"plugins": {
|
||||
"request-validation": {
|
||||
"rejected_code": 505
|
||||
}
|
||||
},
|
||||
"upstream": {
|
||||
"nodes": {
|
||||
"127.0.0.1:1982": 1
|
||||
},
|
||||
"type": "roundrobin"
|
||||
},
|
||||
"uri": "/plugin/request/validation"
|
||||
}]])
|
||||
if code >= 300 then
|
||||
ngx.status = code
|
||||
end
|
||||
ngx.say(body)
|
||||
}
|
||||
}
|
||||
--- request
|
||||
GET /t
|
||||
--- response_body_like eval
|
||||
qr/object matches none of the requireds/
|
||||
--- error_code chomp
|
||||
400
|
||||
--- no_error_log
|
||||
[error]
|
||||
|
Loading…
Reference in New Issue
Block a user