Skip to content

Commit

Permalink
fix: mocking plugin panic when response_example contain $ (#8810)
Browse files Browse the repository at this point in the history
  • Loading branch information
roketyyang committed Feb 8, 2023
1 parent 5984321 commit 0efa561
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apisix/plugins/mocking.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function gen_by_property(property)
end


function _M.access(conf)
function _M.access(conf, ctx)
local response_content = ""

if conf.response_example then
Expand All @@ -218,7 +218,7 @@ function _M.access(conf)
if conf.delay > 0 then
ngx.sleep(conf.delay)
end
return conf.response_status, core.utils.resolve_var(response_content)
return conf.response_status, core.utils.resolve_var(response_content, ctx.var)
end

return _M
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The `mocking` Plugin is used for mocking an API. When executed, it returns rando
| delay | integer | False | | Response delay in seconds. |
| response_status | integer | False | 200 | HTTP status code of the response. |
| content_type | string | False | application/json | Header `Content-Type` of the response. |
| response_example | string | False | | Body of the response. |
| response_example | string | False | | Body of the response, support use variables, like `$remote_addr $consumer_name`. |
| response_schema | object | False | | The JSON schema object for the response. Works when `response_example` is unspecified. |
| with_mock_header | boolean | False | true | When set to `true`, adds a response header `x-mock-by: APISIX/{version}`. |

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ description: 本文介绍了关于 Apache APISIX `mocking` 插件的基本信息
| delay | integer|| | 延时返回的时间,单位为秒。 |
| response_status | integer|| 200 | 返回响应的 HTTP 状态码。 |
| content_type | string || application/json | 返回响应的 Header `Content-Type`|
| response_example| string || | 返回响应的 Body,与 `response_schema` 字段二选一。 |
| response_example| string || | 返回响应的 Body,支持使用变量,例如 `$remote_addr $consumer_name``response_schema` 字段二选一。 |
| response_schema | object || | 指定响应的 `jsonschema` 对象,未指定 `response_example` 字段时生效。 |
| with_mock_header| boolean|| true | 当设置为 `true` 时,将添加响应头 `x-mock-by: APISIX/{version}`。设置为 `false` 时则不添加该响应头。 |

Expand Down
78 changes: 78 additions & 0 deletions t/plugin/mocking.t
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,81 @@ passed
GET /hello
--- response_headers
Content-Type: application/json
=== TEST 15: set route(return response example:"remote_addr:127.0.0.1")
--- 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": {
"mocking": {
"delay": 1,
"content_type": "text/plain",
"response_status": 200,
"response_example": "remote_addr:$remote_addr"
}
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 16: hit route(return response example:"remote_addr:127.0.0.1")
--- request
GET /hello
--- response_body chomp
remote_addr:127.0.0.1
=== TEST 17: set route(return response example:"empty_var:")
--- 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": {
"mocking": {
"delay": 1,
"content_type": "text/plain",
"response_status": 200,
"response_example": "empty_var:$foo"
}
},
"uri": "/hello"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 18: hit route(return response example:"empty_var:")
--- request
GET /hello
--- response_body chomp
empty_var:

0 comments on commit 0efa561

Please sign in to comment.