108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
package fiber
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/valyala/fasthttp"
|
|
"go.lab260.ru/fielderr"
|
|
|
|
httpmodel "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/http"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service"
|
|
)
|
|
|
|
// TestTransportErrorMapping checks safe API errors for framework-generated failures.
|
|
func TestTransportErrorMapping(t *testing.T) {
|
|
t.Run("large body", func(t *testing.T) {
|
|
if normalizeTransportError(fiber.ErrRequestEntityTooLarge) != errTooLarge {
|
|
t.Fatal("incorrect body-limit error")
|
|
}
|
|
})
|
|
t.Run("method not allowed", func(t *testing.T) {
|
|
if normalizeTransportError(fiber.ErrMethodNotAllowed) != service.ErrNotFound {
|
|
t.Fatal("unexpected method error")
|
|
}
|
|
})
|
|
t.Run("invalid request", func(t *testing.T) {
|
|
if normalizeTransportError(fiber.ErrBadRequest) != service.ErrInvalid {
|
|
t.Fatal("unexpected input error")
|
|
}
|
|
})
|
|
t.Run("unexpected framework failure", func(t *testing.T) {
|
|
if normalizeTransportError(fiber.ErrInternalServerError) != errInternal {
|
|
t.Fatal("unexpected failure escaped normalization")
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestController_handleErr writes a safe, correlated fallback response.
|
|
func TestController_handleErr(t *testing.T) {
|
|
t.Run("Negative: unknown internal error", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
raw := &fasthttp.RequestCtx{}
|
|
ctx := ctrl.server.AcquireCtx(raw)
|
|
defer ctrl.server.ReleaseCtx(ctx)
|
|
|
|
// Act.
|
|
err := ctrl.handleErr(ctx, errors.New("private storage secret"))
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if raw.Response.StatusCode() != http.StatusInternalServerError {
|
|
t.Fatalf("status=%d", raw.Response.StatusCode())
|
|
}
|
|
var body httpmodel.ErrorResponse
|
|
if err := json.Unmarshal(raw.Response.Body(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
requestID := string(raw.Response.Header.Peek(headerRequestID))
|
|
if requestID == "" || body.Error.RequestID != requestID {
|
|
t.Fatalf("uncorrelated error: %+v, header=%q", body, requestID)
|
|
}
|
|
if body.Error.Code != "internal_error" || body.Error.Message != "internal server error" {
|
|
t.Fatalf("unsafe fallback: %+v", body)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: missing status mapping", func(t *testing.T) {
|
|
// Arrange.
|
|
ctrl := testController(t, nil)
|
|
registry, err := fielderr.NewRegistry()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctrl.errorRegistry = registry
|
|
raw := &fasthttp.RequestCtx{}
|
|
ctx := ctrl.server.AcquireCtx(raw)
|
|
defer ctrl.server.ReleaseCtx(ctx)
|
|
|
|
// Act.
|
|
err = ctrl.handleErr(ctx, service.ErrInvalid)
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if raw.Response.StatusCode() != http.StatusInternalServerError {
|
|
t.Fatalf("status=%d", raw.Response.StatusCode())
|
|
}
|
|
var body httpmodel.ErrorResponse
|
|
if err := json.Unmarshal(raw.Response.Body(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
requestID := string(raw.Response.Header.Peek(headerRequestID))
|
|
if requestID == "" || body.Error.RequestID != requestID {
|
|
t.Fatalf("uncorrelated error: %+v, header=%q", body, requestID)
|
|
}
|
|
if body.Error.Code != "internal_error" || body.Error.Message != "internal server error" {
|
|
t.Fatalf("unsafe fallback: %+v", body)
|
|
}
|
|
})
|
|
}
|