From 96c57048f71fa15032375d4ff4a900311c214d49 Mon Sep 17 00:00:00 2001 From: Nick Porter Date: Wed, 30 Jul 2025 14:15:16 +0100 Subject: [PATCH] Add test of %rest() receiving a failure HTTP status code --- scripts/ci/openresty-setup.sh | 4 ++++ scripts/ci/openresty/fail.lua | 5 +++++ src/tests/modules/rest/rest_xlat.unlang | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 scripts/ci/openresty/fail.lua diff --git a/scripts/ci/openresty-setup.sh b/scripts/ci/openresty-setup.sh index c97d81f4eb..c86783ddfe 100755 --- a/scripts/ci/openresty-setup.sh +++ b/scripts/ci/openresty-setup.sh @@ -102,6 +102,10 @@ http { location ~ ^/delay(.*)$ { content_by_lua_file ${APIDIR}/delay-api.lua; } + + location ~ ^/fail(.*)$ { + content_by_lua_file ${APIDIR}/fail.lua; + } } server { diff --git a/scripts/ci/openresty/fail.lua b/scripts/ci/openresty/fail.lua new file mode 100644 index 0000000000..76a5025e0c --- /dev/null +++ b/scripts/ci/openresty/fail.lua @@ -0,0 +1,5 @@ +-- API which will return code 400 with a JSON formatted error message + +ngx.status = ngx.HTTP_BAD_REQUEST +ngx.say('{"error": "Invalid request"}') +return ngx.exit(ngx.HTTP_BAD_REQUEST) diff --git a/src/tests/modules/rest/rest_xlat.unlang b/src/tests/modules/rest/rest_xlat.unlang index e96b7689c5..ca452b0502 100644 --- a/src/tests/modules/rest/rest_xlat.unlang +++ b/src/tests/modules/rest/rest_xlat.unlang @@ -212,4 +212,25 @@ if (!(Module-Failure-Message == "curl request failed: Timeout was reached (28)") test_fail } +# Call an endpoint which will return a "failure" status code +result_string := %rest('GET', "http://%{server_host}:%uri.safe(%{server_port})/fail") + +if (REST-HTTP-Status-Code != 400) { + test_fail +} + +# The output should not be returned +if (result_string) { + test_fail +} + +# Server output will be in REST-HTTP-Body (this endpoint returns JSON) +map json REST-HTTP-Body { + test_string := '$.error' +} + +if (test_string != 'Invalid request') { + test_fail +} + test_pass -- 2.47.2