@@ -31,7 +31,7 @@ use Cwd qw(cwd);
3131
3232repeat_each(2 );
3333
34- plan tests => repeat_each() * (blocks() * 9 ) - 4 ;
34+ plan tests => repeat_each() * (blocks() * 9 ) + 2 ;
3535
3636my $ 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