#!/bin/bash # Debug: Print current directory echo "Current directory: $(pwd)" # Source environment variables from .env file if it exists if [ -f .env ]; then export $(grep -v '^#' .env | xargs) echo "Sourced environment variables from .env" echo "NODE_ENV=${NODE_ENV:-(not set)}" echo "BYPASS_AUTH_IN_DEV=${BYPASS_AUTH_IN_DEV:-(not set)}" echo "After sourcing, NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-(not set)}" fi # Source environment variables from .env.local file if it exists in the current directory if [ -f .env.local ]; then export $(grep -v '^#' .env.local | xargs) echo "Sourced environment variables from .env.local" echo "NODE_ENV=${NODE_ENV:-(not set)}" echo "BYPASS_AUTH_IN_DEV=${BYPASS_AUTH_IN_DEV:-(not set)}" echo "After sourcing current dir .env, NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-(not set)}" fi # If API key is not set, prompt for it API_KEY=${NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY:-$(read -p "Enter your NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY: " key; echo $key)} if [ -z "$API_KEY" ]; then echo "ERROR: API key is required for this test" exit 1 fi echo "Testing rate limit with API_KEY=${API_KEY:0:10}..." # Only show first 10 chars for security # Define test data for news creation current_date=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z") test_data='{ "title": "Test News Article", "content": "This is a test article for rate limiting", "url": "https://example.com/test-article", "source": "Rate Limit Test", "category": "Tech", "article_date": "'"$current_date"'" }' # Run requests until we hit the limit for i in {1..15}; do # Execute request, hide all curl output response=$(curl -s -i -X POST "http://localhost:3000/api/news" \ -H "Content-Type: application/json" \ -H "x-api-key: $API_KEY" \ -d "$test_data" 2>/dev/null) # Extract only what we need status=$(echo "$response" | grep -m 1 "HTTP" | awk '{print $2}') remaining=$(echo "$response" | grep -m 1 -i "X-RateLimit-Remaining" | awk '{print $2}' | tr -d '\r') limit=$(echo "$response" | grep -m 1 -i "X-RateLimit-Limit" | awk '{print $2}' | tr -d '\r') reset=$(echo "$response" | grep -m 1 -i "X-RateLimit-Reset" | awk '{print $2}' | tr -d '\r') # Extract body and truncate to 100 characters body=$(echo "$response" | awk -v RS='\r\n\r\n' 'END{print}') truncated_body="${body:0:100}" if [ ${#body} -gt 100 ]; then truncated_body+="..." fi # Output with truncated response echo "Request $i: Status $status, Remaining: ${remaining:-N/A} of ${limit:-N/A}, Reset: ${reset:-N/A}" echo "Response (truncated): $truncated_body" echo "" # Exit on rate limit if [ "$status" == "429" ]; then echo "✓ Rate limit hit after $i requests - working correctly!" exit 0 fi sleep 0.5 done echo "Completed all requests without hitting rate limit."