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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to