2019-10-31 09:27:28 +08:00
|
|
|
--
|
|
|
|
-- 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.
|
|
|
|
--
|
2020-06-13 16:10:27 +08:00
|
|
|
local http = require("resty.http")
|
2020-11-30 10:32:35 +08:00
|
|
|
local json = require("toolkit.json")
|
2020-09-19 19:07:39 +08:00
|
|
|
local core = require("apisix.core")
|
2020-06-13 16:10:27 +08:00
|
|
|
local aes = require "resty.aes"
|
|
|
|
local ngx_encode_base64 = ngx.encode_base64
|
2020-11-18 23:58:36 +08:00
|
|
|
local str_find = core.string.find
|
2020-06-13 16:10:27 +08:00
|
|
|
local dir_names = {}
|
2019-06-13 12:01:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
|
|
|
|
local function com_tab(pattern, data, deep)
|
|
|
|
deep = deep or 1
|
|
|
|
|
|
|
|
for k, v in pairs(pattern) do
|
|
|
|
dir_names[deep] = k
|
|
|
|
|
2020-03-13 15:56:38 +08:00
|
|
|
if v == ngx.null then
|
|
|
|
v = nil
|
|
|
|
end
|
|
|
|
|
2020-12-23 16:40:19 +08:00
|
|
|
if type(v) == "table" and data[k] then
|
2019-06-13 12:01:36 +08:00
|
|
|
local ok, err = com_tab(v, data[k], deep + 1)
|
|
|
|
if not ok then
|
|
|
|
return false, err
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif v ~= data[k] then
|
|
|
|
return false, "path: " .. table.concat(dir_names, "->", 1, deep)
|
2019-06-25 06:36:52 +08:00
|
|
|
.. " expect: " .. tostring(v) .. " got: "
|
|
|
|
.. tostring(data[k])
|
2019-06-13 12:01:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-08-19 14:02:33 +08:00
|
|
|
local methods = {
|
|
|
|
[ngx.HTTP_GET ] = "GET",
|
|
|
|
[ngx.HTTP_HEAD ] = "HEAD",
|
|
|
|
[ngx.HTTP_PUT ] = "PUT",
|
|
|
|
[ngx.HTTP_POST ] = "POST",
|
|
|
|
[ngx.HTTP_DELETE ] = "DELETE",
|
|
|
|
[ngx.HTTP_OPTIONS] = "OPTIONS",
|
|
|
|
[ngx.HTTP_PATCH] = "PATCH",
|
2019-09-17 16:57:51 +08:00
|
|
|
[ngx.HTTP_TRACE] = "TRACE",
|
2019-08-19 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-24 09:10:11 +08:00
|
|
|
function _M.test_ipv6(uri)
|
|
|
|
local sock = ngx.socket.tcp()
|
|
|
|
local ok, err = sock:connect("[::1]", 12345)
|
|
|
|
if not ok then
|
|
|
|
ngx.say("failed to connect: ", err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("connected: ", ok)
|
|
|
|
|
|
|
|
local req = "GET " .. uri .. " HTTP/1.0\r\nHost: localhost\r\n"
|
|
|
|
.. "Connection: close\r\n\r\n"
|
|
|
|
-- req = "OK"
|
|
|
|
-- ngx.log(ngx.WARN, "req: ", req)
|
|
|
|
|
|
|
|
local bytes, err = sock:send(req)
|
|
|
|
if not bytes then
|
|
|
|
ngx.say("failed to send request: ", err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("request sent: ", bytes)
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local line, err, part = sock:receive()
|
|
|
|
if line then
|
2021-05-10 11:59:51 +08:00
|
|
|
if line ~= "" then
|
|
|
|
ngx.say("received: ", line)
|
|
|
|
else
|
|
|
|
ngx.say("received:")
|
|
|
|
end
|
2019-08-24 09:10:11 +08:00
|
|
|
|
|
|
|
else
|
|
|
|
ngx.say("failed to receive a line: ", err, " [", part, "]")
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ok, err = sock:close()
|
|
|
|
ngx.say("close: ", ok, " ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-04-12 09:31:58 +08:00
|
|
|
function _M.comp_tab(left_tab, right_tab)
|
2020-10-26 21:50:51 +08:00
|
|
|
local err
|
2020-05-12 15:50:52 +08:00
|
|
|
dir_names = {}
|
|
|
|
|
2020-12-10 15:51:00 +08:00
|
|
|
local _
|
2020-04-12 09:31:58 +08:00
|
|
|
if type(left_tab) == "string" then
|
2020-12-10 15:51:00 +08:00
|
|
|
left_tab, _, err = json.decode(left_tab)
|
2020-10-26 21:50:51 +08:00
|
|
|
if not left_tab then
|
|
|
|
return false, "failed to decode expected data: " .. err
|
|
|
|
end
|
2020-04-12 09:31:58 +08:00
|
|
|
end
|
|
|
|
if type(right_tab) == "string" then
|
2020-12-10 15:51:00 +08:00
|
|
|
right_tab, _, err = json.decode(right_tab)
|
2020-10-26 21:50:51 +08:00
|
|
|
if not right_tab then
|
|
|
|
return false, "failed to decode expected data: " .. err
|
|
|
|
end
|
2020-04-12 09:31:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local ok, err = com_tab(left_tab, right_tab)
|
|
|
|
if not ok then
|
2020-05-12 15:50:52 +08:00
|
|
|
return false, err
|
2020-04-12 09:31:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-20 20:42:10 +08:00
|
|
|
local function set_yaml(fn, data)
|
2020-11-15 19:36:53 +08:00
|
|
|
local profile = os.getenv("APISIX_PROFILE")
|
|
|
|
if profile then
|
2021-02-20 20:42:10 +08:00
|
|
|
fn = fn .. "-" .. profile .. ".yaml"
|
2020-11-15 19:36:53 +08:00
|
|
|
else
|
2021-02-20 20:42:10 +08:00
|
|
|
fn = fn .. ".yaml"
|
2020-11-15 19:36:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local f = assert(io.open(os.getenv("TEST_NGINX_HTML_DIR") .. "/../conf/" .. fn, 'w'))
|
2020-11-03 13:46:24 +08:00
|
|
|
assert(f:write(data))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-20 20:42:10 +08:00
|
|
|
function _M.set_config_yaml(data)
|
|
|
|
set_yaml("config", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function _M.set_apisix_yaml(data)
|
|
|
|
set_yaml("apisix", data)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-06-02 09:26:24 +08:00
|
|
|
function _M.test(uri, method, body, pattern, headers)
|
|
|
|
if not headers then
|
|
|
|
headers = {}
|
|
|
|
end
|
|
|
|
if not headers["Content-Type"] then
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
end
|
|
|
|
|
2019-07-28 11:10:30 +08:00
|
|
|
if type(body) == "table" then
|
|
|
|
body = json.encode(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(pattern) == "table" then
|
|
|
|
pattern = json.encode(pattern)
|
|
|
|
end
|
|
|
|
|
2019-08-19 14:02:33 +08:00
|
|
|
if type(method) == "number" then
|
|
|
|
method = methods[method]
|
|
|
|
end
|
|
|
|
|
|
|
|
local httpc = http.new()
|
|
|
|
-- https://github.com/ledgetech/lua-resty-http
|
|
|
|
uri = ngx.var.scheme .. "://" .. ngx.var.server_addr
|
|
|
|
.. ":" .. ngx.var.server_port .. uri
|
2020-04-12 09:31:58 +08:00
|
|
|
local res, err = httpc:request_uri(uri,
|
2019-08-19 14:02:33 +08:00
|
|
|
{
|
|
|
|
method = method,
|
|
|
|
body = body,
|
|
|
|
keepalive = false,
|
2020-06-02 09:26:24 +08:00
|
|
|
headers = headers,
|
2019-08-19 14:02:33 +08:00
|
|
|
}
|
|
|
|
)
|
2019-08-24 09:10:11 +08:00
|
|
|
if not res then
|
|
|
|
ngx.log(ngx.ERR, "failed http: ", err)
|
|
|
|
return nil, err
|
|
|
|
end
|
2019-06-13 12:01:36 +08:00
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
if res.status >= 300 then
|
2019-06-13 12:01:36 +08:00
|
|
|
return res.status, res.body
|
|
|
|
end
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
if pattern == nil then
|
|
|
|
return res.status, "passed", res.body
|
|
|
|
end
|
2020-04-12 09:31:58 +08:00
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
local res_data = json.decode(res.body)
|
2020-04-12 09:31:58 +08:00
|
|
|
local ok, err = _M.comp_tab(pattern, res_data)
|
2019-06-13 12:01:36 +08:00
|
|
|
if not ok then
|
|
|
|
return 500, "failed, " .. err, res_data
|
|
|
|
end
|
|
|
|
|
|
|
|
return 200, "passed", res_data
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-22 10:44:07 +08:00
|
|
|
function _M.read_file(path)
|
|
|
|
local f = assert(io.open(path, "rb"))
|
|
|
|
local cert = f:read("*all")
|
|
|
|
f:close()
|
|
|
|
return cert
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-02-26 21:26:01 +08:00
|
|
|
function _M.req_self_with_http(uri, method, body, headers)
|
|
|
|
if type(body) == "table" then
|
|
|
|
body = json.encode(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(method) == "number" then
|
|
|
|
method = methods[method]
|
|
|
|
end
|
|
|
|
headers = headers or {}
|
|
|
|
|
|
|
|
local httpc = http.new()
|
|
|
|
-- https://github.com/ledgetech/lua-resty-http
|
|
|
|
uri = ngx.var.scheme .. "://" .. ngx.var.server_addr
|
|
|
|
.. ":" .. ngx.var.server_port .. uri
|
|
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
local res, err = httpc:request_uri(uri,
|
|
|
|
{
|
|
|
|
method = method,
|
|
|
|
body = body,
|
|
|
|
keepalive = false,
|
|
|
|
headers = headers,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-06-13 16:10:27 +08:00
|
|
|
function _M.aes_encrypt(origin)
|
|
|
|
local iv = "1234567890123456"
|
|
|
|
local aes_128_cbc_with_iv = assert(aes:new(iv, nil, aes.cipher(128, "cbc"), {iv=iv}))
|
|
|
|
|
|
|
|
if aes_128_cbc_with_iv ~= nil and str_find(origin, "---") then
|
|
|
|
local encrypted = aes_128_cbc_with_iv:encrypt(origin)
|
|
|
|
if encrypted == nil then
|
|
|
|
core.log.error("failed to encrypt key[", origin, "] ")
|
|
|
|
return origin
|
|
|
|
end
|
|
|
|
|
|
|
|
return ngx_encode_base64(encrypted)
|
|
|
|
end
|
|
|
|
|
|
|
|
return origin
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
return _M
|