AtlatestRepositorysigil-http

sigil-http / tree / testtest-integration.sh

1#!/usr/bin/env bash
2# Integration tests for (sigil http server) Phase 2 features
3#
4# Tests:
5# - GET requests
6# - POST requests with body
7# - Request body reading with Content-Length
8
9set -e
11SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
13SERVER_LOG="/tmp/sigil-http-test.log"
14PORT=8099
16# Ensure we're in the project root
17cd "$PROJECT_ROOT"
19# Kill any existing test server
20pkill -f "integration-server" 2>/dev/null || true
21sleep 1
23# Compile the test server if needed
24echo "Compiling test server..."
25./build/bin/sgl-boot --compile packages/sigil-http/test/integration-server.sgl \
26 packages/sigil-http/lib/test/integration-server.sgb
28# Start server on a different port for testing
29echo "Starting test server on port $PORT..."
30./build/bin/sigil -L packages/sigil-http/lib packages/sigil-http/lib/test/integration-server.sgb > "$SERVER_LOG" 2>&1 &
31SERVER_PID=$!
33# Wait for server to start
34sleep 3
36# Check if server is running
37if ! kill -0 $SERVER_PID 2>/dev/null; then
38 echo "FAIL: Server failed to start"
39 cat "$SERVER_LOG"
40 exit 1
41fi
43FAILED=0
45run_test() {
46 local name="$1"
47 local expected="$2"
48 local actual="$3"
50 if [ "$expected" = "$actual" ]; then
51 echo "PASS: $name"
52 else
53 echo "FAIL: $name"
54 echo " Expected: $expected"
55 echo " Actual: $actual"
56 FAILED=1
57 fi
60# Test 1: Simple GET request
61echo ""
62echo "=== Test 1: Simple GET /hello ==="
63RESULT=$(curl -s --max-time 5 http://localhost:$PORT/hello)
64run_test "GET /hello returns 'Hello, World!'" "Hello, World!" "$RESULT"
66# Test 2: GET returns 404 for unknown path
67echo ""
68echo "=== Test 2: GET unknown path returns 404 ==="
69RESULT=$(curl -s --max-time 5 -o /dev/null -w "%{http_code}" http://localhost:$PORT/unknown)
70run_test "GET /unknown returns 404" "404" "$RESULT"
72# Test 3: POST with body
73echo ""
74echo "=== Test 3: POST with body ==="
75RESULT=$(curl -s --max-time 5 -X POST -d "test body data" http://localhost:$PORT/echo-body)
76run_test "POST /echo-body echoes body" "test body data" "$RESULT"
78# Test 4: POST without body
79echo ""
80echo "=== Test 4: POST without body ==="
81RESULT=$(curl -s --max-time 5 -X POST http://localhost:$PORT/echo-body)
82run_test "POST /echo-body without body" "(no body)" "$RESULT"
84# Test 5: POST with larger body
85echo ""
86echo "=== Test 5: POST with larger body ==="
87LARGE_BODY="This is a larger body with multiple lines.
88Line 2 of the body.
89Line 3 with some special chars: <>\"'&
90End of body."
91RESULT=$(curl -s --max-time 5 -X POST -d "$LARGE_BODY" http://localhost:$PORT/echo-body)
92run_test "POST /echo-body with large body" "$LARGE_BODY" "$RESULT"
94# Cleanup
95echo ""
96echo "Cleaning up..."
97kill $SERVER_PID 2>/dev/null || true
98wait $SERVER_PID 2>/dev/null || true
100# Summary
101echo ""
102if [ $FAILED -eq 0 ]; then
103 echo "All tests passed!"
104 exit 0
105else
106 echo "Some tests failed!"
107 echo ""
108 echo "Server log:"
109 cat "$SERVER_LOG"
110 exit 1
111fi