Skip to content

Commit 2f9e5e6

Browse files
committed
A little more consistency with square brackets
1 parent 538a498 commit 2f9e5e6

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

tests.sh

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -euo pipefail
55
function message {
66
echo ""
77
echo "---------------------------------------------------------------"
8-
echo $1
8+
echo "$1"
99
echo "---------------------------------------------------------------"
1010
}
1111

@@ -14,11 +14,11 @@ RED=$(echo -en '\033[01;31m')
1414
GREEN=$(echo -en '\033[01;32m')
1515

1616
function failed {
17-
echo ${RED}$1${RESTORE}
17+
echo "${RED}${1}${RESTORE}"
1818
}
1919

2020
function passed {
21-
echo ${GREEN}$1${RESTORE}
21+
echo "${GREEN}${1}${RESTORE}"
2222
}
2323

2424
wait_for_ready() {
@@ -52,16 +52,16 @@ cleanup() {
5252

5353
trap cleanup EXIT
5454

55-
if ! [ -x "$(command -v jq)" ]; then
55+
if ! command -v jq >/dev/null 2>&1; then
5656
message "JQ not installed. Installing..."
5757
sudo apt -y install jq
5858
fi
5959

6060
message " Check if we're in Github Actions or local run "
61-
if [ -n "${GITHUB_ACTIONS:-}" ]; then
61+
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
6262
echo " Github Actions. Image should already be built."
6363
docker images
64-
if [ -z "$(docker images -q mendhak/http-https-echo:testing 2> /dev/null)" ]; then
64+
if [[ -z "$(docker images -q mendhak/http-https-echo:testing 2> /dev/null)" ]]; then
6565
echo "Docker image mendhak/http-https-echo:testing not found. Exiting."
6666
exit 1
6767
fi
@@ -85,10 +85,10 @@ wait_for_ready
8585

8686
message " Make http(s) request, and test the path, method, header and status code. "
8787
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb 'https://localhost:8443/hello-world?ccc=ddd&myquery=98765')
88-
if [ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ] && \
89-
[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ] && \
90-
[ "$(echo "$REQUEST" | jq -r '.query.myquery')" == '98765' ] && \
91-
[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]; then
88+
if [[ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ]] && \
89+
[[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ]] && \
90+
[[ "$(echo "$REQUEST" | jq -r '.query.myquery')" == '98765' ]] && \
91+
[[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]]; then
9292
passed "HTTPS request passed."
9393
else
9494
failed "HTTPS request failed."
@@ -97,7 +97,7 @@ else
9797
fi
9898
REQUEST_WITH_STATUS_CODE=$(curl -s -k -o /dev/null -w "%{http_code}" -H "x-set-response-status-code: 404" https://localhost:8443/hello-world)
9999
REQUEST_WITH_STATUS_CODE_V=$(curl -v -k -o /dev/null -w "%{http_code}" -H "x-set-response-status-code: 404" https://localhost:8443/hello-world)
100-
if [ "$REQUEST_WITH_STATUS_CODE" = "404" ]; then
100+
if [[ "$REQUEST_WITH_STATUS_CODE" == "404" ]]; then
101101
passed "HTTPS status code header passed."
102102
else
103103
failed "HTTPS status code header failed."
@@ -107,7 +107,7 @@ fi
107107

108108
REQUEST_WITH_STATUS_CODE=$(curl -s -k -o /dev/null -w "%{http_code}" https://localhost:8443/status/test?x-set-response-status-code=419)
109109
REQUEST_WITH_STATUS_CODE_V=$(curl -v -k -o /dev/null -w "%{http_code}" https://localhost:8443/hello-world?x-set-response-status-code=419)
110-
if [ "$REQUEST_WITH_STATUS_CODE" = "419" ]; then
110+
if [[ "$REQUEST_WITH_STATUS_CODE" == "419" ]]; then
111111
passed "HTTPS status code querystring passed."
112112
else
113113
failed "HTTPS status code querystring failed."
@@ -162,9 +162,9 @@ else
162162
fi
163163

164164
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
165-
if [ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ] && \
166-
[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ] && \
167-
[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]; then
165+
if [[ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ]] && \
166+
[[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ]] && \
167+
[[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]]; then
168168
passed "HTTP request with arbitrary header passed."
169169
else
170170
failed "HTTP request with arbitrary header failed."
@@ -174,7 +174,7 @@ fi
174174

175175
message " Make JSON request, and test that json is in the output. "
176176
REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d '{"a":"b"}' http://localhost:8080/)
177-
if [ "$(echo "$REQUEST" | jq -r '.json.a')" == 'b' ]; then
177+
if [[ "$(echo "$REQUEST" | jq -r '.json.a')" == 'b' ]]; then
178178
passed "JSON test passed."
179179
else
180180
failed "JSON test failed."
@@ -185,7 +185,7 @@ fi
185185

186186
message " Make JSON request with gzip Content-Encoding, and test that json is in the output. "
187187
REQUEST=$(echo -n '{"a":"b"}' | gzip | curl -s -X POST -H "Content-Encoding: gzip" -H "Content-Type: application/json" --data-binary @- http://localhost:8080/)
188-
if [ "$(echo "$REQUEST" | jq -r '.json.a')" == 'b' ]; then
188+
if [[ "$(echo "$REQUEST" | jq -r '.json.a')" == 'b' ]]; then
189189
passed "JSON test passed."
190190
else
191191
failed "JSON test failed."
@@ -194,7 +194,7 @@ else
194194
fi
195195

196196
REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d 'not-json' http://localhost:8080)
197-
if [ "$(echo "$REQUEST" | jq -r '.json')" == 'null' ]; then
197+
if [[ "$(echo "$REQUEST" | jq -r '.json')" == 'null' ]]; then
198198
passed "JSON with Invalid Body test passed."
199199
else
200200
failed "JSON with Invalid Body test failed."
@@ -214,7 +214,7 @@ message " Make request with a header within limit."
214214
LARGE_HEADER_VALUE=$(head -c 600 </dev/urandom | base64 | tr -d '\n')
215215
REQUEST=$(curl -s -k -H "Large-Header: $LARGE_HEADER_VALUE" https://localhost:8443/)
216216

217-
if [ "$(echo "$REQUEST" | jq -r '.headers."large-header"')" == "$LARGE_HEADER_VALUE" ]; then
217+
if [[ "$(echo "$REQUEST" | jq -r '.headers."large-header"')" == "$LARGE_HEADER_VALUE" ]]; then
218218
passed "Large header test passed."
219219
else
220220
failed "Large header test failed."
@@ -244,9 +244,9 @@ wait_for_ready
244244

245245
message " Make http(s) request, and test the path, method and header. "
246246
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
247-
if [ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ] && \
248-
[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ] && \
249-
[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]; then
247+
if [[ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ]] && \
248+
[[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ]] && \
249+
[[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]]; then
250250
passed "HTTPS request passed."
251251
else
252252
failed "HTTPS request failed."
@@ -255,9 +255,9 @@ else
255255
fi
256256

257257
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
258-
if [ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ] && \
259-
[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ] && \
260-
[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]; then
258+
if [[ "$(echo "$REQUEST" | jq -r '.path')" == '/hello-world' ]] && \
259+
[[ "$(echo "$REQUEST" | jq -r '.method')" == 'PUT' ]] && \
260+
[[ "$(echo "$REQUEST" | jq -r '.headers.arbitrary')" == 'Header' ]]; then
261261
passed "HTTP request passed."
262262
else
263263
failed "HTTP request failed."
@@ -275,12 +275,12 @@ docker run -d --rm -e ECHO_BACK_TO_CLIENT=false --name http-echo-tests -p 8080:8
275275
wait_for_ready
276276

277277
REQUEST=$(curl -s -k http://localhost:8080/a/b/c)
278-
if [[ -z ${REQUEST} ]]
278+
if [[ -z "$REQUEST" ]]
279279
then
280280
passed "Response is empty."
281281
else
282282
failed "Expected empty response, but got a non-empty response."
283-
echo $REQUEST
283+
echo "$REQUEST"
284284
exit 1
285285
fi
286286

@@ -310,9 +310,9 @@ docker run -d --rm -e JWT_HEADER=Authentication --name http-echo-tests -p 8080:8
310310
wait_for_ready
311311

312312
REQUEST=$(curl -s -k -H "Authentication: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" https://localhost:8443/ )
313-
if [ "$(echo "$REQUEST" | jq -r '.jwt.header.typ')" == 'JWT' ] && \
314-
[ "$(echo "$REQUEST" | jq -r '.jwt.header.alg')" == 'HS256' ] && \
315-
[ "$(echo "$REQUEST" | jq -r '.jwt.payload.sub')" == '1234567890' ]; then
313+
if [[ "$(echo "$REQUEST" | jq -r '.jwt.header.typ')" == 'JWT' ]] && \
314+
[[ "$(echo "$REQUEST" | jq -r '.jwt.header.alg')" == 'HS256' ]] && \
315+
[[ "$(echo "$REQUEST" | jq -r '.jwt.payload.sub')" == '1234567890' ]]; then
316316
passed "JWT request passed."
317317
else
318318
failed "JWT request failed."
@@ -332,8 +332,8 @@ wait_for_ready "/ping"
332332
curl -s -k -X POST -d "banana" https://localhost:8443/ping > /dev/null
333333

334334
# There should be 3 lines, the "listening on...", the /ping ready test, and the /ping POST test.
335-
if [ $(docker logs http-echo-tests | wc -l) == 3 ] && \
336-
! [ $(docker logs http-echo-tests | grep banana) ]
335+
if [[ "$(docker logs http-echo-tests | wc -l)" == 3 ]] && \
336+
! docker logs http-echo-tests | grep -q banana
337337
then
338338
passed "LOG_IGNORE_PATH ignored the /ping path"
339339
else
@@ -353,8 +353,8 @@ wait_for_ready "/health"
353353
curl -s -k -X POST -d "banana" https://localhost:8443/metrics > /dev/null
354354

355355
# There should be 3 lines, the "listening on...", the /health ready test, and the /metrics POST test.
356-
if [ $(docker logs http-echo-tests | wc -l) == 3 ] && \
357-
! [ $(docker logs http-echo-tests | grep banana) ]
356+
if [[ "$(docker logs http-echo-tests | wc -l)" == 3 ]] && \
357+
! docker logs http-echo-tests | grep -q banana
358358
then
359359
passed "LOG_IGNORE_PATH ignored the /metrics path"
360360
else
@@ -366,7 +366,7 @@ fi
366366
# Test a positive case where the path is not ignored
367367
curl -s -k -X POST -d "strawberry" https://localhost:8443/veryvisible > /dev/null
368368

369-
if [[ $(docker logs http-echo-tests | grep strawberry) ]]
369+
if docker logs http-echo-tests | grep -q strawberry
370370
then
371371
passed "LOG_IGNORE_PATH didn't ignore the /veryvisible path"
372372
else
@@ -387,8 +387,8 @@ wait_for_ready "/hello"
387387
curl -s -k -X POST -d "banana" https://localhost:8443/ > /dev/null
388388

389389
# There should be 3 lines, the "listening on", the "/hello" ready test, and the POST banana test.
390-
if [ $(docker logs http-echo-tests | wc -l) == 3 ] && \
391-
! [ $(docker logs http-echo-tests | grep banana) ]
390+
if [[ "$(docker logs http-echo-tests | wc -l)" == 3 ]] && \
391+
! docker logs http-echo-tests | grep -q banana
392392
then
393393
passed "LOG_IGNORE_PATH ignored all paths"
394394
else
@@ -407,7 +407,7 @@ docker run -d --rm -e DISABLE_REQUEST_LOGS=true --name http-echo-tests -p 8080:8
407407
wait_for_ready "/healthy"
408408

409409
curl -s -k -X GET https://localhost:8443/strawberry > /dev/null
410-
if [ $(docker logs http-echo-tests | grep -c "GET /strawberry HTTP/1.1") -eq 0 ]
410+
if [[ "$(docker logs http-echo-tests | grep -c "GET /strawberry HTTP/1.1")" -eq 0 ]]
411411
then
412412
passed "DISABLE_REQUEST_LOGS disabled Express HTTP logging"
413413
else
@@ -449,8 +449,8 @@ wait_for_ready
449449
curl -s -k -X POST -d "tiramisu" https://localhost:8443/ > /dev/null
450450

451451
# There will be 4 lines, the Listening on, the / ready test and response, the POST test and response
452-
if [ $(docker logs http-echo-tests | wc -l) == 5 ] && \
453-
[ $(docker logs http-echo-tests | grep tiramisu) ]
452+
if [[ "$(docker logs http-echo-tests | wc -l)" == 5 ]] && \
453+
docker logs http-echo-tests | grep -q tiramisu
454454
then
455455
passed "LOG_WITHOUT_NEWLINE logged output in single line"
456456
else
@@ -469,7 +469,7 @@ docker run -d --name http-echo-tests --rm mendhak/http-https-echo:testing
469469

470470
WHOAMI=$(docker exec http-echo-tests whoami)
471471

472-
if [ "$WHOAMI" == "node" ]
472+
if [[ "$WHOAMI" == "node" ]]
473473
then
474474
passed "Running as non root user"
475475
else
@@ -491,7 +491,7 @@ curl -s http://localhost:8080 > /dev/null
491491

492492
WHOAMI="$(docker exec http-echo-tests id -u)"
493493

494-
if [ "$WHOAMI" == "$CONTAINER_USER" ]
494+
if [[ "$WHOAMI" == "$CONTAINER_USER" ]]
495495
then
496496
passed "Running as $CONTAINER_USER user"
497497
else
@@ -513,7 +513,7 @@ wait_for_ready
513513

514514
COMMON_NAME="$(curl -sk --cert fullchain.pem --key testpk.pem https://localhost:8443/ | jq -r '.clientCertificate.subject.CN')"
515515
SAN="$(curl -sk --cert fullchain.pem --key testpk.pem https://localhost:8443/ | jq -r '.clientCertificate.subjectaltname')"
516-
if [ "$COMMON_NAME" == "client.example.net" ] && [ "$SAN" == "DNS:client.example.net" ]
516+
if [[ "$COMMON_NAME" == "client.example.net" && "$SAN" == "DNS:client.example.net" ]]
517517
then
518518
passed "Client certificate details are present in the output"
519519
else
@@ -523,7 +523,7 @@ fi
523523

524524
message " Check if certificate is not passed, then client certificate details are empty"
525525
CLIENT_CERT="$(curl -sk https://localhost:8443/ | jq -r '.clientCertificate')"
526-
if [ "$CLIENT_CERT" == "{}" ]
526+
if [[ "$CLIENT_CERT" == "{}" ]]
527527
then
528528
passed "Client certificate details are not present in the response"
529529
else
@@ -533,7 +533,7 @@ fi
533533

534534
message " Check that HTTP server does not have any client certificate property"
535535
CLIENT_CERT=$(curl -sk --cert cert.pem --key testpk.pem http://localhost:8080/ | jq 'has("clientCertificate")')
536-
if [ "$CLIENT_CERT" == "false" ]
536+
if [[ "$CLIENT_CERT" == "false" ]]
537537
then
538538
passed "Client certificate details are not present in regular HTTP server"
539539
else
@@ -567,7 +567,7 @@ wait_for_ready
567567

568568
REQUEST_WITH_STATUS_CODE="$(curl -s --cacert "$(pwd)/server_fullchain.pem" -o /dev/null -w "%{http_code}" \
569569
--resolve "${cert_common_name}:8443:127.0.0.1" "https://${cert_common_name}:8443/hello-world")"
570-
if [ "${REQUEST_WITH_STATUS_CODE}" = 200 ]
570+
if [[ "${REQUEST_WITH_STATUS_CODE}" == 200 ]]
571571
then
572572
passed "Server certificate and private key are loaded from configured custom location"
573573
else
@@ -585,7 +585,7 @@ wait_for_ready
585585

586586
RESPONSE_BODY="$(curl -sk https://localhost:8443/ | jq -r '.env.ECHO_INCLUDE_ENV_VARS')"
587587

588-
if [ "$RESPONSE_BODY" == "1" ]
588+
if [[ "$RESPONSE_BODY" == "1" ]]
589589
then
590590
passed "Environment variables present in the output"
591591
else
@@ -603,7 +603,7 @@ wait_for_ready
603603

604604
RESPONSE_BODY_ENV_CHECK="$(curl -sk https://localhost:8443/ | jq 'has("env")')"
605605

606-
if [ "$RESPONSE_BODY_ENV_CHECK" == "false" ]
606+
if [[ "$RESPONSE_BODY_ENV_CHECK" == "false" ]]
607607
then
608608
passed "Environment variables not present in the output by default"
609609
else
@@ -677,7 +677,7 @@ wait_for_removed
677677

678678
message " Start container with a custom response body from a file "
679679
echo "<h1>Hello World</h1>" > test.html
680-
docker run -d --rm -v ${PWD}/test.html:/app/test.html --name http-echo-tests -p 8080:8080 -e OVERRIDE_RESPONSE_BODY_FILE_PATH=/test.html -t mendhak/http-https-echo:testing
680+
docker run -d --rm -v "${PWD}/test.html:/app/test.html" --name http-echo-tests -p 8080:8080 -e OVERRIDE_RESPONSE_BODY_FILE_PATH=/test.html -t mendhak/http-https-echo:testing
681681
wait_for_ready
682682

683683
RESPONSE_BODY=$(curl -s http://localhost:8080)
@@ -713,7 +713,7 @@ console.log(sign('my-value','mysecretkey123'));")
713713

714714

715715
RESPONSE=$(curl -s http://localhost:8080/ -H "Cookie: mysigned=s:${SIGNED_COOKIE}")
716-
if [ "$(echo "$RESPONSE" | jq -r '.signedCookies.mysigned')" == 'my-value' ]; then
716+
if [[ "$(echo "$RESPONSE" | jq -r '.signedCookies.mysigned')" == 'my-value' ]]; then
717717
passed "Signed cookie test passed."
718718
else
719719
failed "Signed cookie test failed."
@@ -732,8 +732,8 @@ wait_for_ready
732732

733733

734734
RESPONSE=$(curl -s http://localhost:8080/ -H "Cookie: foo=bar; baz=qux")
735-
if [ "$(echo "$RESPONSE" | jq -r '.cookies.foo')" == 'bar' ] && \
736-
[ "$(echo "$RESPONSE" | jq -r '.cookies.baz')" == 'qux' ]; then
735+
if [[ "$(echo "$RESPONSE" | jq -r '.cookies.foo')" == 'bar' ]] && \
736+
[[ "$(echo "$RESPONSE" | jq -r '.cookies.baz')" == 'qux' ]]; then
737737
passed "Cookies returned in response test passed."
738738
else
739739
failed "Cookies returned in response test failed."

0 commit comments

Comments
 (0)