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

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


Reply via email to