Reference documents:

example.lua

local core = require("apisix.core")
local pairs = pairs
local type = type
local ngx = ngx
local buffers = {}
local schema = {
    type = "object",
    properties = {
        message = {
            description = "需要打印的日志",
            type = "string"
        }
    },
    required = { "message" },
    minProperties = 1,
}

local plugin_name = "example"

local _M = {
    version = 0.1,
    priority = 412,
    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
    core.log.info("xxxxxxxx: ", "gjx")
    return true
end

function _M.log(conf, ctx)
    buffers.message = conf.message
    core.log.debug("metadata:",core.json.encode(buffers,true))
end

return _M

Example.t

use t::APISIX 'no_plan';

repeat_each(1); -- 重复次数
no_long_string(); -- 默认地,失败的字符串匹配测试将会使用 Test::LongString 模块产生一个错误信息,在 run_tests 之前调用这个函数将会关闭它
no_root_location();
no_shuffle(); -- 在no_shuffle()调用的情况下,测试块的运行顺序与它们在测试文件中出现的顺序完全一致
log_level('debug'); -- 日志级别
run_tests; -- 这个放在最后

__DATA__

=== TEST 1: sanity  -- 用例名称
--- config   -- 用于配置Nginx conf 信息
    location /t {
        content_by_lua_block {
            local plugin = require("apisix.plugins.example")
             local ctx ={
                headers={
                }
            }
            local ok, err = plugin.check_schema({message="gejiax"})
            if not ok then
                ngx.say(err)
            end
            plugin.log({message="gejiax"},ctx)
            ngx.say("done")  -- 打印结果
        }
    }
--- request  -- 调用请求,校验结果
GET /t
--- more_headers -- 头部信息
Authorization: Bearer eyJhbGc
--- response_headers  -- 响应返回头部信息
in: out
--- error_code: 401 -- 状态码
--- response_body  -- 请求结果
done
--- no_error_log  -- 表示会对 nginx 的 error.log 检查,必须没有 EORROR 级别的记录
[error]
--- response_body_like eval  --返回值正则表达式校验
qr/"Access Denied"/
--- error_log eval   --错误日志正则表达式
qr/conf_version: \d+#1,/

content_by_lua_block description

=== TEST 12: Add https endpoint with ssl_verify false
--- config
    location /t {
        content_by_lua_block {
            local t = require("lib.test_admin").test  -- 调用apisix接口的实现,👇就是如何去调用API接口的示例
            local code, body = t('/apisix/admin/routes/1',
                 ngx.HTTP_PUT,
                 [[{
                        "plugins": {
                            "authz-keycloak": {
                                "token_endpoint": "https://127.0.0.1:8443/auth/realms/University/protocol/openid-connect/token",
                                "permissions": ["course_resource#delete"],
                                "client_id": "course_management",
                                "grant_type": "urn:ietf:params:oauth:grant-type:uma-ticket",
                                "timeout": 3000,
                                "ssl_verify": false
                            }
                        },
                        "upstream": {
                            "nodes": {
                                "127.0.0.1:1982": 1
                            },
                            "type": "roundrobin"
                        },
                        "uri": "/hello1"
                }]],
                [[{
                    "node": {
                        "value": {
                            "plugins": {
                                "authz-keycloak": {
                                    "token_endpoint": "https://127.0.0.1:8443/auth/realms/University/protocol/openid-connect/token",
                                    "permissions": ["course_resource#delete"],
                                    "client_id": "course_management",
                                    "grant_type": "urn:ietf:params:oauth:grant-type:uma-ticket",
                                    "timeout": 3000,
                                    "ssl_verify": false
                                }
                            },
                            "upstream": {
                                "nodes": {
                                    "127.0.0.1:1982": 1
                                },
                                "type": "roundrobin"
                            },
                            "uri": "/hello1"
                        },
                        "key": "/apisix/routes/1"
                    },
                    "action": "set"
                }]]
                )

            if code >= 300 then
                ngx.status = code
            end
            ngx.say(body)
        }
    }
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]

HTTP request

location /t {
        content_by_lua_block {
            local json_decode = require("toolkit.json").decode  -- json 工具类
            local http = require "resty.http"
            local httpc = http.new()
            local uri = "http://127.0.0.1:8090/auth/realms/University/protocol/openid-connect/token"
            local res, err = httpc:request_uri(uri, {
                    method = "POST",
                    body = "grant_type=password&client_id=course_management&client_secret=d1ec69e9-55d2-4109-a3ea-befa071579d5&username=teacher@gmail.com&password=123456",
                    headers = {
                        ["Content-Type"] = "application/x-www-form-urlencoded"
                    }
                })

            if res.status == 200 then
                local body = json_decode(res.body)
                local accessToken = body["access_token"]


                uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1"
                local res, err = httpc:request_uri(uri, {
                    method = "GET",
                    headers = {
                        ["Authorization"] = "Bearer " .. accessToken,
                    }
                 })

                if res.status == 200 then
                    ngx.say(true)
                else
                    ngx.say(false)
                end
            else
                ngx.say(false)
            end
        }
    }

multiple locations

=== TEST 6: first request timeout
--- config
    location = /aggregate {
        content_by_lua_block {
            local core = require("apisix.core")
            local t = require("lib.test_admin").test
            local code, body = t('/apisix/batch-requests',
                ngx.HTTP_POST,
                [=[{
                    "timeout": 100,
                    "pipeline":[
                    {
                        "path": "/b",
                        "headers": {
                            "Header1": "hello",
                            "Header2": "world"
                        }
                    },{
                        "path": "/c",
                        "method": "PUT"
                    },{
                        "path": "/d"
                    }]
                }]=],
                [=[[
                {
                    "status": 504,
                    "reason": "upstream timeout"
                }
                ]]=]
                )

            ngx.status = code
            ngx.say(body)
        }
    }

    location = /b {
        content_by_lua_block {
            ngx.sleep(1)
            ngx.status = 200
        }
    }
    location = /c {
        content_by_lua_block {
            ngx.status = 201
        }
    }
    location = /d {
        content_by_lua_block {
            ngx.status = 202
        }
    }
--- request
GET /aggregate
--- response_body
passed
--- error_log
timeout

迹_Jason
1k 声望65 粉丝

feeling主义者,追求极致的简约,创造最好的用户体验


引用和评论

0 条评论