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实例:

  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. }

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

  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. }

如何启用

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

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

测试插件

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. }

curl访问将返回如下结果:

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

移除插件

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

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

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