#!/bin/bash # Test script for remote-local-storage application # This script starts the server and tests both saveLS and loadLS endpoints # with curl's --resolve flag to test multiple hostnames set -e # Define test variables PORT="3243" # Flags KEEP_FILES=false while getopts "k" FLAG; do case "${FLAG}" in k) KEEP_FILES=true ;; esac done # Create temporary directory for testing TEST_DIR=$(mktemp -d) echo "Using test directory: $TEST_DIR" echo "To keep files, run with: ./test.sh -k" echo "" # Cleanup function cleanup() { echo "Cleaning up..." kill $(jobs -p) 2>/dev/null || true if [ "$KEEP_FILES" = false ]; then rm -rf "$TEST_DIR" fi } # Set trap to always run cleanup on exit trap cleanup EXIT # Save binary path and cd into temp directory TEST_BINARY="$PWD/rls" cd "$TEST_DIR" echo "Starting remote-local-storage server on localhost:$PORT..." "$TEST_BINARY" -address="localhost:$PORT" & SERVER_PID=$! # Give the server a moment to start sleep 2 # Define dump files DUMP_FILE="localhost:$PORT-dump.json" EXAMPLE_DUMP_FILE="example.com:$PORT-dump.json" BASE_URL="http://localhost:$PORT" echo "Testing loadLS endpoint for localhost (should return empty JSON)..." curl -s "$BASE_URL/wrap/loadLS" | jq . echo "" echo "Testing saveLS endpoint with sample data for localhost..." SAMPLE_DATA='{"testKey":"testValue","anotherKey":"anotherValue"}' curl -s -X POST -H "Content-Type: application/json" \ --data "$SAMPLE_DATA" "$BASE_URL/wrap/saveLS" echo "" echo "Testing loadLS endpoint after saving (should return saved data)..." curl -s "$BASE_URL/wrap/loadLS" | jq . echo "" echo "Checking localhost dump file contents..." if [ -f "$DUMP_FILE" ]; then echo "Dump file exists at: $DUMP_FILE" jq . "$DUMP_FILE" else echo "Dump file not found!" exit 1 fi echo "" echo "Testing with --resolve flag for different hostname (example.com)..." echo "Testing loadLS endpoint for example.com..." curl -s --resolve "example.com:$PORT:127.0.0.1" \ "http://example.com:$PORT/wrap/loadLS" | jq . echo "" echo "Saving data for example.com..." EXAMPLE_DATA='{"exampleKey":"exampleValue","exampleNum":123}' curl -s -X POST --resolve "example.com:$PORT:127.0.0.1" \ -H "Content-Type: application/json" \ --data "$EXAMPLE_DATA" "http://example.com:$PORT/wrap/saveLS" # Wait a moment for server processing sleep 1 echo "" echo "Loading data for example.com..." curl -s --resolve "example.com:$PORT:127.0.0.1" \ "http://example.com:$PORT/wrap/loadLS" | jq . echo "" echo "Checking example.com dump file contents..." if [ -f "$EXAMPLE_DUMP_FILE" ]; then echo "Dump file exists at: $EXAMPLE_DUMP_FILE" jq . "$EXAMPLE_DUMP_FILE" echo "" echo "Verification:" echo " ✓ localhost dump file: $DUMP_FILE" echo " ✓ example.com dump file: $EXAMPLE_DUMP_FILE" echo " ✓ Two different dump files generated for two different hosts" echo "" if [ "$KEEP_FILES" = false ]; then echo "Files were created in $TEST_DIR" echo "Run: ls -la $TEST_DIR" else echo "Files kept in $TEST_DIR for inspection" fi echo "" echo "Test completed successfully!" else echo "Dump file not found!" exit 1 fi