You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/12/27 11:43:42 UTC
[GitHub] [apisix] Drery opened a new pull request #5940: feat: mocking plugin
Drery opened a new pull request #5940:
URL: https://github.com/apache/apisix/pull/5940
### What this PR does / why we need it:
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
it is a api mock plugin, generate random response with the json-schema.
this plugin is develop for front-end developer. when back-end devloper is coding the api, front-end developer can use mock response returned by "mocking plugin" to debugging code, and when api is finished, just unbind the "mock plugin" to proxy the real backend and get the real response.
### Pre-submission checklist:
<!--
Please follow the requirements:
1. Use Draft if the PR is not ready to be reviewed
2. Test is required for the feat/fix PR, unless you have a good reason
3. Doc is required for the feat PR
4. Use a new commit to resolve review instead of `push -f`
5. If you need to resolve merge conflicts after the PR is reviewed, please merge master but do not rebase
6. Use "request review" to notify the reviewer once you have resolved the review
-->
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](https://github.com/apache/apisix/tree/master#community) first**
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] bisakhmondal commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r814470012
##########
File path: conf/config-default.yaml
##########
@@ -329,8 +329,9 @@ plugins: # plugin list (sorted by priority)
- zipkin # priority: 11011
- request-id # priority: 11010
- fault-injection # priority: 11000
+ - mocking # priority: 10900
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+# - batch-requests # priority: 4010
Review comment:
```suggestion
#- batch-requests # priority: 4010
```
Let's keep it as it was earlier.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r788431950
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
Review comment:
@leslie-tsang
Hello, I already fixed this code lint.So what do I do next?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018213802
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1021898782
@starsz hello,please check again!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807496469
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+# - batch-requests # priority: 4010
Review comment:
Please sort the list by priority
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r776207099
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
+function _M.access(conf)
+ local output = gen_object(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
Review comment:
> 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.
@shuaijinchao thank for your sugguest. i commit the PR just want to know how do you think about this idea.
if you approve this idea, i will enrich this plugin to support more scenes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002476664
> 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1039775513
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r806515999
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,234 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
Review comment:
Please add space around English word
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+# - batch-requests # priority: 4010
Review comment:
Please note the indentation. And maybe we should put it together with `fault-injection` instead of `serverless-pre-function`?
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,364 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 2: hit route(return response example:"hello world")
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+hello world
+
+
+
+=== TEST 3: set route(return response schema: string case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"string",
+ "example":"hello"
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 4: hit route(return response schema: string case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":"hello"}
+
+
+
+=== TEST 5: set route(return response schema: integer case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"integer",
+ "example":4
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 6: hit route(return response schema: integer case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":4}
+
+
+
+=== TEST 7: set route(return response schema: number case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"number",
+ "example":5.5
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 8: hit route(return response schema: number case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":5.5}
+
+
+
+=== TEST 9: set route(return response schema: boolean case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"boolean",
+ "example":true
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 10: hit route(return response schema: boolean case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":true}
+
+
+
+=== TEST 11: set route(return response schema: object case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"object"
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 12: hit route(return response schema: object case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":{}}
+
+
+
+=== TEST 13: set route(return response header: application/json)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_example": "{\"field1\":{}}"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
Review comment:
Duplicate field
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
Review comment:
```suggestion
| response_schema | object | optional | | | The jsonschema object for the response is specified. This property takes effect if the `response_example` is not specified |
```
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,364 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
Review comment:
This field is set by default. Please remove it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1032149937
> >
>
> @spacewander I merged the master and the issue is not fixed, I fix it in my code, please run the CI again!
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r802221664
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,234 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
+| response_status | integer| 可选 | 200 | | 返回的响应http status code |
+| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_example时生效,具体结构看后文说明 |
+| with_mock_header | boolean | 可选 | true | | 是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为false则不返回 |
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object`, `array`
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
+以下是一个JSON-Schema实例:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+以下为该JSON-Schema可能生成的返回对象:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
+
+这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `mocking` 插件。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## 测试插件
+
+当`mocking`插件配置如下时:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+curl访问将返回如下结果:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: TAPISIX/2.10.0
Review comment:
> Ditto
fixed~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002831437
yes, @Drery here is the mailing list of Proposals, see https://lists.apache.org/list?dev@apisix.apache.org:lte=1M:proposal

Here is the mailing list subscription guide: https://apisix.apache.org/docs/general/subscribe-guide
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018216551
Hi @Drery, you could use online panel to fix this :)

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r788562924
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
Review comment:
@leslie-tsang Hi, how can I run code checks in my own fork repository
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r786750863
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
Review comment:
Hello there, Please refer to [Non-Global Functions](https://www.lua.org/pil/6.2.html).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r780016707
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,198 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
Review comment:
need to avoid access global metatable
```lua
local string = string
```
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,206 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+ - [简介](#简介)
+ - [属性](#属性)
+ - [示例](#示例)
+ - [如何在 `route` 或 `service` 上使用](#如何在`route`或`service`上使用)
+ - [如何在 `consumer` 上使用](#如何在`consumer`上使用)
+ - [移除插件](#移除插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒。 |
+| response_status | integer| 可选 | | | 返回的响应http status。 |
+| content_type | string | 可选 | | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一。 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明。 |
+支持的字段类型:string,number,integer,boolean,object,array。
Review comment:
```suggestion
支持的字段类型:`string`, `number`, `integer`, `boolean`, `object,array`
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,198 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local type, charset
+ if sep_idx then
+ type = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ type = content_type
+ end
+ return type, charset
+end
+
Review comment:
Plz follow the [CODE_STYLE.md](https://github.com/apache/apisix/blob/master/CODE_STYLE.md#blank-line)
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,206 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+ - [简介](#简介)
+ - [属性](#属性)
+ - [示例](#示例)
+ - [如何在 `route` 或 `service` 上使用](#如何在`route`或`service`上使用)
+ - [如何在 `consumer` 上使用](#如何在`consumer`上使用)
+ - [移除插件](#移除插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒。 |
+| response_status | integer| 可选 | | | 返回的响应http status。 |
+| content_type | string | 可选 | | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一。 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明。 |
Review comment:
```suggestion
| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
| response_status | integer| 可选 | 200 | | 返回的响应http status code |
| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明 |
```
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,206 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+ - [简介](#简介)
+ - [属性](#属性)
+ - [示例](#示例)
+ - [如何在 `route` 或 `service` 上使用](#如何在`route`或`service`上使用)
+ - [如何在 `consumer` 上使用](#如何在`consumer`上使用)
+ - [移除插件](#移除插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒。 |
+| response_status | integer| 可选 | | | 返回的响应http status。 |
+| content_type | string | 可选 | | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一。 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明。 |
+支持的字段类型:string,number,integer,boolean,object,array。
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
Review comment:
```suggestion
基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
```
Ditto
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,198 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local type, charset
+ if sep_idx then
+ type = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ type = content_type
+ end
+ return type, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local type, _ = parse_content_type(conf.content_type)
+ if type ~= "application/xml" and
+ type ~= "application/json" and
+ type ~= "text/plain" and
+ type ~= "text/html" and
+ type ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
Review comment:
```suggestion
if example ~= nil and type(example) == "string" then
```
`type` Ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1032131858
>
@spacewander I merged the master and the issue is not fixed, I fix it in my code, please run the CI again!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] shuaijinchao commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r801463943
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `integer`, `boolean`, `object`, `array`
+Base data types (string, number, integer, Boolean) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a JSON-Schema example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this JSON-Schema:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## How To Enable
+
+Here, use `route` as an example (`service` is used in the same way) to enable the `mocking` plugin on the specified `route`.
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## Test Plugin
+
+the `mocking` plugin is configured as follows:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+Use curl to access:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: TAPISIX/2.10.0
Review comment:
```suggestion
Server: APISIX/2.10.0
```
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,234 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
+| response_status | integer| 可选 | 200 | | 返回的响应http status code |
+| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_example时生效,具体结构看后文说明 |
+| with_mock_header | boolean | 可选 | true | | 是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为false则不返回 |
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object`, `array`
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
+以下是一个JSON-Schema实例:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+以下为该JSON-Schema可能生成的返回对象:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
+
+这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `mocking` 插件。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## 测试插件
+
+当`mocking`插件配置如下时:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+curl访问将返回如下结果:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: TAPISIX/2.10.0
Review comment:
Ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1041074878
@leslie-tsang done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery removed a comment on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery removed a comment on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018213802
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1020956521
@bisakhmondal @starsz I have already resolve the conversations, please check again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1039926998
@spacewander @shuaijinchao hello, all CI is success, please review again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807472898
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+# - batch-requests # priority: 4010
Review comment:
> Please note the indentation. And maybe we should put it together with `fault-injection` instead of `serverless-pre-function`?
ok,it's a good suggestion
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1041029268
@spacewander All comment resolved, please review again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1041053914
@spacewander done, is it sorted as this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1048513702
@shuaijinchao
Please have a look when you are free.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] bisakhmondal commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r791053166
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
Review comment:
Too many operations. Better do this
```lua
local m = ngx.re.match(content_type, "([ -~]*);([ -~]*)")
-- m[1] typ, m[2] charset
if #m == 2 then
return m[1], m[2]
end
return content_type
```
Also, please be consistent about return type. Line 68 charset is nil, but line 58 2nd return arg is ""
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
Review comment:
```suggestion
if example and type(example) == "string" then
```
would do just fine.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+
+local gen_array, gen_object
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
+ local output = {}
+ if property.properties == nil then
Review comment:
```suggestion
if not property.properties then
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
Review comment:
same and take care of similar rest of them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r780882639
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -43,7 +43,8 @@ Mock API插件,绑定该插件后将随机返回指定格式的mock数据,
| content_type | string | 可选 | | | 返回的响应头的Content-Type。 |
| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一。 |
| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明。 |
-支持的字段类型:string,number,integer,boolean,object,array。
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object,array`
Review comment:
object,array?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1032185633
@starsz Hello, all CI is pass, please check again if there are any issues that need to be fixed~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1023835155
Let's merge master to make CI pass?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1038727785
@spacewander @bisakhmondal All comment resolved, please take a look if that's right!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] starsz commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r792251381
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `INTEGER`, `Boolean`, `object`, `array`
Review comment:
Why `INTERGER` is upper case? And so do `Boolean`
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,207 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return ""
+ end
+ local m = ngx.re.match(content_type, "([ -~]*);([ -~]*)")
+ if #m == 2 then
+ return m[1], m[2]
+ end
+ return content_type
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ local typ = parse_content_type(conf.content_type)
+ local support_content_type = {
+ ["application/xml"] = true,
+ ["application/json"] = true,
+ ["text/plain"] = true,
+ ["text/html"] = true,
+ ["text/xml"] = true
+ }
+ if not support_content_type[typ] then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+
+local gen_array, gen_object
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
+ local output = {}
+ if not property.properties then
+ return output
+ end
+ for k, v in pairs(property.properties) do
+ local typ = string.lower(v.type)
+ if typ == "array" then
+ output[k] = gen_array(v)
+ elseif typ == "object" then
+ output[k] = gen_object(v)
+ else
+ output[k] = gen_base(v)
+ end
+ end
+ return output
+end
+
+function _M.access(conf)
+ local response_content = ""
+
+ if conf.response_example then
+ response_content = conf.response_example
+ else
+ local output = gen_object(conf.response_schema)
+ local typ, _ = parse_content_type(conf.content_type)
Review comment:
```suggestion
local typ = parse_content_type(conf.content_type)
```
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `INTEGER`, `Boolean`, `object`, `array`
+Base data types (string, number, integer, Boolean) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a JSON-Schema example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this JSON-Schema:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
Review comment:
Avoid using Chinese in English doc.
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,136 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: hit route(return response example:"hello world")
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+hello world
+--- no_error_log
+[error]
+
+
+
+=== TEST 3: set route(return response schema)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"string",
+ "example":"hello"
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 4: hit route(return response schema)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":"hello"}
+--- no_error_log
+[error]
Review comment:
Better to add a test for `string`, `number`, `integer`, `boolean`.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,207 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return ""
+ end
+ local m = ngx.re.match(content_type, "([ -~]*);([ -~]*)")
Review comment:
Better to define `local ngx_re = ngx.re` in the front
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807496635
##########
File path: t/admin/plugins.t
##########
@@ -68,6 +68,7 @@ zipkin
request-id
fault-injection
serverless-pre-function
+mocking
Review comment:
Ditto (Please sort the list by priority)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander merged pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander merged pull request #5940:
URL: https://github.com/apache/apisix/pull/5940
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r786398848
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
Review comment:
```suggestion
-- specify response content type, support application/xml, text/plain
-- and application/json, default application/json
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
Review comment:
Ditto
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
Review comment:
```suggestion
local function gen_array(property)
```
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,232 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
+| response_status | integer| 可选 | 200 | | 返回的响应http status code |
+| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明 |
+| with_mock_header | boolean | 可选 | true | | 是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为false则不返回 |
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object`, `array`
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
+以下是一个json schema实例:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+以下为该json schema可能生成的返回对象:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
+
+这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `mocking` 插件。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## 测试插件
+当mocking插件配置如下时:
Review comment:
```suggestion
当 mocking 插件配置如下时:
```
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,232 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
+| response_status | integer| 可选 | 200 | | 返回的响应http status code |
+| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明 |
+| with_mock_header | boolean | 可选 | true | | 是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为false则不返回 |
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object`, `array`
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
+以下是一个json schema实例:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+以下为该json schema可能生成的返回对象:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
+
+这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `mocking` 插件。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## 测试插件
+当mocking插件配置如下时:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+curl访问将返回如下结果:
+```shell
+curl http://127.0.0.1:9080/test-mock -i
Review comment:
```suggestion
$ curl http://127.0.0.1:9080/test-mock -i
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
Review comment:
```suggestion
local type = type
local table = table
local math = math
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- specify response body.
+ response_example = { type = "string" },
+ -- specify response json schema,if response_example is not nil,this conf will be ignore.
Review comment:
```suggestion
-- specify response json schema, if response_example is not nil, this conf will be ignore.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r789348744
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
Review comment:
fork APISIX repo, and enable GitHub action. submit a PR to your own forks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery closed pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery closed pull request #5940:
URL: https://github.com/apache/apisix/pull/5940
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] starsz commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r790814817
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
Review comment:
```suggestion
response_status = { type = "integer", default = 200, minimum = 100 },
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
Review comment:
You can put this to schema define.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
Review comment:
Define a table like this would be better.
```
local support_content_type = {
["application/xml"] = true,
...
}
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
Review comment:
Better to use `enum` to define the support type.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
Review comment:
```suggestion
local typ = parse_content_type(conf.content_type)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1019458130
@spacewander @leslie-tsang
hello, what does this error mains?

the workflow link is: https://github.com/apache/apisix/runs/4911836590?check_suite_focus=true
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1019897972
@juzhiyuan @spacewander @shuaijinchao @leslie-tsang @bisakhmondal
hello, all workflow is pass, please review the code if there is any issue to fix?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r788431950
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
Review comment:
@leslie-tsang
Hello, I already fixed this code lint.So what do I do next?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1020956521
@bisakhmondal @starsz I have already resolve the conversations, please check again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] shuaijinchao commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002522578
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r802221664
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,234 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
+
+## 属性
+
+| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | 可选 | | | 延时返回的时间,单位为秒 |
+| response_status | integer| 可选 | 200 | | 返回的响应http status code |
+| content_type | string | 可选 | application/json | | 返回的响应头的Content-Type。 |
+| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一 |
+| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_example时生效,具体结构看后文说明 |
+| with_mock_header | boolean | 可选 | true | | 是否返回响应头:"x-mock-by: APISIX/{version}",默认返回,指定为false则不返回 |
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object`, `array`
+基础数据类型(string,number,integer,boolean)可通过配置example属性指定生成的响应值,未配置时随机返回。
+以下是一个JSON-Schema实例:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+以下为该JSON-Schema可能生成的返回对象:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## 如何启用
+
+这里以`route`为例(`service`的使用是同样的方法),在指定的 `route` 上启用 `mocking` 插件。
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## 测试插件
+
+当`mocking`插件配置如下时:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+curl访问将返回如下结果:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: TAPISIX/2.10.0
Review comment:
> Ditto
@shuaijinchao @starsz fixed~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1038559716
@spacewander is there any progress?
<img width="800" alt="image" src="https://user-images.githubusercontent.com/9653109/153789392-1ad529ab-6928-4ac9-ac75-369ab34db1dc.png">
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807472898
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+# - batch-requests # priority: 4010
Review comment:
> Please note the indentation. And maybe we should put it together with `fault-injection` instead of `serverless-pre-function`?
@spacewander ok,it's a good suggestion,how about 10900?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1043823300
> @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: https://github.com/apache/apisix/pull/5984#discussion_r777187158.
So.... Just get used to it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1043731989
@shuaijinchao @juzhiyuan @leslie-tsang @spacewander 请各位大佬再看一下,还有不要再抠格式排版这种细节了好吗???
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r802590061
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,230 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+local ngx_re = ngx.re
+local json = core.json
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return ""
+ end
+ local m = ngx_re.match(content_type, "([ -~]*);([ -~]*)")
+ if m and #m == 2 then
+ return m[1], m[2]
+ end
+ return content_type
+end
+
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ local typ = parse_content_type(conf.content_type)
+ local support_content_type = {
+ ["application/xml"] = true,
+ ["application/json"] = true,
+ ["text/plain"] = true,
+ ["text/html"] = true,
+ ["text/xml"] = true
+ }
+ if not support_content_type[typ] then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+
+local function gen_string(example)
+ if example and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+
+local function gen_number(example)
+ if example and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+
+local function gen_integer(example)
+ if example and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+
+local function gen_boolean(example)
+ if example and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ end
+
+ if typ == "number" then
+ return gen_number(example)
+ end
+
+ if typ == "integer" then
+ return gen_integer(example)
+ end
+
+ if typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+
+local gen_array, gen_object
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
Review comment:
We can refactor this part and use a function that turns typ into the generated data.
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+ - batch-requests # priority: 4010
Review comment:
`batch-requests` should not be enabled...
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,402 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
Review comment:
CC @bisakhmondal
Could you give a detailed guide? Note that better to teach others to do it instead of doing it yourself.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,230 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+local ngx_re = ngx.re
+local json = core.json
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return ""
+ end
+ local m = ngx_re.match(content_type, "([ -~]*);([ -~]*)")
Review comment:
Please use `'jo'` in the regex function
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,230 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+local ngx_re = ngx.re
+local json = core.json
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return ""
+ end
+ local m = ngx_re.match(content_type, "([ -~]*);([ -~]*)")
+ if m and #m == 2 then
+ return m[1], m[2]
+ end
+ return content_type
+end
+
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ local typ = parse_content_type(conf.content_type)
+ local support_content_type = {
Review comment:
Let's move the constant table in the module level.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,230 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
Review comment:
Let's sort the statements to make the `require` in a group.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807496635
##########
File path: t/admin/plugins.t
##########
@@ -68,6 +68,7 @@ zipkin
request-id
fault-injection
serverless-pre-function
+mocking
Review comment:
Ditto
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,224 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local ngx = ngx
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local core = require("apisix.core")
+local xml2lua = require("xml2lua")
Review comment:
Let's put the `require` at the top, and sort all the `xxx = xxx` localized imports by their names.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1039775513
@spacewander I deleted one more line of code,please help me to run the CI again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018215189
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
emm, i have no access to resolve conflicts.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r791386377
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
Review comment:
> Better to use `enum` to define the support type.
@starsz because the content_type support two parts type and charset such as: "application/json" and "application/json;charset=utf8",so it's hard to define a enum~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1022882747
> LGTM. But CI failed.
@starsz hello,I fixed, please run the CI again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery edited a comment on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery edited a comment on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1022943852
@starsz hello, there's a error when run the unit test, but it's not produce by my code, please have a look at this.

it's should be produced by this comment:

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r775770634
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
Review comment:
Better to use 2 blank lines between functions
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
Review comment:
```suggestion
return core.schema.check(schema, conf)
```
is enough
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,196 @@
+---
+title: limit-req
Review comment:
Bad title
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,196 @@
+---
+title: limit-req
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+ - [简介](#简介)
+ - [属性](#属性)
+ - [示例](#示例)
Review comment:
Please follow the structure of other plugin doc, like https://raw.githubusercontent.com/apache/apisix/master/docs/zh/latest/plugins/gzip.md
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
+function _M.access(conf)
+ local output = gen_object(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
+ return 200, core.utils.resolve_var(core.json.encode(output))
+end
+
+function gen_object(property)
+ local output = {}
+ if property.properties == nil then
+ return output
+ end
+ for k, v in pairs(property.properties) do
+ local type = string.lower(v.type)
+ if type == "array" then
+ output[k] = gen_array(v)
+ elseif type == "object" then
+ output[k] = gen_object(v)
+ else
+ output[k] = get_base(v)
+ end
+ end
+ return output
+end
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local type = string.lower(v.type)
+ for i = 1, n do
+ if type == "array" then
+ table.insert(output, gen_array(v))
+ elseif type == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, get_base(v))
+ end
+ end
+ return output
+end
+
+function get_base(property)
+ local type = string.lower(property.type)
+ local example = property.example
+ if type == "string" then
+ return gen_string(example)
+ elseif type == "number" then
+ return gen_number(example)
+ elseif type == "integer" then
+ return gen_integer(example)
+ elseif type == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 2)
Review comment:
The range ([0, 2]) is incorrect?
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
Review comment:
Let's move plugin_name down to _M
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002468169
@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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1003308570
The mail link is https://lists.apache.org/thread/xbkwz2k1dpoxgpcs7gm8t8d6ddvq1dto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1003299223
@shuaijinchao @juzhiyuan
Thank you.
I already send a proposal to the mailing list.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r780914436
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -43,7 +43,8 @@ Mock API插件,绑定该插件后将随机返回指定格式的mock数据,
| content_type | string | 可选 | | | 返回的响应头的Content-Type。 |
| response_example| string | 可选 | | | 返回的响应体,与response_schema二选一。 |
| response_schema | object | 可选 | | | 指定响应的json schema对象,未指定response_body时生效,具体结构看后文说明。 |
-支持的字段类型:string,number,integer,boolean,object,array。
+
+支持的字段类型:`string`, `number`, `integer`, `boolean`, `object,array`
Review comment:
emm,should be `object`,`array`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery removed a comment on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery removed a comment on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018213459
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
@juzhiyu
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018213459
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
@juzhiyu
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r791386377
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
Review comment:
> Better to use `enum` to define the support type.
@starsz because the content_type support two parts type and charset such as: "application/json" and "application/json;charset=utf8",so it's hard to define a enum~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1025073291
> Let's merge master to make CI pass?
@spacewander done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] bisakhmondal commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r794227851
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,402 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
Review comment:
We don't need it here. Please remove it.
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,402 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
Review comment:
Hey, could you please update it?
reference: https://github.com/apache/apisix/pull/6216/files
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] bisakhmondal commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r802626941
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,402 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
Review comment:
Hi @Drery, It seems I somehow missed your ping, really sorry for that. It's really simple. Do give it a try, I can guide you.
A bit of context: here we are using Perl scripting (a [fork](https://github.com/iresty/test-nginx) of test-nginx framework). As you can see the location path for each test is `/t` - we don't need to define `--- request` block for every unit tests. Instead we can manually inject it at the very beginning.
What the block actually does (see my inline comments)?
```perl
add_block_preprocessor(sub {
my ($block) = @_;
# this snippet sets the `request` field if not set from an unit test. also it gives you the flexibility to override in certain tests (say POST, PUT etc)
if (!$block->request) {
$block->set_value("request", "GET /t");
}
# here also, you don't have to manually put `--- no_error_log` block for each unit tests. It will inject it automatically.
if (!$block->error_log && !$block->no_error_log) {
$block->set_value("no_error_log", "[error]\n[alert]");
}
});
```
If you are really interested to understand how the whole thing works you can run a test file and side by side compare `t/servroot/conf/nginx.conf` with https://github.com/apache/apisix/blob/master/t/APISIX.pm
An extra example:
If you notice, the test config is written inside `--- config` block. But you may wonder, how we are bridging the gap between writing the tests (in Perl) and running those tests in a test environment (requires an nginx.conf with some Lua blocks). This is where the Perl scripting helps - before running any tests we generate a new nginx.conf from the `--- config` set inside the test case, please take a look
https://github.com/apache/apisix/blob/defafb1e426c4ffe4aa740c9bb9ad26992fa2c2e/t/APISIX.pm#L641-L650
Hope this helps. Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] shuaijinchao commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r775853230
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
+function _M.access(conf)
+ local output = gen_object(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
Review comment:
Why must it be json? I think the configuration will be better.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002462381
Hi I just added the discussion to this PR https://github.com/apache/apisix/discussions/5896
@Drery would like to contribute the Mocking plugin as Kong does 😄 Do we need a proposal or design guide?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r776207099
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
+function _M.access(conf)
+ local output = gen_object(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
Review comment:
> 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.
thank for your sugguest. i commit the PR just want to know how do you think about this idea.
if you approve this idea, i will enrich this plugin to support more scenes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery removed a comment on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery removed a comment on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018215189
> Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
emm, i have no access to resolve conflicts.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r806515999
##########
File path: docs/zh/latest/plugins/mocking.md
##########
@@ -0,0 +1,234 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## 目录
+
+- [**简介**](#简介)
+- [**属性**](#属性)
+- [**如何启用**](#如何启用)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 简介
+
+Mock API插件,绑定该插件后将随机返回指定格式的mock数据,不再转发到后端。
Review comment:
Please add space around English word
##########
File path: conf/config-default.yaml
##########
@@ -329,7 +329,8 @@ plugins: # plugin list (sorted by priority)
- request-id # priority: 11010
- fault-injection # priority: 11000
- serverless-pre-function # priority: 10000
- #- batch-requests # priority: 4010
+ - mocking # priority: 9900
+# - batch-requests # priority: 4010
Review comment:
Please note the indentation. And maybe we should put it together with `fault-injection` instead of `serverless-pre-function`?
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,364 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 2: hit route(return response example:"hello world")
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+hello world
+
+
+
+=== TEST 3: set route(return response schema: string case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"string",
+ "example":"hello"
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 4: hit route(return response schema: string case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":"hello"}
+
+
+
+=== TEST 5: set route(return response schema: integer case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"integer",
+ "example":4
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 6: hit route(return response schema: integer case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":4}
+
+
+
+=== TEST 7: set route(return response schema: number case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"number",
+ "example":5.5
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 8: hit route(return response schema: number case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":5.5}
+
+
+
+=== TEST 9: set route(return response schema: boolean case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"boolean",
+ "example":true
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 10: hit route(return response schema: boolean case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":true}
+
+
+
+=== TEST 11: set route(return response schema: object case)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_schema": {
+ "type": "object",
+ "properties": {
+ "field1":{
+ "type":"object"
+ }
+ }
+ }
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
+--- response_body
+passed
+
+
+
+=== TEST 12: hit route(return response schema: object case)
+--- request
+GET /hello
+--- error_code: 200
+--- response_body chomp
+{"field1":{}}
+
+
+
+=== TEST 13: set route(return response header: application/json)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_example": "{\"field1\":{}}"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
Review comment:
Duplicate field
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
Review comment:
```suggestion
| response_schema | object | optional | | | The jsonschema object for the response is specified. This property takes effect if the `response_example` is not specified |
```
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,364 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- error_code: 200
Review comment:
This field is set by default. Please remove it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1018158113
Hi @Drery, there have 2 conflicts in Docs, please resolve them :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1019758947
> @spacewander @leslie-tsang hello, what does this error mains?  the workflow link is: https://github.com/apache/apisix/runs/4911836590?check_suite_focus=true
Hello there, please refer to [nginx-test-filter](https://metacpan.org/pod/Test::Nginx::Socket#Filters), may help
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] leslie-tsang commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
leslie-tsang commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807507294
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
Review comment:
```suggestion
Mock API plugin, When the plugin is bound, it returns random mock data in the specified format and is no longer forwarded to the upstreams.
```
Would be better ?
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jsonschema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `integer`, `boolean`, `object`, `array`
+Base data types (`string`, `number`, `integer`, `Boolean`) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a `jsonschema` example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this `jsonschema`:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## How To Enable
+
+Here, use `route` as an example (`service` is used in the same way) to enable the `mocking` plugin on the specified `route`.
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## Test Plugin
+
+the `mocking` plugin is configured as follows:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+Use curl to access:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: APISIX/2.10.0
Review comment:
```suggestion
...
Content-Type: application/json;charset=utf8
x-mock-by: APISIX/2.10.0
...
```
There is no need to include irrelevant information.
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jsonschema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `integer`, `boolean`, `object`, `array`
+Base data types (`string`, `number`, `integer`, `Boolean`) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a `jsonschema` example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this `jsonschema`:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## How To Enable
+
+Here, use `route` as an example (`service` is used in the same way) to enable the `mocking` plugin on the specified `route`.
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## Test Plugin
+
+the `mocking` plugin is configured as follows:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+Use curl to access:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: APISIX/2.10.0
+
+{"a":1,"b":2}
+```
+
+## Disable Plugin
+
+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:
Review comment:
```suggestion
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 ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807511993
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jsonschema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `integer`, `boolean`, `object`, `array`
+Base data types (`string`, `number`, `integer`, `Boolean`) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a `jsonschema` example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this `jsonschema`:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## How To Enable
+
+Here, use `route` as an example (`service` is used in the same way) to enable the `mocking` plugin on the specified `route`.
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## Test Plugin
+
+the `mocking` plugin is configured as follows:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+Use curl to access:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: APISIX/2.10.0
+
+{"a":1,"b":2}
+```
+
+## Disable Plugin
+
+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:
Review comment:
emm, I just copy this from other plugins~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1043987746
@spacewander @leslie-tsang Thanks, I resolved the conflicts, please have a review again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1023036387
@spacewander @starsz hello, the error is still exists, please pay attention to this issue~

the link is: https://github.com/apache/apisix/runs/4963622807?check_suite_focus=true
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] shuaijinchao commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002044382
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] shuaijinchao commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r775853230
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,148 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ return true, nil
+end
+
+function _M.access(conf)
+ local output = gen_object(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
Review comment:
I think the Moking plugin should not only support JSON.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r786727680
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
Review comment:
@leslie-tsang
the `gen_array` function call the `gen_object` function, and the `gen_object` function also call the `gen_array` function.
so i can't define the two function as local.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r775690331
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
Review comment:
The response_schema is checked in `core.schema.check` so there is no need to check it again.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
+ if conf.response_schema then
Review comment:
Ditto
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
+ if conf.response_schema then
+ local output = genObject(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
+ return 200, core.utils.resolve_var(core.json.encode(output))
+ end
+end
+
+function genObject(property)
+ local output = {}
+ if property.properties == nil then
+ return output
+ end
+ for k, v in pairs(property.properties) do
+ if v.type == "array" then
+ output[k] = genArray(v)
+ elseif v.type == "object" then
+ output[k] = genObject(v)
+ else
+ output[k] = genBase(v)
+ end
+ end
+ return output
+end
+
+function genArray(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ for i = 1, n do
+ if type == "array" then
+ table.insert(output, genArray(v))
+ elseif type == "object" then
+ table.insert(output, genObject(v))
+ else
+ table.insert(output, genBase(v))
+ end
+ end
+ return output
+end
+
+function genBase(property)
+ local type = property.type
+ local example = property.example
+ if type == "string" then
+ return genString(example)
+ elseif type == "number" then
+ return genNumber(example)
+ elseif type == "integer" then
+ return genInteger(example)
+ elseif type == "boolean" then
+ return genBoolean(example)
+ end
+ return nil
+end
+
+function genString(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local t = {
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
+ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
+ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
+ }
+ local n = math.random(1, 10)
+ local s = ""
+ for i = 1, n do
+ s = s .. t[math.random(#t)]
+ end ;
Review comment:
Please remove the `;`
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
+ if conf.response_schema then
+ local output = genObject(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
+ return 200, core.utils.resolve_var(core.json.encode(output))
+ end
+end
+
+function genObject(property)
+ local output = {}
+ if property.properties == nil then
+ return output
+ end
+ for k, v in pairs(property.properties) do
+ if v.type == "array" then
+ output[k] = genArray(v)
+ elseif v.type == "object" then
+ output[k] = genObject(v)
+ else
+ output[k] = genBase(v)
+ end
+ end
+ return output
+end
+
+function genArray(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ for i = 1, n do
+ if type == "array" then
+ table.insert(output, genArray(v))
+ elseif type == "object" then
+ table.insert(output, genObject(v))
+ else
+ table.insert(output, genBase(v))
+ end
+ end
+ return output
+end
+
+function genBase(property)
+ local type = property.type
+ local example = property.example
+ if type == "string" then
+ return genString(example)
+ elseif type == "number" then
+ return genNumber(example)
+ elseif type == "integer" then
+ return genInteger(example)
+ elseif type == "boolean" then
+ return genBoolean(example)
+ end
+ return nil
+end
+
+function genString(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local t = {
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
+ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
+ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
+ }
+ local n = math.random(1, 10)
+ local s = ""
+ for i = 1, n do
+ s = s .. t[math.random(#t)]
Review comment:
We can use `table.concat` to generate a string
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
+ if conf.response_schema then
+ local output = genObject(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
+ return 200, core.utils.resolve_var(core.json.encode(output))
+ end
+end
+
+function genObject(property)
Review comment:
Please use underscore style for the name
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
+ if conf.response_schema then
+ local output = genObject(conf.response_schema)
+ ngx.header["Content-Type"] = "application/json"
+ return 200, core.utils.resolve_var(core.json.encode(output))
+ end
+end
+
+function genObject(property)
+ local output = {}
+ if property.properties == nil then
+ return output
+ end
+ for k, v in pairs(property.properties) do
+ if v.type == "array" then
+ output[k] = genArray(v)
+ elseif v.type == "object" then
+ output[k] = genObject(v)
+ else
+ output[k] = genBase(v)
+ end
+ end
+ return output
+end
+
+function genArray(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ for i = 1, n do
+ if type == "array" then
+ table.insert(output, genArray(v))
+ elseif type == "object" then
+ table.insert(output, genObject(v))
+ else
+ table.insert(output, genBase(v))
+ end
+ end
+ return output
+end
+
+function genBase(property)
+ local type = property.type
+ local example = property.example
+ if type == "string" then
+ return genString(example)
+ elseif type == "number" then
+ return genNumber(example)
+ elseif type == "integer" then
+ return genInteger(example)
+ elseif type == "boolean" then
+ return genBoolean(example)
+ end
+ return nil
+end
+
+function genString(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local t = {
Review comment:
We can use ASCII to generate the target chars? There is no need to hardcode a table.
Like `string.char(math.random(n, m))`
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,160 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local plugin_name = "mocking"
+
+local schema = {
+ type = "object",
+ properties = {
+ response_schema = { type = "object" }
+ },
+ required = { "response_schema" }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = plugin_name,
+ schema = schema,
+}
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.response_schema then
+ ok, err = core.schema.valid(conf.response_schema)
+ if not ok then
+ return false, err
+ end
+ end
+
+ return true, nil
+end
+
+function _M.rewrite(conf)
Review comment:
Please use `access` for the method name.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] juzhiyuan commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1002471837
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?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] bisakhmondal commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
bisakhmondal commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r791053166
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
Review comment:
Too many operations. Better do this
```lua
local m = ngx.re.match(content_type, "([ -~]*);([ -~]*)")
-- m[1] typ, m[2] charset
if #m == 2 then
return m[1], m[2]
end
return content_type
```
Also, please be consistent about return type. Line 68 charset is nil, but line 58 2nd return arg is ""
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
Review comment:
```suggestion
if example and type(example) == "string" then
```
would do just fine.
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+
+local gen_array, gen_object
+
+function gen_array(property)
+ local output = {}
+ if property.items == nil then
+ return nil
+ end
+ local v = property.items
+ local n = math.random(1, 3)
+ local typ = string.lower(v.type)
+ for i = 1, n do
+ if typ == "array" then
+ table.insert(output, gen_array(v))
+ elseif typ == "object" then
+ table.insert(output, gen_object(v))
+ else
+ table.insert(output, gen_base(v))
+ end
+ end
+ return output
+end
+
+function gen_object(property)
+ local output = {}
+ if property.properties == nil then
Review comment:
```suggestion
if not property.properties then
```
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
Review comment:
same and take care of similar rest of them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r787548966
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,204 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core = require("apisix.core")
+local ngx = ngx
+local xml2lua = require("xml2lua")
+local string = string
+local type = type
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer" },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 1 },
+ -- specify response content type,support application/xml,text/plain and application/json,default application/json
+ content_type = { type = "string", default = "application/json" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
+ name = "mocking",
+ schema = schema,
+}
+
+local function parse_content_type(content_type)
+ if not content_type then
+ return "", ""
+ end
+ local sep_idx = string.find(content_type, ";")
+ local typ, charset
+ if sep_idx then
+ typ = string.sub(content_type, 1, sep_idx - 1)
+ charset = string.sub(content_type, sep_idx + 1)
+ else
+ typ = content_type
+ end
+ return typ, charset
+end
+
+function _M.check_schema(conf)
+ local ok, err = core.schema.check(schema, conf)
+ if not ok then
+ return false, err
+ end
+
+ if conf.content_type == "" then
+ conf.content_type = "application/json;charset=utf8"
+ end
+ local typ, _ = parse_content_type(conf.content_type)
+ if typ ~= "application/xml" and
+ typ ~= "application/json" and
+ typ ~= "text/plain" and
+ typ ~= "text/html" and
+ typ ~= "text/xml" then
+ return false, "unsupported content type!"
+ end
+ return true
+end
+
+local function gen_string(example)
+ if example ~= nil and type(example) == "string" then
+ return example
+ end
+ local n = math.random(1, 10)
+ local list = {}
+ for i = 1, n do
+ table.insert(list, string.char(math.random(97, 122)))
+ end
+ return table.concat(list)
+end
+
+local function gen_number(example)
+ if example ~= nil and type(example) == "number" then
+ return example
+ end
+ return math.random() * 10000
+end
+
+local function gen_integer(example)
+ if example ~= nil and type(example) == "number" then
+ return math.floor(example)
+ end
+ return math.random(1, 10000)
+end
+
+local function gen_boolean(example)
+ if example ~= nil and type(example) == "boolean" then
+ return example
+ end
+ local r = math.random(0, 1)
+ if r == 0 then
+ return false
+ end
+ return true
+end
+
+local function gen_base(property)
+ local typ = string.lower(property.type)
+ local example = property.example
+ if typ == "string" then
+ return gen_string(example)
+ elseif typ == "number" then
+ return gen_number(example)
+ elseif typ == "integer" then
+ return gen_integer(example)
+ elseif typ == "boolean" then
+ return gen_boolean(example)
+ end
+ return nil
+end
+
+function gen_array(property)
Review comment:
> Hello there, Please refer to [Non-Global Functions](https://www.lua.org/pil/6.2.html).
@leslie-tsang It used to be that way.Thank you for your suggestion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r795128847
##########
File path: t/plugin/mocking.t
##########
@@ -0,0 +1,402 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+ if ($ENV{TEST_NGINX_CHECK_LEAK}) {
+ $SkipReason = "unavailable for the hup tests";
+
+ } else {
+ $ENV{TEST_NGINX_USE_HUP} = 1;
+ undef $ENV{TEST_NGINX_USE_STAP};
+ }
+}
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: set route(return response example:"hello world")
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "text/plain",
+ "response_status": 200,
+ "response_example": "hello world"
+ }
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- error_code: 200
+--- response_body
+passed
+--- no_error_log
Review comment:
>
@bisakhmondal sorry, I dont know how to update, could you please help me to update? I can give you the permission.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery edited a comment on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery edited a comment on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1022943852
@starsz hello, there's a error when run the unit test, but it's not produce by my code, please have a look at this.

it's should be produced by this comment:

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] Drery commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
Drery commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1022943852
@starsz hello, there's a error when run the unit test, but it's not produce by my code, please have a look at this.

it's should be produced by this comment:

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r807497904
##########
File path: apisix/plugins/mocking.lua
##########
@@ -0,0 +1,224 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements. See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local ngx = ngx
+local string = string
+local type = type
+local table = table
+local math = math
+local pairs = pairs
+
+local core = require("apisix.core")
+local xml2lua = require("xml2lua")
+local json = core.json
+local ngx_re = ngx.re
+
+local support_content_type = {
+ ["application/xml"] = true,
+ ["application/json"] = true,
+ ["text/plain"] = true,
+ ["text/html"] = true,
+ ["text/xml"] = true
+}
+
+local schema = {
+ type = "object",
+ properties = {
+ -- specify response delay time,default 0ms
+ delay = { type = "integer", default = 0 },
+ -- specify response status,default 200
+ response_status = { type = "integer", default = 200, minimum = 100 },
+ -- specify response content type, support application/xml, text/plain
+ -- and application/json, default application/json
+ content_type = { type = "string", default = "application/json;charset=utf8" },
+ -- 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" },
+ with_mock_header = { type = "boolean", default = true }
+ },
+ anyOf = {
+ { required = { "response_example" } },
+ { required = { "response_schema" } }
+ }
+}
+
+local _M = {
+ version = 0.1,
+ priority = 9900,
Review comment:
Don't forget to update the priority in the code
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] starsz commented on a change in pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
starsz commented on a change in pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#discussion_r801625349
##########
File path: docs/en/latest/plugins/mocking.md
##########
@@ -0,0 +1,236 @@
+---
+title: mocking
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+Mock API plugin,Binding the plugin returns random mock data in the specified format and is no longer forwarded to the back end.
+
+## Attributes
+
+| Name | Type | Requirement | Default | Valid | Description |
+| ------------- | -------| ----- | ----- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
+| delay | integer | optional | | | Delay return time, in seconds |
+| response_status | integer| optional | 200 | | response http status code |
+| content_type | string | optional | application/json | | response header Content-Type。 |
+| response_example| string | optional | | | response body |
+| response_schema | object | optional | | | The jSON-schema object for the response is specified. This property takes effect if the `response_example` is not specified |
+| with_mock_header | boolean | optional | true | | Whether to return the response header: "x-mock-by: APISIX/{version}", returned by default, false does not return |
+
+Supported field types: `string`, `number`, `integer`, `boolean`, `object`, `array`
+Base data types (string, number, integer, Boolean) through configuration example attribute to specify the generated response value, random return not configured.
+Here is a JSON-Schema example:
+
+```json
+{
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+}
+```
+
+Here are the return objects that might be generated by this JSON-Schema:
+
+```json
+{
+ "field1": 123.12,
+ "field3": {
+ "field3_1": "LCFE0",
+ "field3_2": {
+ "field3_2_1": true,
+ "field3_2_2": [
+ 155,
+ 155
+ ]
+ }
+ },
+ "field0": "abcd",
+ "field2": [
+ "sC"
+ ]
+}
+```
+
+## How To Enable
+
+Here, use `route` as an example (`service` is used in the same way) to enable the `mocking` plugin on the specified `route`.
+
+```shell
+curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+ "methods": ["GET"],
+ "uri": "/index.html",
+ "plugins": {
+ "mocking": {
+ "delay": 1,
+ "content_type": "application/json",
+ "response_status": 200,
+ "response_schema": {
+ "properties":{
+ "field0":{
+ "example":"abcd",
+ "type":"string"
+ },
+ "field1":{
+ "example":123.12,
+ "type":"number"
+ },
+ "field3":{
+ "properties":{
+ "field3_1":{
+ "type":"string"
+ },
+ "field3_2":{
+ "properties":{
+ "field3_2_1":{
+ "example":true,
+ "type":"boolean"
+ },
+ "field3_2_2":{
+ "items":{
+ "example":155.55,
+ "type":"integer"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ },
+ "type":"object"
+ },
+ "field2":{
+ "items":{
+ "type":"string"
+ },
+ "type":"array"
+ }
+ },
+ "type":"object"
+ }
+ }
+ },
+ "upstream": {
+ "type": "roundrobin",
+ "nodes": {
+ "127.0.0.1:1980": 1
+ }
+ }
+}'
+```
+
+## Test Plugin
+
+the `mocking` plugin is configured as follows:
+
+```json
+{
+ "delay":0,
+ "content_type":"",
+ "with_mock_header":true,
+ "response_status":201,
+ "response_example":"{\"a\":1,\"b\":2}"
+}
+```
+
+Use curl to access:
+
+```shell
+$ curl http://127.0.0.1:9080/test-mock -i
+HTTP/1.1 201 Created
+Date: Fri, 14 Jan 2022 11:49:34 GMT
+Content-Type: application/json;charset=utf8
+Transfer-Encoding: chunked
+Connection: keep-alive
+x-mock-by: APISIX/2.10.0
+Server: TAPISIX/2.10.0
Review comment:
+1
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on pull request #5940: feat: mocking plugin
Posted by GitBox <gi...@apache.org>.
spacewander commented on pull request #5940:
URL: https://github.com/apache/apisix/pull/5940#issuecomment-1038687155
Err...
It is already done 5 days ago. Please see https://github.com/apache/apisix/pull/5940#discussion_r802626941
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org