Skip to main content
Version: 3.17

mocking

描述#

mocking 插件允许你在不将请求转发到上游服务的情况下模拟 API 响应。该插件支持自定义响应状态码、响应体、响应头等。在开发、测试或调试阶段,当实际上游服务不可用、正在维护或调用成本较高时,该插件尤为有用。

属性#

名称类型必选项默认值描述
delayinteger0延迟返回响应的时间,单位为秒。
response_statusinteger200响应的 HTTP 状态码。
content_typestringapplication/json;charset=utf8响应的 Content-Type 标头值。
response_examplestring二选一响应体内容。支持 NGINX 变量,例如 $remote_addr。与 response_schema 二选一,且至少配置其中一个,不能同时配置。
response_schemaobject二选一用于生成随机模拟响应体的 JSON Schema 对象。与 response_example 二选一,且至少配置其中一个,不能同时配置。
with_mock_headerbooleantrue设置为 true 时,将添加响应头 x-mock-by: APISIX/{version}
response_headersobject要添加到模拟响应中的标头。例如:{"X-Foo": "bar"}
note

response_exampleresponse_schema 不能同时配置,且至少需要配置其中一个,否则插件配置将无法通过校验。

response_schema 支持以下字段类型:

  • string
  • number
  • integer
  • boolean
  • object
  • array

示例#

下面的示例演示了如何在不同场景中在路由上配置 mocking

note

你可以这样从 config.yaml 中获取 admin_key 并存入环境变量:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

生成特定模拟响应#

以下示例演示如何配置插件以生成特定的模拟响应和响应状态码,而不将请求转发到上游服务。

创建带有 mocking 插件的路由:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${admin_key}" \
-d '{
"id": "mocking-route",
"uri": "/anything",
"plugins": {
"mocking": {
"response_status": 201,
"response_example": "{\"Lastname\":\"Brown\",\"Age\":56}"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

向路由发送请求:

curl -i "http://127.0.0.1:9080/anything"

你应该收到 HTTP/1.1 201 Created 模拟响应,响应体如下:

{"Lastname":"Brown","Age":56}

生成模拟响应标头#

以下示例演示如何配置插件以生成模拟响应标头,并在响应体中使用内置的 NGINX 变量。

创建带有 mocking 插件的路由:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${admin_key}" \
-d '{
"id": "mocking-route",
"uri": "/anything",
"plugins": {
"mocking": {
"response_headers": {
"X-User-Id": "100",
"X-Product-Id": "apac-398-472"
},
"response_example": "Client IP: $remote_addr"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

向路由发送请求:

curl -i "http://127.0.0.1:9080/anything"

你应该收到类似以下内容的响应:

HTTP/1.1 200 OK
...
X-Product-Id: apac-398-472
X-User-Id: 100

Client IP: 192.168.65.1

使用 JSON Schema 生成模拟响应#

以下示例演示如何配置插件以按照特定的 JSON Schema 生成模拟响应。

创建带有 mocking 插件的路由,并定义 JSON Schema:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${admin_key}" \
-d '{
"id": "mocking-route",
"uri": "/anything",
"plugins": {
"mocking": {
"response_schema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"example": "abcd"
},
"ip": {
"type": "number",
"example": 192.168
},
"random_str_arr": {
"type": "array",
"items": {
"type": "string"
}
},
"nested_obj": {
"type": "object",
"properties": {
"random_str": {
"type": "string"
},
"child_nested_obj": {
"type": "object",
"properties": {
"random_bool": {
"type": "boolean",
"example": true
},
"random_int_arr": {
"type": "array",
"items": {
"type": "integer",
"example": 155
}
}
}
}
}
}
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

向路由发送请求:

curl -i "http://127.0.0.1:9080/anything"

你应该看到类似以下内容的模拟响应,而非来自上游服务的实际响应:

{
"ip": 192.168,
"random_str_arr": [
"fb", "lyquibkwc", "r"
],
"id": "abcd",
"nested_obj": {
"random_str": "bzbb",
"child_nested_obj": {
"random_bool": true,
"random_int_arr": [155, 155, 155]
}
}
}