Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mocking plugin #5940

Merged
merged 63 commits into from Feb 25, 2022
Merged

feat: mocking plugin #5940

merged 63 commits into from Feb 25, 2022

Conversation

Drery
Copy link
Contributor

@Drery Drery commented Dec 27, 2021

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.

apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Show resolved Hide resolved
apisix/plugins/mocking.lua Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
docs/zh/latest/plugins/mocking.md Outdated Show resolved Hide resolved
docs/zh/latest/plugins/mocking.md Outdated Show resolved Hide resolved
@shuaijinchao
Copy link
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
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
Copy link
Contributor Author

Drery commented Dec 29, 2021

@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
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
Copy link
Contributor Author

Drery commented Dec 29, 2021

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
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
Member

yes, @Drery here is the mailing list of Proposals, see https://lists.apache.org/list?dev@apisix.apache.org:lte=1M:proposal

image

Here is the mailing list subscription guide: https://apisix.apache.org/docs/general/subscribe-guide

@Drery
Copy link
Contributor Author

Drery commented Dec 31, 2021

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

@juzhiyuan
Copy link
Member

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

docs/zh/latest/plugins/mocking.md Outdated Show resolved Hide resolved
docs/zh/latest/plugins/mocking.md Outdated Show resolved Hide resolved
docs/zh/latest/plugins/mocking.md Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
apisix/plugins/mocking.lua Outdated Show resolved Hide resolved
Drery and others added 5 commits January 10, 2022 10:51
Co-authored-by: leslie <59061168+leslie-tsang@users.noreply.github.com>
Co-authored-by: leslie <59061168+leslie-tsang@users.noreply.github.com>
@Drery
Copy link
Contributor Author

Drery commented Feb 16, 2022

@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
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
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~

docs/en/latest/plugins/mocking.md Outdated Show resolved Hide resolved
docs/en/latest/plugins/mocking.md Outdated Show resolved Hide resolved
Drery and others added 2 commits February 16, 2022 11:50
Co-authored-by: leslie <59061168+leslie-tsang@users.noreply.github.com>
Co-authored-by: leslie <59061168+leslie-tsang@users.noreply.github.com>
@Drery
Copy link
Contributor Author

Drery commented Feb 16, 2022

@leslie-tsang done

@Drery
Copy link
Contributor Author

Drery commented Feb 18, 2022

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

spacewander
spacewander previously approved these changes Feb 18, 2022
@spacewander
Copy link
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
Copy link
Member

@leslie-tsang leslie-tsang left a comment

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
Copy link
Contributor Author

Drery commented Feb 18, 2022

@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
Member

@shuaijinchao
Please have a look when you are free.

shuaijinchao
shuaijinchao previously approved these changes Feb 25, 2022
Copy link
Member

@shuaijinchao shuaijinchao left a comment

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
Copy link
Member

@bisakhmondal bisakhmondal left a comment

Choose a reason for hiding this comment

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

Thanks, @Drery.

conf/config-default.yaml Outdated Show resolved Hide resolved
Co-authored-by: Bisakh <bisakhmondal00@gmail.com>
@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.

None yet

7 participants