Skip to content

Instantly share code, notes, and snippets.

@jmwebservices
Created January 9, 2026 11:04
Show Gist options
  • Select an option

  • Save jmwebservices/ae9ecfdf7cad8c2f01c2d331d0a567bd to your computer and use it in GitHub Desktop.

Select an option

Save jmwebservices/ae9ecfdf7cad8c2f01c2d331d0a567bd to your computer and use it in GitHub Desktop.
cURL script that tests the response codes of URLs.
#!/usr/bin/env bash
# Exit on unset variables
set -u
# Curl options
CURL_OPTS="-s -o /dev/null -w %{http_code}"
# --------------------------------------------------
# Tests
# Format:
# EXPECTED_CODE|URL
# --------------------------------------------------
TESTS=(
"200|http://localhost"
"403|http://localhost/admin/"
)
for test in "${TESTS[@]}"; do
EXPECTED_CODE="${test%%|*}"
URL="${test#*|}"
if [[ "$URL" == https:* ]]; then
RESPONSE_CODE=$(curl -k $CURL_OPTS "$URL")
else
RESPONSE_CODE=$(curl $CURL_OPTS "$URL")
fi
if [[ "$RESPONSE_CODE" == "$EXPECTED_CODE" ]]; then
printf "\033[0;32mPASS\tExpect:%s\tResponse:%s\t%s\033[0m\n" "$EXPECTED_CODE" "$RESPONSE_CODE" "$URL"
else
printf "\033[0;31mFAIL\tExpect:%s\tResponse:%s\t%s\033[0m\n" "$EXPECTED_CODE" "$RESPONSE_CODE" "$URL"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment