Files
Charon/backend/internal/api/middleware/request_id_test.go
GitHub Actions 63cebf07ab Refactor services and improve error handling
- Updated file permissions in certificate_service_test.go and log_service_test.go to use octal notation.
- Added a new doc.go file to document the services package.
- Enhanced error handling in docker_service.go, log_service.go, notification_service.go, proxyhost_service.go, remoteserver_service.go, update_service.go, and uptime_service.go by logging errors when closing resources.
- Improved log_service.go to simplify log file processing and deduplication.
- Introduced CRUD tests for notification templates in notification_service_template_test.go.
- Removed the obsolete python_compile_check.sh script.
- Updated notification_service.go to improve template management functions.
- Added tests for uptime service notifications in uptime_service_notification_test.go.
2025-12-08 05:55:17 +00:00

38 lines
861 B
Go

package middleware
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/Wikid82/charon/backend/internal/logger"
"github.com/gin-gonic/gin"
)
func TestRequestIDAddsHeaderAndLogger(t *testing.T) {
buf := &bytes.Buffer{}
logger.Init(true, buf)
router := gin.New()
router.Use(RequestID())
router.GET("/test", func(c *gin.Context) {
// Ensure logger exists in context and header is present
if _, ok := c.Get("logger"); !ok {
t.Fatalf("expected request-scoped logger in context")
}
c.String(200, "ok")
})
req := httptest.NewRequest(http.MethodGet, "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", w.Code)
}
if w.Header().Get(RequestIDHeader) == "" {
t.Fatalf("expected response to include X-Request-ID header")
}
}