mocking

Description

The mocking Plugin is used for mocking an API. When executed, it returns random mock data in the format specified and the request is not forwarded to the Upstream.

Attributes

NameTypeRequiredDefaultDescription
delayintegerFalseResponse delay in seconds.
response_statusintegerFalse200HTTP status code of the response.
content_typestringFalseapplication/jsonHeader Content-Type of the response.
response_examplestringFalseBody of the response, support use variables, like $remote_addr $consumer_name.
response_schemaobjectFalseThe JSON schema object for the response. Works when response_example is unspecified.
with_mock_headerbooleanFalsetrueWhen set to true, adds a response header x-mock-by: APISIX/{version}.
response_headersobjectfalseHeaders to be added in the mocked response. Example: {“X-Foo”: “bar”, “X-Few”: “baz”}

The JSON schema supports the following types in their fields:

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

Here is a JSON schema example:

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

This is the response generated by the Plugin from this 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. }

Enable Plugin

The example below configures the mocking Plugin for a specific Route:

mocking - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -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. }'

Example usage

Once you have configured the Plugin as mentioned above, you can test the Route.

The example used here uses this mocked response:

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

Now to test the Route:

  1. curl http://127.0.0.1:9080/test-mock -i
  1. HTTP/1.1 201 Created
  2. ...
  3. Content-Type: application/json;charset=utf8
  4. x-mock-by: APISIX/2.10.0
  5. ...
  6. {"a":1,"b":2}

Delete Plugin

To remove the mocking Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -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. }'