Branchwell

AI Resource Pro / website / scripts / test-rate-limit.sh

1#!/bin/bash
2
3# Debug: Print current directory
4echo "Current directory: $(pwd)"
5
6# Source environment variables from .env file if it exists
7if [ -f .env ]; then
8 export $(grep -v '^#' .env | xargs)
9 echo "Sourced environment variables from .env"
10 echo "NODE_ENV=${NODE_ENV:-(not set)}"
11 echo "BYPASS_AUTH_IN_DEV=${BYPASS_AUTH_IN_DEV:-(not set)}"
12 echo "After sourcing, NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-(not set)}"
13fi
14
15# Source environment variables from .env.local file if it exists in the current directory
16if [ -f .env.local ]; then
17 export $(grep -v '^#' .env.local | xargs)
18 echo "Sourced environment variables from .env.local"
19 echo "NODE_ENV=${NODE_ENV:-(not set)}"
20 echo "BYPASS_AUTH_IN_DEV=${BYPASS_AUTH_IN_DEV:-(not set)}"
21 echo "After sourcing current dir .env, NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-(not set)}"
22fi
23
24# If API key is not set, prompt for it
25API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-$(read -p "Enter your NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY: " key; echo $key)}
26
27if [ -z "$API_KEY" ]; then
28 echo "ERROR: API key is required for this test"
29 exit 1
30fi
31
32echo "Testing rate limit with API_KEY=${API_KEY:0:10}..." # Only show first 10 chars for security
33
34# Define test data for news creation
35current_date=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
36test_data='{
37 "title": "Test News Article",
38 "content": "This is a test article for rate limiting",
39 "url": "https://example.com/test-article",
40 "source": "Rate Limit Test",
41 "category": "Tech",
42 "article_date": "'"$current_date"'"
43}'
44
45# Run requests until we hit the limit
46for i in {1..15}; do
47 # Execute request, hide all curl output
48 response=$(curl -s -i -X POST "http://localhost:3000/api/news" \
49 -H "Content-Type: application/json" \
50 -H "x-api-key: $API_KEY" \
51 -d "$test_data" 2>/dev/null)
52
53 # Extract only what we need
54 status=$(echo "$response" | grep -m 1 "HTTP" | awk '{print $2}')
55 remaining=$(echo "$response" | grep -m 1 -i "X-RateLimit-Remaining" | awk '{print $2}' | tr -d '\r')
56 limit=$(echo "$response" | grep -m 1 -i "X-RateLimit-Limit" | awk '{print $2}' | tr -d '\r')
57 reset=$(echo "$response" | grep -m 1 -i "X-RateLimit-Reset" | awk '{print $2}' | tr -d '\r')
58
59 # Extract body and truncate to 100 characters
60 body=$(echo "$response" | awk -v RS='\r\n\r\n' 'END{print}')
61 truncated_body="${body:0:100}"
62 if [ ${#body} -gt 100 ]; then
63 truncated_body+="..."
64 fi
65
66 # Output with truncated response
67 echo "Request $i: Status $status, Remaining: ${remaining:-N/A} of ${limit:-N/A}, Reset: ${reset:-N/A}"
68 echo "Response (truncated): $truncated_body"
69 echo ""
70
71 # Exit on rate limit
72 if [ "$status" == "429" ]; then
73 echo "✓ Rate limit hit after $i requests - working correctly!"
74 exit 0
75 fi
76
77 sleep 0.5
78done
79
80echo "Completed all requests without hitting rate limit."