Skip to content

Commit

Permalink
fix(pdk) allow kong.response.exit on header_filter with no body
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Dec 3, 2018
1 parent 98b3ebd commit 49a5ffd
Show file tree
Hide file tree
Showing 5 changed files with 3,163 additions and 9 deletions.
8 changes: 6 additions & 2 deletions kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ local function new(self, major_version)
-- Unless manually specified, this method will automatically set the
-- Content-Length header in the produced response for convenience.
-- @function kong.response.exit
-- @phases rewrite, access, admin_api
-- @phases rewrite, access, admin_api, header_filter (only if `body` is nil)
-- @tparam number status The status to be used
-- @tparam[opt] table|string body The body to be used
-- @tparam[opt] table headers The headers to be used
Expand All @@ -553,7 +553,11 @@ local function new(self, major_version)
-- ["WWW-Authenticate"] = "Basic"
-- })
function _RESPONSE.exit(status, body, headers)
check_phase(rewrite_access)
if body == nil then
check_phase(rewrite_access_header)
else
check_phase(rewrite_access)
end

if ngx.headers_sent then
error("headers have already been sent", 2)
Expand Down
236 changes: 229 additions & 7 deletions t/01-pdk/08-response/00-phase_checks.t
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ GET /t
=== TEST 2: verify phase checking for kong.response.exit, failing phases
=== TEST 2: verify phase checking for kong.response.exit with table, failing phases
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -225,6 +225,7 @@ qq{
log = false,
admin_api = true,
},
}
phase_check_functions(phases.init_worker, true)
Expand Down Expand Up @@ -262,7 +263,76 @@ GET /t
=== TEST 3: verify phase checking for kong.response.exit, rewrite, with plain string
=== TEST 3: verify phase checking for kong.response.exit and with no body, failing phases
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location / {
return 200;
}
}
init_worker_by_lua_block {
phases = require("kong.pdk.private.phases").phases
phase_check_module = "response"
phase_check_data = {
{
method = "exit",
args = { 200 },
init_worker = false,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = false,
log = false,
admin_api = true,
},
}
phase_check_functions(phases.init_worker, true)
}
#ssl_certificate_by_lua_block {
# phase_check_functions(phases.certificate)
#}
}
--- config
location /t {
proxy_pass http://unix:$TEST_NGINX_NXSOCK/nginx.sock;
set $upstream_uri '/t';
set $upstream_scheme 'http';
header_filter_by_lua_block {
phase_check_functions(phases.header_filter, true)
-- reset Content-Length after partial execution with
-- phase checks disabled
ngx.header["Content-Length"] = 0
}
body_filter_by_lua_block {
phase_check_functions(phases.body_filter, true)
}
log_by_lua_block {
phase_check_functions(phases.log, true)
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 4: verify phase checking for kong.response.exit, rewrite, with plain string
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -312,7 +382,7 @@ GET /t
=== TEST 4: verify phase checking for kong.response.exit, rewrite, with tables
=== TEST 5: verify phase checking for kong.response.exit, rewrite, with tables
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -363,7 +433,57 @@ GET /t
=== TEST 5: verify phase checking for kong.response.exit, access, with plain string
=== TEST 6: verify phase checking for kong.response.exit, rewrite, with no body
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location / {
return 200;
}
}
init_worker_by_lua_block {
phases = require("kong.pdk.private.phases").phases
phase_check_module = "response"
phase_check_data = {
{
method = "exit",
args = { 200 },
init_worker = false,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = false,
log = false,
},
}
}
}
--- config
location /t {
proxy_pass http://unix:$TEST_NGINX_NXSOCK/nginx.sock;
set $upstream_uri '/t';
set $upstream_scheme 'http';
rewrite_by_lua_block {
phase_check_functions(phases.rewrite, true)
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 7: verify phase checking for kong.response.exit, access, with plain string
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -414,7 +534,7 @@ GET /t
=== TEST 6: verify phase checking for kong.response.exit, access, with tables
=== TEST 8: verify phase checking for kong.response.exit, access, with tables
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -465,7 +585,58 @@ GET /t
=== TEST 7: verify phase checking for kong.response.exit, admin_api, with plain string
=== TEST 9: verify phase checking for kong.response.exit, access, with no body
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location / {
return 200;
}
}
init_worker_by_lua_block {
phases = require("kong.pdk.private.phases").phases
phase_check_module = "response"
phase_check_data = {
{
method = "exit",
args = { 200 },
init_worker = false,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = false,
log = false,
admin_api = true,
},
}
}
}
--- config
location /t {
proxy_pass http://unix:$TEST_NGINX_NXSOCK/nginx.sock;
set $upstream_uri '/t';
set $upstream_scheme 'http';
access_by_lua_block {
phase_check_functions(phases.access, true)
}
}
--- request
GET /t
--- no_error_log
[error]
=== TEST 10: verify phase checking for kong.response.exit, admin_api, with plain string
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -516,7 +687,7 @@ GET /t
=== TEST 8: verify phase checking for kong.response.exit, admin_api, with tables
=== TEST 11: verify phase checking for kong.response.exit, admin_api, with tables
--- http_config eval
qq{
$t::Util::HttpConfig
Expand Down Expand Up @@ -564,3 +735,54 @@ qq{
GET /t
--- no_error_log
[error]
=== TEST 12: verify phase checking for kong.response.exit, admin_api, with no body
--- http_config eval
qq{
$t::Util::HttpConfig
server {
listen unix:$ENV{TEST_NGINX_NXSOCK}/nginx.sock;
location / {
return 200;
}
}
init_worker_by_lua_block {
phases = require("kong.pdk.private.phases").phases
phase_check_module = "response"
phase_check_data = {
{
method = "exit",
args = { 200 },
init_worker = false,
certificate = "pending",
rewrite = true,
access = true,
header_filter = true,
body_filter = false,
log = false,
admin_api = true,
},
}
}
}
--- config
location /t {
proxy_pass http://unix:$TEST_NGINX_NXSOCK/nginx.sock;
set $upstream_uri '/t';
set $upstream_scheme 'http';
access_by_lua_block {
phase_check_functions(phases.admin_api, true)
}
}
--- request
GET /t
--- no_error_log
[error]
27 changes: 27 additions & 0 deletions t/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ our $HttpConfig = <<_EOC_;
local private_phases = require("kong.pdk.private.phases")
local phases = private_phases.phases
-- This function executes 1 or more pdk methods twice: the first time with phase
-- checking deactivated, and the second time with phase checking activated.
-- Params:
-- * phase: the phase we want to test, i.e. "access"
-- * skip_fnlist controls a check: by default, this method
-- will check that the provided list of methods is "complete" - that
-- all the methods inside the `mod` (see below) are covered. Setting
-- `skip_fnlist` to `true` will skip that test (so the `mod` can have
-- methods that go untested)
--
-- This method also reads from 2 globals:
-- * phase_check_module is just a string used to determine the "module"
-- For example, if `phase_check_module` is "kong.response", then `mod` is "response"
-- * phase_check_data is an array of tables with this format:
-- {
-- method = "exit", -- the method inside mod, `kong.response.exit` for example
-- args = { 200 }, -- passed to the method
-- init_worker = false, -- expected to always throw an error on init_worker phase
-- certificate = "pending", -- ignored phase
-- rewrite = true, -- expected to work with and without the phase checker
-- access = true,
-- header_filter = "forced false", -- exit will only error with the phase_checks active
-- body_filter = false,
-- log = false,
-- admin_api = true,
-- }
--
function phase_check_functions(phase, skip_fnlist)
-- mock balancer structure
Expand Down
Loading

0 comments on commit 49a5ffd

Please sign in to comment.