Files

212 lines
5.6 KiB
Go

package fiber
import (
"net/http"
"testing"
"testing/synctest"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
)
// TestController_InFlightAdmission checks rejection, recovery and health exemption independently.
func TestController_InFlightAdmission(t *testing.T) {
cases := []struct {
name string
release bool
path string
status int
}{
{"Negative: occupied execution slot", false, "/api/v1/users/me", http.StatusServiceUnavailable},
{"Positive: released execution slot", true, "/api/v1/users/me", http.StatusUnauthorized},
{"Positive: health bypasses occupied slot", false, "/health/live", http.StatusOK},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
ctrl := testController(t, func(cfg *config.Config) {
cfg.HTTP.Admission.MaxInFlight = 1
})
if !ctrl.admission.(*fixtureAdmission).Acquire() {
t.Fatal("fixture could not reserve slot")
}
if tc.release {
ctrl.admission.(*fixtureAdmission).Release()
} else {
t.Cleanup(ctrl.admission.(*fixtureAdmission).Release)
}
// Act.
status, body, err := request(ctrl, http.MethodGet, tc.path, "", nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, tc.status, body)
})
}
}
// TestController_RateAdmission checks exhausted tokens without timing-dependent HTTP setup.
func TestController_RateAdmission(t *testing.T) {
t.Run("/api/v1/users/me", func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
// Arrange.
ctrl := testController(t, func(cfg *config.Config) {
cfg.HTTP.Admission.RequestsPerSecond = 1
cfg.HTTP.Admission.Burst = 1
})
if !ctrl.admission.(*fixtureAdmission).Allow(time.Now()) {
t.Fatal("fixture token missing")
}
expected := http.StatusTooManyRequests
// Act.
status, body, err := request(ctrl, http.MethodGet, "/api/v1/users/me", "", nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, expected, body)
})
})
t.Run("/health/ready", func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
// Arrange.
ctrl := testController(t, func(cfg *config.Config) {
cfg.HTTP.Admission.RequestsPerSecond = 1
cfg.HTTP.Admission.Burst = 1
})
if !ctrl.admission.(*fixtureAdmission).Allow(time.Now()) {
t.Fatal("fixture token missing")
}
expected := http.StatusOK
// Act.
status, body, err := request(ctrl, http.MethodGet, "/health/ready", "", nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, expected, body)
})
})
}
// TestController_Draining checks each endpoint's admission policy during shutdown.
func TestController_Draining(t *testing.T) {
cases := []struct {
name string
path string
status int
}{
{"Negative: readiness disabled", "/health/ready", http.StatusServiceUnavailable},
{"Positive: liveness remains available", "/health/live", http.StatusOK},
{"Negative: business requests rejected", "/api/v1/users/me", http.StatusServiceUnavailable},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
ctrl.stopping.Store(true)
// Act.
status, body, err := request(ctrl, http.MethodGet, tc.path, "", nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, tc.status, body)
})
}
}
// TestController_UnknownRoute checks the public fallback for an unmatched path.
func TestController_UnknownRoute(t *testing.T) {
// Arrange.
ctrl := testController(t, nil)
// Act.
status, body, err := request(ctrl, http.MethodGet, "/missing", "", nil)
// Assert.
if err != nil {
t.Fatal(err)
}
assertHTTPStatus(t, status, http.StatusNotFound, body)
assertErrorCode(t, body, "not_found")
}
// TestController_New rejects each missing dependency independently.
func TestController_New(t *testing.T) {
t.Run("Negative: missing configuration", func(t *testing.T) {
// Arrange.
fixture := testController(t, nil)
// Act.
ctrl, err := New(fixture.log, nil, fixture.service, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness)
// Assert.
if err == nil || ctrl != nil {
t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err)
}
})
t.Run("Negative: missing logger", func(t *testing.T) {
// Arrange.
fixture := testController(t, nil)
// Act.
ctrl, err := New(nil, &fixture.cfg, fixture.service, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness)
// Assert.
if err == nil || ctrl != nil {
t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err)
}
})
t.Run("Negative: missing service", func(t *testing.T) {
// Arrange.
fixture := testController(t, nil)
// Act.
ctrl, err := New(fixture.log, &fixture.cfg, nil, fixture.errorRegistry, fixture.admission, fixture.auth, fixture.readiness)
// Assert.
if err == nil || ctrl != nil {
t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err)
}
})
t.Run("Negative: missing registry", func(t *testing.T) {
// Arrange.
fixture := testController(t, nil)
// Act.
ctrl, err := New(fixture.log, &fixture.cfg, fixture.service, nil, fixture.admission, fixture.auth, fixture.readiness)
// Assert.
if err == nil || ctrl != nil {
t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err)
}
})
t.Run("Negative: missing limiter", func(t *testing.T) {
// Arrange.
fixture := testController(t, nil)
// Act.
ctrl, err := New(fixture.log, &fixture.cfg, fixture.service, fixture.errorRegistry, nil, fixture.auth, fixture.readiness)
// Assert.
if err == nil || ctrl != nil {
t.Fatalf("incomplete controller accepted: %v, %v", ctrl, err)
}
})
}