Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: mocking plugin #5940

Merged
merged 63 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
befb95c
feat: mocking plugin
Dec 27, 2021
ff1b662
fix: fix code lint
Dec 28, 2021
1c38b88
feat: support delay,status,content-type and response_example
Jan 6, 2022
2f09041
fix: random gen boolean
Jan 6, 2022
c5c7449
Update docs/zh/latest/plugins/mocking.md
Drery Jan 10, 2022
ab7dd01
Update docs/zh/latest/plugins/mocking.md
Drery Jan 10, 2022
8b15af7
fix: coding lint
Jan 10, 2022
4a27281
feat: with mock header
Jan 10, 2022
cefcaf2
feat: with mock header
Jan 10, 2022
41df69a
fix: update md
Jan 10, 2022
f807386
fix: update md
Jan 10, 2022
f86fd49
feat: add xml2lua rock spec
Jan 14, 2022
0a02f27
fix: update md
Jan 14, 2022
a351ded
Update apisix/plugins/mocking.lua
Drery Jan 18, 2022
9848555
Update docs/zh/latest/plugins/mocking.md
Drery Jan 18, 2022
9190d27
Update docs/zh/latest/plugins/mocking.md
Drery Jan 18, 2022
27d6d1b
Update apisix/plugins/mocking.lua
Drery Jan 18, 2022
439cf14
Update apisix/plugins/mocking.lua
Drery Jan 18, 2022
0c296b5
feat: local function
Jan 19, 2022
0d92b43
Merge branch 'master' of github.com:Drery/apisix
Jan 19, 2022
5fb3647
fix: fix code lint
Jan 20, 2022
a62f7a8
feat: add en md
Jan 21, 2022
e66a680
fix: fix conflicts
Jan 21, 2022
db37e2d
feat: add plugin in config-default.yaml
Jan 21, 2022
71e9af1
fix: add mocking plugin in plugin.t
Jan 23, 2022
a42a7fe
feat: add mocking test
Jan 23, 2022
67e5496
feat: update mocking test
Jan 23, 2022
ebfe46f
feat: update mocking
Jan 24, 2022
b894f95
feat: remove chomp
Jan 24, 2022
1454033
Update apisix/plugins/mocking.lua
Drery Jan 25, 2022
6ff7f11
Update apisix/plugins/mocking.lua
Drery Jan 25, 2022
ce39d6c
Update apisix/plugins/mocking.lua
Drery Jan 25, 2022
7f900bb
Update apisix/plugins/mocking.lua
Drery Jan 25, 2022
08a0c55
feat: fix some issue
Jan 25, 2022
6fb6b0c
feat: merge update
Jan 25, 2022
8f374f3
Update apisix/plugins/mocking.lua
Drery Jan 26, 2022
514f0af
feat: fix lint
Jan 26, 2022
bebc44a
Merge branch 'master' of github.com:Drery/apisix
Jan 26, 2022
6fd29c6
feat: merge upstram master
Jan 26, 2022
e525a84
fix test case
Jan 27, 2022
ed9ae66
Merge remote-tracking branch 'upstream/master'
Jan 27, 2022
10fdc1d
remove array test
Jan 27, 2022
d693611
Merge remote-tracking branch 'upstream/master'
Jan 30, 2022
968fdb2
feat: merge upstream
Jan 30, 2022
43ac0de
Merge remote-tracking branch 'upstream/master'
Feb 8, 2022
5a54066
fix: batch-requests plugin
Feb 8, 2022
288f7e0
fix: change tapisix to apisix in md file
Feb 9, 2022
49fdd59
gen by property
Feb 14, 2022
7da4aa6
remove batch-requests in plugin.t
Feb 14, 2022
2f2cffc
Merge remote-tracking branch 'upstream/master'
Feb 14, 2022
f5d9529
fix: add response in mocking.t
Feb 15, 2022
9d511c6
Update docs/en/latest/plugins/mocking.md
Drery Feb 15, 2022
f87bf28
fix: format code and doc
Feb 16, 2022
65447a2
Merge branch 'master' of github.com:Drery/apisix
Feb 16, 2022
95c7678
fix: format code and doc
Feb 16, 2022
e88b7d5
feat: update plugin priority
Feb 16, 2022
6f7a256
feat: sort
Feb 16, 2022
396d2a9
feat: update priority
Feb 16, 2022
9e0b57b
Update docs/en/latest/plugins/mocking.md
Drery Feb 16, 2022
e7cfd2f
Update docs/en/latest/plugins/mocking.md
Drery Feb 16, 2022
45047e5
feat: fix conflict
Feb 18, 2022
502a69b
Merge branch 'master' of github.com:Drery/apisix
Feb 18, 2022
b1b789c
Update conf/config-default.yaml
spacewander Feb 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 224 additions & 0 deletions apisix/plugins/mocking.lua
Original file line number Diff line number Diff line change
@@ -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 core = require("apisix.core")
local xml2lua = require("xml2lua")
Copy link
Member

Choose a reason for hiding this comment

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

Let's put the require at the top, and sort all the xxx = xxx localized imports by their names.


local json = core.json
local math = math
local ngx = ngx
local ngx_re = ngx.re
local pairs = pairs
local string = string
local table = table
local type = type

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 = 10900,
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, "([ -~]*);([ -~]*)", "jo")
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)
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 gen_array, gen_object, gen_by_property

function gen_array(property)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function gen_array(property)
local function gen_array(property)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Hello there, Please refer to Non-Global Functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello there, Please refer to Non-Global Functions.

@leslie-tsang It used to be that way.Thank you for your suggestion.

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
table.insert(output, gen_by_property(v))
end
return output
end


function gen_object(property)
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@leslie-tsang Hi, how can I run code checks in my own fork repository

Copy link
Member

Choose a reason for hiding this comment

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

fork APISIX repo, and enable GitHub action. submit a PR to your own forks.

local output = {}
if not property.properties then
return output
end
for k, v in pairs(property.properties) do
output[k] = gen_by_property(v)
end
return output
end


function gen_by_property(property)
local typ = string.lower(property.type)
local example = property.example

if typ == "array" then
return gen_array(property)
end

if typ == "object" then
return gen_object(property)
end

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


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)
if typ == "application/xml" or typ == "text/xml" then
response_content = xml2lua.toXml(output, "data")

elseif typ == "application/json" or typ == "text/plain" then
response_content = json.encode(output)

else
core.log.error("json schema body only support xml and json content type")
end
end

ngx.header["Content-Type"] = conf.content_type
if conf.with_mock_header then
ngx.header["x-mock-by"] = "APISIX/" .. core.version.VERSION
end

if conf.delay > 0 then
ngx.sleep(conf.delay)
end
return conf.response_status, core.utils.resolve_var(response_content)
end

return _M
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ 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
- cors # priority: 4000
Expand Down
3 changes: 2 additions & 1 deletion docs/en/latest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"plugins/proxy-rewrite",
"plugins/grpc-transcode",
"plugins/grpc-web",
"plugins/fault-injection"
"plugins/fault-injection",
"plugins/mocking"
]
},
{
Expand Down
Loading