-- unit test for /api/get_client_tasks.json
local tb = require("resty.iresty_test")
local json = require("cjson")
local test = tb.new({unit_name="get_client_tasks"})
function tb:init()
self.mid = string.rep('0',32)
end
function tb:test_0000()
-- 正常请求
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":[1600,1700]}]] }
)
if 200 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0001()
-- 缺少body
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0002()
-- 错误的json内容
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":"[1600,1700]}]] }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
function tb:test_0003()
-- 错误的json格式
local res = ngx.location.capture(
'/api/get_client_tasks.json?mid='..self.mid,
{ method = ngx.HTTP_POST, body=[[{"type":"[1600,1700]"}]] }
)
if 400 ~= res.status then
error("failed code:" .. res.status)
end
end
test:run()
Nginx output:
0.000 [get_client_tasks] unit test start
0.001 \_[test_0000] PASS
0.001 \_[test_0001] PASS
0.001 \_[test_0002] PASS
0.001 \_[test_0003] PASS
0.001 [get_client_tasks] unit test complete