ploughshares/test_api_curl.sh

110 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
# Colors for better readability
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Base URLs to test
DEV_URL="http://localhost:5005"
PROD_URL="https://ploughshares.nixc.us"
# Default to development URL unless specified
URL=${1:-$DEV_URL}
echo -e "${BLUE}Testing API endpoints with curl for ${URL}${NC}"
echo "=================================================="
# Function to test API endpoint with different curl options
test_api() {
local endpoint=$1
local method=$2
local description=$3
local headers=$4
local data=$5
local full_url="${URL}${endpoint}"
echo -e "\n${YELLOW}Test: ${description}${NC}"
echo "------------------------------------------------"
# Build curl command based on parameters
cmd="curl -s"
# Add method if specified
if [ -n "$method" ]; then
cmd="$cmd -X $method"
fi
# Add headers if specified
if [ -n "$headers" ]; then
cmd="$cmd $headers"
fi
# Add data if specified
if [ -n "$data" ]; then
cmd="$cmd -d '$data'"
fi
# Add URL
cmd="$cmd \"$full_url\""
# Show the command being executed
echo -e "${BLUE}Command:${NC} $cmd"
# Execute the command
echo -e "${BLUE}Response:${NC}"
eval $cmd | jq 2>/dev/null || echo "Response is not valid JSON"
echo "------------------------------------------------"
}
# Test 1: Basic GET request to API test endpoint
test_api "/api/test" "GET" "Basic GET request to API test endpoint" "" ""
# Test 2: GET request with Accept header
test_api "/api/test" "GET" "GET request with Accept: application/json header" "-H \"Accept: application/json\"" ""
# Test 3: OPTIONS request (CORS preflight)
test_api "/api/test" "OPTIONS" "OPTIONS request (CORS preflight)" "-H \"Origin: http://example.com\"" ""
# Test 4: POST request with JSON data
test_api "/api/transactions" "POST" "POST request with JSON data" "-H \"Content-Type: application/json\"" "{\"test\": \"data\"}"
# Test 5: GET request with custom User-Agent
test_api "/api/test" "GET" "GET request with custom User-Agent" "-H \"User-Agent: CustomClient/1.0\"" ""
# Test 6: GET request with Authorization header
test_api "/api/test" "GET" "GET request with Authorization header" "-H \"Authorization: Bearer test-token\"" ""
# Test 7: POST request with form data
test_api "/api/transactions" "POST" "POST request with form data" "-H \"Content-Type: application/x-www-form-urlencoded\"" "test=data"
# Test 8: GET request with multiple custom headers
test_api "/api/test" "GET" "GET request with multiple custom headers" "-H \"Accept: application/json\" -H \"X-Custom-Header: test\"" ""
echo -e "\n${BLUE}API curl tests completed for ${URL}${NC}"
# Now test the API with a more complex curl command that simulates a real client
echo -e "\n${YELLOW}Simulating a real API client with complex curl command${NC}"
echo "------------------------------------------------"
echo -e "${BLUE}Command:${NC}"
echo "curl -v -X POST \"${URL}/api/transactions\" \\"
echo " -H \"Content-Type: application/json\" \\"
echo " -H \"Accept: application/json\" \\"
echo " -H \"User-Agent: APIClient/1.0\" \\"
echo " -H \"Authorization: Bearer test-token\" \\"
echo " -d '{\"transaction_type\":\"test\",\"amount\":100,\"recipient\":\"Test Corp\"}'"
echo -e "\n${BLUE}Response:${NC}"
curl -v -X POST "${URL}/api/transactions" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "User-Agent: APIClient/1.0" \
-H "Authorization: Bearer test-token" \
-d '{"transaction_type":"test","amount":100,"recipient":"Test Corp"}' 2>&1
echo -e "\n------------------------------------------------"
echo -e "${GREEN}All tests completed.${NC}"