Skip to content

Commit fbe710f

Browse files
committed
Merge pull request #11 from adobe-apiplatform/issue-8
Fixes #8: Converter for the validator response codes
2 parents c3bc196 + 301a654 commit fbe710f

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

src/lua/api-gateway/validation/validatorsHandlerErrorDecorator.lua

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function ValidatorHandlerErrorDecorator:decorateResponse( response_status, respo
106106

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

135135
-- if no custom status code was used, assume the default one is right by trusting the validators
136136
if ( response_body ~= nil and #response_body > 0 and response_body ~= "nil\n" ) then
137-
ngx.status = response_status
137+
ngx.status = self:convertToValidHttpStatusCode(response_status)
138138
ngx.say( response_body )
139139
return
140140
end
141141
-- if there is no custom response form the validator just exit with the status
142-
ngx.exit( response_status )
142+
ngx.exit( self:convertToValidHttpStatusCode(response_status) )
143+
end
144+
145+
--- Convert the codes sent by validators to real HTTP response codes
146+
-- @param response_status
147+
--
148+
function ValidatorHandlerErrorDecorator:convertToValidHttpStatusCode(response_status)
149+
if (response_status >= 100 and response_status <= 599) then
150+
return response_status
151+
end
152+
153+
local http_code_str = string.sub(tostring(response_status), 0, 3)
154+
local http_code_number = assert(tonumber(http_code_str), "Invalid HTTP Status Code when decorating response: " .. http_code_str)
155+
if (http_code_number >= 100 and http_code_number <= 599) then
156+
return http_code_number
157+
end
158+
159+
ngx.log(ngx.DEBUG, "Status code: " , tostring(response_status) , " has not a valid HTTP Status Code format")
160+
return 500
143161
end
144162

145163
--- Parse the response message and replace any variables, if found (at most 3 variables)

test/perl/api-gateway/validation/validatorHandler.t

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use Cwd qw(cwd);
3131

3232
repeat_each(2);
3333

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

3636
my $pwd = cwd();
3737

@@ -534,3 +534,88 @@ custom-header-2: this is a lua variable",
534534
[error]
535535
536536
537+
=== TEST 9: test that validation responses codes are corrected to standard HTTP Status Codes
538+
--- http_config eval: $::HttpConfig
539+
--- config
540+
include ../../api-gateway/default_validators.conf;
541+
542+
error_log ../test-logs/validatorHandler_test9_error.log debug;
543+
544+
location /validator_1 {
545+
return 200;
546+
}
547+
location /validator_2 {
548+
content_by_lua '
549+
ngx.status = 401000
550+
ngx.say("{ error_code = \\"401000\\", message = \\"I am invalid\\" }")
551+
';
552+
}
553+
location /validator_3 {
554+
content_by_lua '
555+
ngx.status = 700000
556+
ngx.say("{ error_code = \\"700000\\", message = \\"Invalid status code\\" }")
557+
';
558+
}
559+
location /validator_4 {
560+
content_by_lua '
561+
ngx.status = 4091
562+
ngx.say("{ error_code = \\"4091\\", message = \\"Invalid status code\\" }")
563+
';
564+
}
565+
566+
567+
location /test-with-valid-status-code {
568+
set $validator_custom_error_responses '';
569+
set $request_validator_1 "on; path=/validator_1; order=1;";
570+
set $request_validator_2 "on; path=/validator_2; order=2;";
571+
access_by_lua "ngx.apiGateway.validation.validateRequest()";
572+
content_by_lua '
573+
ngx.say("If you see this, validators are failing :(. why ? Pick your answer: http://www.thatwasfunny.com/top-20-programmers-excuses/239")
574+
';
575+
}
576+
577+
location /test-with-invalid-status-code {
578+
set $validator_custom_error_responses '';
579+
set $request_validator_1 "on; path=/validator_1; order=1;";
580+
set $request_validator_2 "on; path=/validator_3; order=2;";
581+
access_by_lua "ngx.apiGateway.validation.validateRequest()";
582+
content_by_lua '
583+
ngx.say("You should not see me")
584+
';
585+
}
586+
587+
location /test-with-string-status-code {
588+
set $validator_custom_error_responses '';
589+
set $request_validator_1 "on; path=/validator_1; order=1;";
590+
set $request_validator_2 "on; path=/validator_4; order=2;";
591+
access_by_lua "ngx.apiGateway.validation.validateRequest()";
592+
content_by_lua '
593+
ngx.say("You should not see me")
594+
';
595+
}
596+
597+
598+
--- pipelined_requests eval
599+
[
600+
"GET /test-with-valid-status-code",
601+
"GET /test-with-invalid-status-code",
602+
"GET /test-with-string-status-code"
603+
]
604+
--- response_body_like eval
605+
[
606+
'^{ error_code = "401000", message = "I am invalid" }.+',
607+
'^{ error_code = "700000", message = "Invalid status code" }+',
608+
'^{ error_code = "4091", message = "Invalid status code" }+'
609+
]
610+
--- response_headers_like eval
611+
[
612+
"Content-Type: text/plain",
613+
"Content-Type: text/plain",
614+
"Content-Type: text/plain"
615+
]
616+
--- error_code_like eval
617+
[401,500,409]
618+
--- no_error_log
619+
[error]
620+
621+

0 commit comments

Comments
 (0)