参考文档:

  • Run Test
  • ❗️❗️❗️Test Nginx 语法具体阐明

example.lua

local core = require("apisix.core")local pairs = pairslocal type = typelocal ngx = ngxlocal 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 trueendfunction _M.log(conf, ctx)    buffers.message = conf.message    core.log.debug("metadata:",core.json.encode(buffers,true))endreturn _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 阐明

=== 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)        }    }--- requestGET /t--- response_bodypassed--- no_error_log[error]

HTTP 申请

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        }    }

多个location

=== 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        }    }--- requestGET /aggregate--- response_bodypassed--- error_logtimeout