Skip to content

Fixes #8: Converter for the validator response codes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function ValidatorHandlerErrorDecorator:decorateResponse( response_status, respo

local o = getResponsesTemplate()[response_status]
if ( o ~= nil ) then
ngx.status = o.http_status
ngx.status = self:convertToValidHttpStatusCode(o.http_status)
-- NOTE: assumption: for the moment if it's custom, then it's application/json
ngx.header["Content-Type"] = "application/json"
-- add custom headers too
Expand Down Expand Up @@ -134,12 +134,30 @@ function ValidatorHandlerErrorDecorator:decorateResponse( response_status, respo

-- if no custom status code was used, assume the default one is right by trusting the validators
if ( response_body ~= nil and #response_body > 0 and response_body ~= "nil\n" ) then
ngx.status = response_status
ngx.status = self:convertToValidHttpStatusCode(response_status)
ngx.say( response_body )
return
end
-- if there is no custom response form the validator just exit with the status
ngx.exit( response_status )
ngx.exit( self:convertToValidHttpStatusCode(response_status) )
end

--- Convert the codes sent by validators to real HTTP response codes
-- @param response_status
--
function ValidatorHandlerErrorDecorator:convertToValidHttpStatusCode(response_status)
if (response_status >= 100 and response_status <= 599) then
return response_status
end

local http_code_str = string.sub(tostring(response_status), 0, 3)
local http_code_number = assert(tonumber(http_code_str), "Invalid HTTP Status Code when decorating response: " .. http_code_str)
if (http_code_number >= 100 and http_code_number <= 599) then
return http_code_number
end

ngx.log(ngx.DEBUG, "Status code: " , tostring(response_status) , " has not a valid HTTP Status Code format")
return 500
end

--- Parse the response message and replace any variables, if found (at most 3 variables)
Expand Down
87 changes: 86 additions & 1 deletion test/perl/api-gateway/validation/validatorHandler.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 9) - 4;
plan tests => repeat_each() * (blocks() * 9) + 2;

my $pwd = cwd();

Expand Down Expand Up @@ -534,3 +534,88 @@ custom-header-2: this is a lua variable",
[error]


=== TEST 9: test that validation responses codes are corrected to standard HTTP Status Codes
--- http_config eval: $::HttpConfig
--- config
include ../../api-gateway/default_validators.conf;

error_log ../test-logs/validatorHandler_test9_error.log debug;

location /validator_1 {
return 200;
}
location /validator_2 {
content_by_lua '
ngx.status = 401000
ngx.say("{ error_code = \\"401000\\", message = \\"I am invalid\\" }")
';
}
location /validator_3 {
content_by_lua '
ngx.status = 700000
ngx.say("{ error_code = \\"700000\\", message = \\"Invalid status code\\" }")
';
}
location /validator_4 {
content_by_lua '
ngx.status = 4091
ngx.say("{ error_code = \\"4091\\", message = \\"Invalid status code\\" }")
';
}


location /test-with-valid-status-code {
set $validator_custom_error_responses '';
set $request_validator_1 "on; path=/validator_1; order=1;";
set $request_validator_2 "on; path=/validator_2; order=2;";
access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua '
ngx.say("If you see this, validators are failing :(. why ? Pick your answer: http://www.thatwasfunny.com/top-20-programmers-excuses/239")
';
}

location /test-with-invalid-status-code {
set $validator_custom_error_responses '';
set $request_validator_1 "on; path=/validator_1; order=1;";
set $request_validator_2 "on; path=/validator_3; order=2;";
access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua '
ngx.say("You should not see me")
';
}

location /test-with-string-status-code {
set $validator_custom_error_responses '';
set $request_validator_1 "on; path=/validator_1; order=1;";
set $request_validator_2 "on; path=/validator_4; order=2;";
access_by_lua "ngx.apiGateway.validation.validateRequest()";
content_by_lua '
ngx.say("You should not see me")
';
}


--- pipelined_requests eval
[
"GET /test-with-valid-status-code",
"GET /test-with-invalid-status-code",
"GET /test-with-string-status-code"
]
--- response_body_like eval
[
'^{ error_code = "401000", message = "I am invalid" }.+',
'^{ error_code = "700000", message = "Invalid status code" }+',
'^{ error_code = "4091", message = "Invalid status code" }+'
]
--- response_headers_like eval
[
"Content-Type: text/plain",
"Content-Type: text/plain",
"Content-Type: text/plain"
]
--- error_code_like eval
[401,500,409]
--- no_error_log
[error]