fix(gateway): preserve duplicate auth behavior

This commit is contained in:
Yanislav Igonin 2026-07-15 23:18:27 +04:00
parent 35f0ee8510
commit 94adc28e65
2 changed files with 38 additions and 1 deletions

View File

@ -7,7 +7,11 @@ import (
)
func Ping(c *fiber.Ctx) error {
headerKey := c.Get("Authorization")
headerValues := c.GetReqHeaders()["Authorization"]
headerKey := ""
if len(headerValues) > 0 {
headerKey = headerValues[len(headerValues)-1]
}
if config.App.Gateway.ApiKey != headerKey {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Unauthorized"})
}

View File

@ -0,0 +1,33 @@
package gateway
import (
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"micrach/config"
)
func TestPingUsesLastAuthorizationHeader(t *testing.T) {
previousConfig := config.App
t.Cleanup(func() {
config.App = previousConfig
})
config.App.Gateway.ApiKey = "configured-key"
app := fiber.New()
app.Get("/api/ping", Ping)
req := httptest.NewRequest("GET", "/api/ping", nil)
req.Header.Add("Authorization", "wrong-key")
req.Header.Add("Authorization", "configured-key")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("test ping request: %v", err)
}
if resp.StatusCode != fiber.StatusOK {
t.Fatalf("expected status %d when final Authorization header matches, got %d", fiber.StatusOK, resp.StatusCode)
}
}