Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mocking plugin panic when response_example contain $ (#8810) #8816

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: