Skip to content

Commit 7b1bc85

Browse files
authored
Merge pull request #4248 from alphagov/fix-redirect-location-in-router-probe-backend
Fix redirect location in router-probe-backend and add test for it
2 parents d428206 + 3fa49dc commit 7b1bc85

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

charts/router-probe-backend/files/openresty.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ http {
5555
access_log /dev/stdout json_event;
5656
error_log /dev/stderr;
5757

58+
absolute_redirect off;
59+
5860
location / {
5961
return 404;
6062
}
@@ -64,7 +66,7 @@ http {
6466
}
6567

6668
location = /__probe__/redirect {
67-
return 301 $scheme://$host/redirected;
69+
return 301 /__probe__/redirected;
6870
}
6971

7072
location = /__probe__/redirected {

charts/router-probe-backend/tests/run-tests.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,50 @@ function expect {
9393
>&2 echo " Status Code for $RESOURCE:"
9494
>&2 echo " Expected: $EXPECTED_STATUS"
9595
>&2 echo " Actual: $ACTUAL_STATUS"
96+
return 1
9697
fi
9798

9899
cat "$TMPFILE"
99100
}
100101

102+
function expect_redirect_to_location {
103+
# Args:
104+
# $1: HTTP Method
105+
# $2: Resource path to curl
106+
# $3: Redirect Location Header Expected in response
107+
local METHOD="$1"
108+
local RESOURCE="$2"
109+
local EXPECTED_LOCATION="$3"
110+
local ACTUAL_LOCATION
111+
112+
>&2 echo -n "Testing ${METHOD} ${RESOURCE} redirects to ${EXPECTED_LOCATION} ... "
113+
ACTUAL_LOCATION=$(
114+
curl --request "${METHOD}" --verbose "http://127.0.0.1:80${RESOURCE}" 2>&1 | \
115+
grep -i '< location:' | \
116+
tr -d '\r' | \
117+
sed -E 's/.*[Ll]ocation: //'
118+
)
119+
120+
if [ "$ACTUAL_LOCATION" == "$EXPECTED_LOCATION" ]; then
121+
>&2 echo "OK"
122+
else
123+
>&2 echo "ERROR"
124+
>&2 echo " Redirect Location for $RESOURCE:"
125+
>&2 echo " Expected: [$EXPECTED_LOCATION]"
126+
>&2 echo " Actual: [$ACTUAL_LOCATION]"
127+
return 1
128+
fi
129+
}
130+
131+
101132
echo "============================================="
102133
echo "Executing Tests:"
103134
echo "============================================="
104135
{
105136
expect "GET" "/" "404"
106137
expect "GET" "/__probe__/ok" "200"
107138
expect "GET" "/__probe__/redirect" "301"
139+
expect_redirect_to_location "GET" "/__probe__/redirect" "/__probe__/redirected"
108140
expect "GET" "/__probe__/not-found" "404"
109141
expect "GET" "/__probe__/internal-server-error" "500"
110142
expect "GET" "/__probe__/get" "200"

0 commit comments

Comments
 (0)