Skip to main content
Version: 2.13

mocking

描述#

Mock API 插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。

属性#

名称类型必选项默认值有效值描述
delayinteger可选延时返回的时间,单位为秒
response_statusinteger可选200返回的响应 http status code
content_typestring可选application/json返回的响应头的 Content-Type。
response_examplestring可选返回的响应体,与response_schema字段二选一
response_schemaobject可选指定响应的jsonschema对象,未指定response_example字段时生效,具体结构看后文说明
with_mock_headerboolean可选true是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为 false 则不返回

支持的字段类型:string, number, integer, boolean, object, array 基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。 以下是一个jsonschema实例:

{
"properties":{
"field0":{
"example":"abcd",
"type":"string"
},
"field1":{
"example":123.12,
"type":"number"
},
"field3":{
"properties":{
"field3_1":{
"type":"string"
},
"field3_2":{
"properties":{
"field3_2_1":{
"example":true,
"type":"boolean"
},
"field3_2_2":{
"items":{
"example":155.55,
"type":"integer"
},
"type":"array"
}
},
"type":"object"
}
},
"type":"object"
},
"field2":{
"items":{
"type":"string"
},
"type":"array"
}
},
"type":"object"
}

以下为该jsonschema可能生成的返回对象:

{
"field1": 123.12,
"field3": {
"field3_1": "LCFE0",
"field3_2": {
"field3_2_1": true,
"field3_2_2": [
155,
155
]
}
},
"field0": "abcd",
"field2": [
"sC"
]
}

如何启用#

这里以route为例(service的使用是同样的方法),在指定的 route 上启用 mocking 插件。

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": ["GET"],
"uri": "/index.html",
"plugins": {
"mocking": {
"delay": 1,
"content_type": "application/json",
"response_status": 200,
"response_schema": {
"properties":{
"field0":{
"example":"abcd",
"type":"string"
},
"field1":{
"example":123.12,
"type":"number"
},
"field3":{
"properties":{
"field3_1":{
"type":"string"
},
"field3_2":{
"properties":{
"field3_2_1":{
"example":true,
"type":"boolean"
},
"field3_2_2":{
"items":{
"example":155.55,
"type":"integer"
},
"type":"array"
}
},
"type":"object"
}
},
"type":"object"
},
"field2":{
"items":{
"type":"string"
},
"type":"array"
}
},
"type":"object"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'

测试插件#

mocking插件配置如下时:

{
"delay":0,
"content_type":"",
"with_mock_header":true,
"response_status":201,
"response_example":"{\"a\":1,\"b\":2}"
}

curl访问将返回如下结果:

$ curl http://127.0.0.1:9080/test-mock -i
HTTP/1.1 201 Created
Date: Fri, 14 Jan 2022 11:49:34 GMT
Content-Type: application/json;charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
x-mock-by: APISIX/2.10.0
Server: APISIX/2.10.0

{"a":1,"b":2}

移除插件#

当你想去掉mocking插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": ["GET"],
"uri": "/index.html",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'

现在就已经移除了mocking插件了。其他插件的开启和移除也是同样的方法。