diff --git a/.github/workflows/test-rate-limits.yml b/.github/workflows/test-rate-limits.yml new file mode 100644 index 0000000..9c76e10 --- /dev/null +++ b/.github/workflows/test-rate-limits.yml @@ -0,0 +1,111 @@ +name: Test Rate Limits + +on: + workflow_dispatch: + inputs: + test_count: + description: "Number of requests to make (default: 110)" + required: false + default: "110" + type: string + +env: + CREATE_DB_WORKER_URL: ${{ secrets.CREATE_DB_WORKER_URL }} + CLAIM_DB_WORKER_URL: ${{ secrets.CLAIM_DB_WORKER_URL }} + +jobs: + test-rate-limits: + name: Test Rate Limits + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Test Create DB Worker Rate Limits + run: | + echo "Testing create-db-worker rate limits..." + TEST_COUNT=${INPUT_TEST_COUNT:-110} + + echo "Making $TEST_COUNT requests to $CREATE_DB_WORKER_URL/test" + + success_count=0 + rate_limited_count=0 + error_count=0 + + for i in $(seq 1 $TEST_COUNT); do + echo "Request $i/$TEST_COUNT" + response=$(curl -s -w "%{http_code}" -o /tmp/response_$i.json "$CREATE_DB_WORKER_URL/test") + status_code=${response: -3} + + if [ "$status_code" = "200" ]; then + echo " โœ… Success (200)" + ((success_count++)) + elif [ "$status_code" = "429" ]; then + echo " โš ๏ธ Rate Limited (429)" + ((rate_limited_count++)) + else + echo " โŒ Error ($status_code)" + ((error_count++)) + fi + + # Small delay between requests + sleep 0.1 + done + + echo "" + echo "๐Ÿ“Š Create DB Worker Results:" + echo " Success: $success_count" + echo " Rate Limited: $rate_limited_count" + echo " Errors: $error_count" + echo " Total: $TEST_COUNT" + + - name: Test Claim DB Worker Rate Limits + run: | + echo "Testing claim-db-worker rate limits..." + TEST_COUNT=${INPUT_TEST_COUNT:-110} + + echo "Making $TEST_COUNT requests to $CLAIM_DB_WORKER_URL/test" + + success_count=0 + rate_limited_count=0 + error_count=0 + + for i in $(seq 1 $TEST_COUNT); do + echo "Request $i/$TEST_COUNT" + response=$(curl -s -w "%{http_code}" -o /tmp/response_$i.json "$CLAIM_DB_WORKER_URL/test") + status_code=${response: -3} + + if [ "$status_code" = "200" ]; then + echo " โœ… Success (200)" + ((success_count++)) + elif [ "$status_code" = "429" ]; then + echo " โš ๏ธ Rate Limited (429)" + ((rate_limited_count++)) + else + echo " โŒ Error ($status_code)" + ((error_count++)) + fi + + # Small delay between requests + sleep 0.1 + done + + echo "" + echo "๐Ÿ“Š Claim DB Worker Results:" + echo " Success: $success_count" + echo " Rate Limited: $rate_limited_count" + echo " Errors: $error_count" + echo " Total: $TEST_COUNT" + + - name: Summary + run: | + echo "๐ŸŽฏ Rate Limit Testing Complete!" + echo "" + echo "Both workers have been tested with ${INPUT_TEST_COUNT:-110} requests each." + echo "Check the logs above to see the rate limiting behavior." + echo "" + echo "Expected behavior:" + echo "- First few requests should succeed (200)" + echo "- Later requests should be rate limited (429)" + echo "- This confirms rate limiting is working correctly" diff --git a/claim-db-worker/src/index.ts b/claim-db-worker/src/index.ts index 302e25d..33f97ca 100644 --- a/claim-db-worker/src/index.ts +++ b/claim-db-worker/src/index.ts @@ -57,6 +57,22 @@ export default { const url = new URL(request.url); + // --- Test endpoint for rate limit testing --- + if (url.pathname === '/test' && request.method === 'GET') { + return new Response( + JSON.stringify({ + status: 'success', + service: 'claim-db-worker', + timestamp: Date.now(), + message: 'Rate limit test endpoint - if you see this, rate limiting passed', + }), + { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }, + ); + } + // --- OAuth Callback Handler --- if (url.pathname === '/auth/callback') { const code = url.searchParams.get('code'); diff --git a/create-db-worker/src/index.ts b/create-db-worker/src/index.ts index 963bf58..038ec70 100644 --- a/create-db-worker/src/index.ts +++ b/create-db-worker/src/index.ts @@ -20,6 +20,22 @@ export default { const url = new URL(request.url); + // --- Test endpoint for rate limit testing --- + if (url.pathname === '/test' && request.method === 'GET') { + return new Response( + JSON.stringify({ + status: 'success', + service: 'create-db-worker', + timestamp: Date.now(), + message: 'Rate limit test endpoint - if you see this, rate limiting passed', + }), + { + status: 200, + headers: { 'Content-Type': 'application/json' }, + }, + ); + } + // --- Health check route --- if (url.pathname === '/health' && request.method === 'GET') { return new Response(JSON.stringify({ status: 'ok', service: 'create-db', timestamp: Date.now() }), { diff --git a/tests/test-rate-limits.sh b/tests/test-rate-limits.sh new file mode 100755 index 0000000..a17f514 --- /dev/null +++ b/tests/test-rate-limits.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Test Rate Limits Script +# Usage: ./tests/test-rate-limits.sh [test_count] [create_db_url] [claim_db_url] + +# Default values +TEST_COUNT=${1:-110} +CREATE_DB_URL=${2:-"http://127.0.0.1:8787"} +CLAIM_DB_URL=${3:-"http://127.0.0.1:9999"} + +echo "๐Ÿงช Testing Rate Limits" +echo "======================" +echo "Test Count: $TEST_COUNT" +echo "Create DB URL: $CREATE_DB_URL" +echo "Claim DB URL: $CLAIM_DB_URL" +echo "" + +# Function to test a worker +test_worker() { + local worker_name=$1 + local worker_url=$2 + local endpoint="$worker_url/test" + + echo "๐Ÿ“Š Testing $worker_name rate limits..." + echo "Making $TEST_COUNT requests to $endpoint" + echo "" + + success_count=0 + rate_limited_count=0 + error_count=0 + + for i in $(seq 1 $TEST_COUNT); do + echo -n "Request $i/$TEST_COUNT: " + + # Make the request and capture both response body and status code + response=$(curl -s -w "%{http_code}" -o /tmp/response_$i.json "$endpoint" 2>/dev/null) + status_code=${response: -3} + + case $status_code in + 200) + echo "โœ… Success (200)" + ((success_count++)) + ;; + 429) + echo "โš ๏ธ Rate Limited (429)" + ((rate_limited_count++)) + ;; + *) + echo "โŒ Error ($status_code)" + ((error_count++)) + ;; + esac + + # Small delay between requests + sleep 0.1 + done + + echo "" + echo "๐Ÿ“Š $worker_name Results:" + echo " Success: $success_count" + echo " Rate Limited: $rate_limited_count" + echo " Errors: $error_count" + echo " Total: $TEST_COUNT" + echo "" +} + +# Test both workers +test_worker "Create DB Worker" "$CREATE_DB_URL" +test_worker "Claim DB Worker" "$CLAIM_DB_URL" + +echo "๐ŸŽฏ Rate Limit Testing Complete!" +echo "" +echo "Expected behavior:" +echo "- First few requests should succeed (200)" +echo "- Later requests should be rate limited (429)" +echo "- This confirms rate limiting is working correctly" +echo "" +echo "๐Ÿ’ก To test with your actual deployed URLs, run:" +echo " ./tests/test-rate-limits.sh 110 https://create-db-temp.prisma.io https://create-db.prisma.io" \ No newline at end of file