diff --git a/.github/workflows/scripts.tests.yml b/.github/workflows/scripts.tests.yml index 9ea513f..bc74687 100644 --- a/.github/workflows/scripts.tests.yml +++ b/.github/workflows/scripts.tests.yml @@ -105,3 +105,13 @@ jobs: echo "git-ref did not return 'branch'." fi + get-request: + name: get-request script + runs-on: ubuntu-latest + steps: + + - uses: actions/checkout@v4 + - uses: jonpugh/goatscripts@main + - run: | + run-with-summary \ + ./tests/get-request-test diff --git a/src/get-request b/src/get-request new file mode 100755 index 0000000..7780782 --- /dev/null +++ b/src/get-request @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +usage() { + echo -n " +get-request METRIC URL [OPTIONS] + +Get specific information from a web request. + +Possible METRICs: + + time + The total time the request took in milliseconds. + + server-time + The server response time (time to first byte) in milliseconds. + + size [FORMAT] + The response body size. FORMAT can be: byte (default), KB, MB + + http-code + The HTTP status code returned by the server. + + body + The response body content. + + headers + The response headers. + + @TODO: more options + +Examples + + get-request time https://www.google.com + get-request server-time https://www.google.com + get-request size https://www.google.com + get-request size https://www.google.com KB + get-request http-code https://www.google.com + get-request body https://www.google.com + get-request headers https://www.google.com + +" +} + +if [[ $# -lt 2 ]]; then + usage + exit 1 +fi + +METRIC=$1 +URL=$2 +FORMAT=${3:-byte} + +case $METRIC in + time) + curl -s -o /dev/null -w '%{time_total}' "$URL" | awk '{printf "%.0f\n", $1 * 1000}' + ;; + server-time) + curl -s -o /dev/null -w '%{time_starttransfer}' "$URL" | awk '{printf "%.0f\n", $1 * 1000}' + ;; + size) + size_bytes=$(curl -s -o /dev/null -w '%{size_download}' "$URL") + case "$FORMAT" in + byte) + printf "%.0f\n" "$size_bytes" + ;; + KB) + awk "BEGIN {printf \"%.2f\n\", $size_bytes / 1024}" + ;; + MB) + awk "BEGIN {printf \"%.2f\n\", $size_bytes / (1024 * 1024)}" + ;; + *) + echo "Unknown format: $FORMAT (use byte, KB, or MB)" + exit 1 + ;; + esac + ;; + http-code) + curl -s -o /dev/null -w '%{http_code}' "$URL" + echo "" + ;; + body) + curl -s "$URL" + ;; + headers) + curl -s -i "$URL" | sed '/^$/q' + ;; + *) + echo "Unknown metric: $METRIC" + usage + exit 1 + ;; +esac diff --git a/tests/get-request-test b/tests/get-request-test new file mode 100755 index 0000000..d9ee9bb --- /dev/null +++ b/tests/get-request-test @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# Test script for get-request + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +GET_REQUEST="$SCRIPT_DIR/src/get-request" +TEST_URL="https://api.github.com" + +# Colors for output +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +test_count=0 +passed_count=0 + +run_test() { + local test_name=$1 + local metric=$2 + local args=${3:-} + + test_count=$((test_count + 1)) + echo -n "Test $test_count: $test_name ... " + + if output=$("$GET_REQUEST" "$metric" "$TEST_URL" $args 2>&1); then + echo -e "${GREEN}PASSED${NC}" + echo " Output: $output" + passed_count=$((passed_count + 1)) + else + echo -e "${RED}FAILED${NC}" + echo " Error: $output" + fi + echo +} + +echo "Running get-request tests against $TEST_URL" +echo "===========================================" +echo "" + +run_test "time metric" "time" +run_test "server-time metric" "server-time" +run_test "size metric (bytes)" "size" +run_test "size metric (KB)" "size" "KB" +run_test "size metric (MB)" "size" "MB" +run_test "http-code metric" "http-code" +run_test "body metric" "body" +run_test "headers metric" "headers" + +echo "" +echo "===========================================" +echo "Tests completed: $passed_count/$test_count passed" + +if [ $passed_count -eq $test_count ]; then + exit 0 +else + exit 1 +fi