test: added basic test case.

This commit is contained in:
Yuansheng Wang 2019-04-11 15:00:59 +08:00
parent e2835eae52
commit 4f2238a40d
4 changed files with 66 additions and 7 deletions

1
.gitignore vendored
View File

@ -41,3 +41,4 @@ luac.out
# develop
logs
t/servroot

View File

@ -1,8 +1,10 @@
-- Copyright (C) Yuansheng Wang
local require = require
local log = require("apimeta.comm.log")
local resp = require("apimeta.comm.resp")
local route_handler = require("apimeta.route.handler")
local new_tab = require("table.new")
local ngx = ngx
local ngx_req = ngx.req
local ngx_var = ngx.var
@ -10,7 +12,11 @@ local ngx_var = ngx.var
local _M = {}
function _M.init()
require("resty.core")
require("ngx.re").opt("jit_stack_size", 200 * 1024)
require("jit.opt").start("minstitch=2", "maxtrace=4000",
"maxrecord=8000", "sizemcode=64",
"maxmcode=4000", "maxirconst=1000")
end
function _M.init_worker()
@ -22,7 +28,8 @@ function _M.access()
local api_ctx = ngx_ctx.api_ctx
if api_ctx == nil then
api_ctx = {}
-- todo: reuse this table
api_ctx = new_tab(0, 32)
ngx_ctx.api_ctx = api_ctx
end
@ -30,11 +37,17 @@ function _M.access()
api_ctx.uri = api_ctx.uri or ngx_var.uri
api_ctx.host = api_ctx.host or ngx_var.host
local router = route_handler.get_router()
local ok = router:dispatch(api_ctx.method, api_ctx.uri, api_ctx)
local router, dispatch_uri = route_handler.get_router()
local ok
if dispatch_uri then
ok = router:dispatch(api_ctx.method, api_ctx.uri, api_ctx)
else
ok = router:dispatch(api_ctx.method, api_ctx.host .. api_ctx.uri,
api_ctx)
end
if not ok then
log.warn("not find any matched route")
resp(403)
resp(404)
end
end

View File

@ -8,6 +8,7 @@ local insert_tab = table.insert
local new_tab = require("table.new")
local router
local dispatch_uri = true
local _M = {}
@ -27,7 +28,7 @@ function _M.get_router()
})
end
return router
return router, dispatch_uri
end
@ -54,7 +55,7 @@ function _M.load_route(routes)
end
router = r3router.new(items)
return router
return router, dispatch_uri
end

44
t/sanity.t Normal file
View File

@ -0,0 +1,44 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);
repeat_each(1);
log_level('warn');
my $pwd = cwd();
add_block_preprocessor(sub {
my ($block) = @_;
my $http_config = $block->http_config // '';
$http_config .= <<_EOC_;
lua_package_path "$pwd/lua/?.lua;;";
init_by_lua_block {
require "resty.core"
}
_EOC_
$block->set_value("http_config", $http_config);
});
plan tests => blocks() * repeat_each() * 2;
run_tests();
__DATA__
=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local apimeta = require("apimeta")
apimeta.access()
}
}
--- request
GET /t
--- error_code: 404
--- response_body_like eval
qr/404 Not Found/