Created
January 9, 2026 11:04
-
-
Save jmwebservices/ae9ecfdf7cad8c2f01c2d331d0a567bd to your computer and use it in GitHub Desktop.
cURL script that tests the response codes of URLs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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