Skip to content

feat: mocking plugin#5940

Merged
spacewander merged 63 commits into
apache:masterfrom
drery:master
Feb 25, 2022
Merged

feat: mocking plugin#5940
spacewander merged 63 commits into
apache:masterfrom
drery:master

Conversation

@drery

@drery drery commented Dec 27, 2021

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

Background

usualy when we develop a new feature on website or miniprogram or mobile application, we go through 3 steps:

  1. design api documents,include api path,paramter,response;
  2. develop frontend and backend at the same time;
  3. testing and debugging the frontend and backend program.

in step 2, the frontend need to mock the api’s parameter and response,to debug program,and if there’s multiple front-end applications,each app need to do mock.

consider this scenario,if our api gateway can support api mocking,the steps 2 will come down to:

  1. turn on mocking plugin when backend is still developing apis.
  2. turn off mocking plugin when the api is finished.
    and there is nothing need to do for frontend developer,even neednt to change the api url.

based on the above scenario, the mocking plugin can be use in more scenario,such as:
when we pressure test the APISIX. we can use the mocking plugins to mock duration of api and status and response,instead of real upstream.

in microservice invocation chain,we can mock service one by one to find the  abnormal service.
Plugin Design

this plugin design is reference to mock.js and kong's mocking plugin.
it is planned to be implemented in two phases.

phase 1, goal to implement base mock abilities.
the general design is as follows:
local schema = {
type = "object",
properties = {
-- specify response delay time,default 0ms
delay = { type = "integer" },
-- specify response status,default 200
response_status = { type = "integer" },
-- specify response content type,support application/xml,text/plain and application/json,default application/json
content_type = { type = "content_type" },
-- specify response body.
response_example = {type = "string"},
-- specify response json schema,if response_example is not nil,this conf will be ignore.
-- generate random response by json schema.
response_schema = { type = "object" },
},
anyOf = {
{required = {"response_example"}},
{required = {"response_schema"}}
}
}

phase 2, goal to implement higher-order ability,such as "expectation and response patterns" and file download.

Related Discussion is #5896
The mail link is https://lists.apache.org/thread/xbkwz2k1dpoxgpcs7gm8t8d6ddvq1dto

Pre-submission checklist:

Did you explain what problem does this PR solve? Or what new features have been added?

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

others

What do you think about this idea?
i just commit the plugin code and chinese document now, If the idead works, i can attach else neccessary.

Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
@drery
drery requested a review from spacewander December 28, 2021 03:24
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua
Comment thread apisix/plugins/mocking.lua
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread docs/zh/latest/plugins/mocking.md Outdated
Comment thread docs/zh/latest/plugins/mocking.md Outdated
Comment thread apisix/plugins/mocking.lua Outdated
@shuaijinchao

Copy link
Copy Markdown
Member

hi, @drery thanks for your contribution. Mock has many applications in front-end and back-end development and client troubleshooting scenarios. this is a good idea. However, I checked this PR. Its current completion rate is relatively low and only supports responses in a single data format.I think the simulation plug-in should support custom responses, custom data formats, custom headers, custom status codes, and custom timeouts to meet the basic simulation requirements.

@juzhiyuan

Copy link
Copy Markdown
Member

Hi I just added the discussion to this PR #5896

@drery would like to contribute the Mocking plugin as Kong does 😄 Do we need a proposal or design guide?

@drery

drery commented Dec 29, 2021

Copy link
Copy Markdown
Contributor Author

@juzhiyuan the kong's mocking plugin is support too simple.i design this plugin is reference to mock.js which is closer to the scenario used for front-end developer.

https://github.com/nuysoft/Mock/wiki/Mock.Random

@juzhiyuan

Copy link
Copy Markdown
Member

Glad to know that, and I used mock.js to do mocking as well 😄 Could @shuaijinchao help to guide @drery if we need this kind of plugin?

@drery

drery commented Dec 29, 2021

Copy link
Copy Markdown
Contributor Author

Glad to know that, and I used mock.js to do mocking as well 😄 Could @shuaijinchao help to guide @drery if we need this kind of plugin?
@juzhiyuan thank you for your attention.
just in our work scene, if the api gateway support mock response, will improve frontend development efficiency.
we can bind or unbind the mocking plugin to convert the mock response or real upstream response, and it's need not to modify js code.

@shuaijinchao

Copy link
Copy Markdown
Member

hi @drery, usually when we add new features, we need to send a proposal to the mailing list. This includes background, plan, and design details. After an open discussion in the community, the coding is completed after the final plan is determined. I think you can start this feature from the proposal first.

cc @juzhiyuan

@juzhiyuan

Copy link
Copy Markdown
Member

@drery

drery commented Dec 31, 2021

Copy link
Copy Markdown
Contributor Author

@shuaijinchao @juzhiyuan
Thank you.
I already send a proposal to the mailing list.

@juzhiyuan

Copy link
Copy Markdown
Member

The mail link is https://lists.apache.org/thread/xbkwz2k1dpoxgpcs7gm8t8d6ddvq1dto

Comment thread docs/zh/latest/plugins/mocking.md Outdated
Comment thread docs/zh/latest/plugins/mocking.md Outdated
Comment thread docs/zh/latest/plugins/mocking.md Outdated
Comment thread apisix/plugins/mocking.lua
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread apisix/plugins/mocking.lua Outdated
Comment thread docs/zh/latest/plugins/mocking.md Outdated
@drery

drery commented Feb 16, 2022

Copy link
Copy Markdown
Contributor Author

@spacewander done, is it sorted as this?

Comment on lines +218 to +220
When you want to disable this plugin, it is very simple,
you can delete the corresponding JSON configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When you want to disable this plugin, it is very simple,
you can delete the corresponding JSON configuration in the plugin configuration,
no need to restart the service, it will take effect immediately:
To disable this plugin, simply delete the corresponding JSON configuration in the plugin configuration,
there is no need to restart the service, the change will take effect immediately:

Would be better ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emm, I just copy this from other plugins~

Comment thread docs/en/latest/plugins/mocking.md Outdated
Comment thread docs/en/latest/plugins/mocking.md Outdated
@drery

drery commented Feb 16, 2022

Copy link
Copy Markdown
Contributor Author

@leslie-tsang done

@drery

drery commented Feb 18, 2022

Copy link
Copy Markdown
Contributor Author

@shuaijinchao @juzhiyuan @leslie-tsang @spacewander 请各位大佬再看一下,还有不要再抠格式排版这种细节了好吗???

spacewander
spacewander previously approved these changes Feb 18, 2022
@spacewander

Copy link
Copy Markdown
Member

@shuaijinchao @juzhiyuan @leslie-tsang @spacewander 请各位大佬再看一下,还有不要再抠格式排版这种细节了好吗???

I can understand your impatience. This PR has been reviewed for a long time.
The reviews in APISIX is very sensitive about the details, even a core developer like me (who has submitted 600+ PR) still get a review comment about the usage of word, for example: #5984 (comment).
So.... Just get used to it.

leslie-tsang
leslie-tsang previously approved these changes Feb 18, 2022

@leslie-tsang leslie-tsang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, There is some optimization of implementation like ctx.lua that can be done in another PR.

Thanks for your contribution, :)

@drery
drery dismissed stale reviews from leslie-tsang and spacewander via 502a69b February 18, 2022 06:25
@drery

drery commented Feb 18, 2022

Copy link
Copy Markdown
Contributor Author

@spacewander @leslie-tsang Thanks, I resolved the conflicts, please have a review again.

spacewander
spacewander previously approved these changes Feb 19, 2022
leslie-tsang
leslie-tsang previously approved these changes Feb 19, 2022
@spacewander

Copy link
Copy Markdown
Member

@shuaijinchao
Please have a look when you are free.

shuaijinchao
shuaijinchao previously approved these changes Feb 25, 2022

@shuaijinchao shuaijinchao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Great @drery

bisakhmondal
bisakhmondal previously approved these changes Feb 25, 2022

@bisakhmondal bisakhmondal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @drery.

Comment thread conf/config-default.yaml Outdated
@spacewander
spacewander merged commit a8528ad into apache:master Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants