veza/veza-backend-api/tests/verify_timeout.sh
2025-12-12 21:34:34 -05:00

77 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
set -e
# Configuration
API_URL="http://localhost:8080"
TIMEOUT_DURATION="2s" # Short timeout for testing
echo "🚀 Starting verification of Context Timeouts (MOD-P1-009)..."
# 1. Start the server with a short timeout
echo " - Starting server with HANDLER_TIMEOUT=$TIMEOUT_DURATION..."
export HANDLER_TIMEOUT=$TIMEOUT_DURATION
export APP_ENV=development
# We need to run the server in the background
# Creating a temporary test handler if needed, but for now we rely on existing endpoints
# or we can create a specific test endpoint in a Go test file which is cleaner.
# BETTER APPROACH: Use Go Test for verification
echo " - Running Go integration test for timeout..."
cat > internal/middleware/timeout_integration_test.go <<EOF
package middleware_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"veza-backend-api/internal/middleware"
)
func TestTimeoutMiddleware_Integration(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
// Configure timeout of 100ms
router.Use(middleware.Timeout(100 * time.Millisecond))
router.GET("/slow", func(c *gin.Context) {
// Simulate work longer than timeout
time.Sleep(200 * time.Millisecond)
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
router.GET("/fast", func(c *gin.Context) {
// Simulate fast work
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
t.Run("SlowRequest_ShouldTimeout", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/slow", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusGatewayTimeout, w.Code)
assert.Contains(t, w.Body.String(), "Request Timeout")
})
t.Run("FastRequest_ShouldPass", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/fast", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
})
}
EOF
# Run the test
go test -v internal/middleware/timeout_integration_test.go
# Cleanup
rm internal/middleware/timeout_integration_test.go
echo "✅ Context Timeout verification passed!"