mocking

描述

mocking 插件用于模拟 API。当执行该插件时,它将随机返回指定格式的模拟数据,并且请求不会转发到上游。

属性

名称类型必选项默认值描述
delayinteger延时返回的时间,单位为秒。
response_statusinteger200返回响应的 HTTP 状态码。
content_typestringapplication/json返回响应的 Header Content-Type
response_examplestring返回响应的 Body,与 response_schema 字段二选一。
response_schemaobject指定响应的 jsonschema 对象,未指定 response_example 字段时生效。
with_mock_headerbooleantrue当设置为 true 时,将添加响应头 x-mock-by: APISIX/{version}。设置为 false 时则不添加该响应头。

JSON Schema 在其字段中支持以下类型:

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

以下是一个 JSON Schema 示例:

  1. {
  2. "properties":{
  3. "field0":{
  4. "example":"abcd",
  5. "type":"string"
  6. },
  7. "field1":{
  8. "example":123.12,
  9. "type":"number"
  10. },
  11. "field3":{
  12. "properties":{
  13. "field3_1":{
  14. "type":"string"
  15. },
  16. "field3_2":{
  17. "properties":{
  18. "field3_2_1":{
  19. "example":true,
  20. "type":"boolean"
  21. },
  22. "field3_2_2":{
  23. "items":{
  24. "example":155.55,
  25. "type":"integer"
  26. },
  27. "type":"array"
  28. }
  29. },
  30. "type":"object"
  31. }
  32. },
  33. "type":"object"
  34. },
  35. "field2":{
  36. "items":{
  37. "type":"string"
  38. },
  39. "type":"array"
  40. }
  41. },
  42. "type":"object"
  43. }

以下为上述 JSON Schema 可能生成的返回对象:

  1. {
  2. "field1": 123.12,
  3. "field3": {
  4. "field3_1": "LCFE0",
  5. "field3_2": {
  6. "field3_2_1": true,
  7. "field3_2_2": [
  8. 155,
  9. 155
  10. ]
  11. }
  12. },
  13. "field0": "abcd",
  14. "field2": [
  15. "sC"
  16. ]
  17. }

启用插件

你可以通过如下命令在指定路由上启用 mocking 插件:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/index.html",
  6. "plugins": {
  7. "mocking": {
  8. "delay": 1,
  9. "content_type": "application/json",
  10. "response_status": 200,
  11. "response_schema": {
  12. "properties":{
  13. "field0":{
  14. "example":"abcd",
  15. "type":"string"
  16. },
  17. "field1":{
  18. "example":123.12,
  19. "type":"number"
  20. },
  21. "field3":{
  22. "properties":{
  23. "field3_1":{
  24. "type":"string"
  25. },
  26. "field3_2":{
  27. "properties":{
  28. "field3_2_1":{
  29. "example":true,
  30. "type":"boolean"
  31. },
  32. "field3_2_2":{
  33. "items":{
  34. "example":155.55,
  35. "type":"integer"
  36. },
  37. "type":"array"
  38. }
  39. },
  40. "type":"object"
  41. }
  42. },
  43. "type":"object"
  44. },
  45. "field2":{
  46. "items":{
  47. "type":"string"
  48. },
  49. "type":"array"
  50. }
  51. },
  52. "type":"object"
  53. }
  54. }
  55. },
  56. "upstream": {
  57. "type": "roundrobin",
  58. "nodes": {
  59. "127.0.0.1:1980": 1
  60. }
  61. }
  62. }'

测试插件

通过上述命令启用插件后,可以使用如下方式测试插件是否启用成功:

mocking 插件配置如下:

  1. {
  2. "delay":0,
  3. "content_type":"",
  4. "with_mock_header":true,
  5. "response_status":201,
  6. "response_example":"{\"a\":1,\"b\":2}"
  7. }

通过如下命令进行测试:

  1. curl http://127.0.0.1:9080/test-mock -i
  1. HTTP/1.1 201 Created
  2. Date: Fri, 14 Jan 2022 11:49:34 GMT
  3. Content-Type: application/json;charset=utf8
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. x-mock-by: APISIX/2.10.0
  7. Server: APISIX/2.10.0
  8. {"a":1,"b":2}

禁用插件

当你需要禁用 mocking 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "methods": ["GET"],
  5. "uri": "/index.html",
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. }'