#!/bin/bash # Comprehensive test script for routing reliability set -e echo "=== Comprehensive Routing Test ===" echo "This test verifies that /dashboard and /api/stats are ALWAYS handled internally" echo "" # Kill any existing proxy pkill -f rproxy || true sleep 1 # Start the proxy with debug output echo "Starting proxy with debug logging..." DEBUG=1 ./rproxy > proxy.log 2>&1 & PROXY_PID=$! sleep 2 # Check if proxy is running if ! ps -p $PROXY_PID > /dev/null; then echo "ERROR: Proxy failed to start" cat proxy.log exit 1 fi echo "Proxy started with PID: $PROXY_PID" echo "" # Function to test a URL and check response test_url() { local url=$1 local expected_pattern=$2 local test_name=$3 echo "Testing: $test_name" response=$(curl -s -w "\nHTTP_CODE:%{http_code}" "$url" 2>/dev/null || true) http_code=$(echo "$response" | grep "HTTP_CODE:" | cut -d: -f2) body=$(echo "$response" | grep -v "HTTP_CODE:") if [ "$http_code" = "200" ]; then if echo "$body" | grep -q "$expected_pattern"; then echo " ✓ Success: Got expected response" return 0 else echo " ✗ FAIL: Got 200 but wrong content" echo " Expected pattern: $expected_pattern" echo " Got: ${body:0:100}..." return 1 fi else echo " ✗ FAIL: Got HTTP $http_code instead of 200" return 1 fi } # Run tests echo "=== Test 1: Basic Dashboard Access ===" test_url "http://localhost:8888/dashboard" "Reverse Proxy Monitor" "Dashboard" echo "" echo "=== Test 2: Basic API Stats Access ===" test_url "http://localhost:8888/api/stats" "cpu_percent" "API Stats" echo "" echo "=== Test 3: Multiple Sequential Dashboard Requests ===" for i in {1..5}; do test_url "http://localhost:8888/dashboard" "Reverse Proxy Monitor" "Dashboard request $i" done echo "" echo "=== Test 4: Multiple Sequential API Requests ===" for i in {1..5}; do test_url "http://localhost:8888/api/stats" "cpu_percent" "API request $i" done echo "" echo "=== Test 5: Mixed Dashboard and API Requests ===" for i in {1..3}; do test_url "http://localhost:8888/dashboard" "Reverse Proxy Monitor" "Dashboard request $i" test_url "http://localhost:8888/api/stats" "cpu_percent" "API request $i" done echo "" echo "=== Test 6: Concurrent Dashboard Requests ===" echo "Sending 10 concurrent dashboard requests..." for i in {1..10}; do ( response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:8888/dashboard" 2>/dev/null) if [ "$response" = "200" ]; then echo " Request $i: ✓ 200 OK" else echo " Request $i: ✗ Got $response" fi ) & done wait echo "" echo "=== Test 7: Concurrent API Requests ===" echo "Sending 10 concurrent API requests..." for i in {1..10}; do ( response=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:8888/api/stats" 2>/dev/null) if [ "$response" = "200" ]; then echo " Request $i: ✓ 200 OK" else echo " Request $i: ✗ Got $response" fi ) & done wait echo "" echo "=== Test 8: Pipelined Requests (Keep-Alive) ===" echo "Testing pipelined requests with keep-alive..." (echo -ne "GET /dashboard HTTP/1.1\r\nHost: localhost\r\n\r\nGET /api/stats HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc localhost 8888 | grep -E "(Reverse Proxy Dashboard|vhost_stats)" | wc -l) > pipeline_result.txt 2>&1 pipeline_count=$(cat pipeline_result.txt) if [ "$pipeline_count" = "2" ]; then echo " ✓ Both pipelined requests were handled correctly" else echo " ✗ FAIL: Only $pipeline_count/2 pipelined requests handled correctly" fi echo "" echo "=== Test 9: Rapid Fire Mixed Requests ===" echo "Sending 50 rapid requests mixing dashboard, API, and proxy requests..." success=0 fail=0 for i in {1..50}; do case $((i % 3)) in 0) url="http://localhost:8888/dashboard" ;; 1) url="http://localhost:8888/api/stats" ;; 2) url="http://localhost:8888/" ;; # This should be proxied esac response=$(curl -s -o /dev/null -w "%{http_code}" "$url" 2>/dev/null || echo "000") if [ "$response" = "200" ] || [ "$response" = "502" ]; then ((success++)) else ((fail++)) echo " Request $i to $url failed with code: $response" fi done echo " Results: $success successful, $fail failed" echo "" echo "=== Test 10: Check Routing Log Patterns ===" echo "Analyzing proxy log for routing decisions..." dashboard_internal=$(grep -c "ROUTING-INTERNAL.*DASHBOARD" proxy.log || true) api_internal=$(grep -c "ROUTING-INTERNAL.*API STATS" proxy.log || true) dashboard_forward=$(grep -c "ROUTING-FORWARD.*dashboard" proxy.log || true) api_forward=$(grep -c "ROUTING-FORWARD.*api/stats" proxy.log || true) echo " Dashboard handled internally: $dashboard_internal times" echo " API stats handled internally: $api_internal times" echo " Dashboard incorrectly forwarded: $dashboard_forward times" echo " API stats incorrectly forwarded: $api_forward times" if [ "$dashboard_forward" -gt 0 ] || [ "$api_forward" -gt 0 ]; then echo " ✗ FAIL: Internal routes were incorrectly forwarded!" echo "" echo "=== Showing incorrect forwarding logs ===" grep "ROUTING-FORWARD.*dashboard\|ROUTING-FORWARD.*api/stats" proxy.log | head -10 || true else echo " ✓ Success: All internal routes handled correctly" fi echo "" echo "=== Cleanup ===" kill $PROXY_PID 2>/dev/null || true wait $PROXY_PID 2>/dev/null || true echo "Proxy stopped" echo "" echo "=== Test Summary ===" if [ "$dashboard_forward" -gt 0 ] || [ "$api_forward" -gt 0 ]; then echo "❌ TESTS FAILED: Internal routes were incorrectly forwarded" echo " Check proxy.log for details" exit 1 else echo "✅ ALL TESTS PASSED: Internal routes are reliably handled" exit 0 fi