Files
mtusi2026lab3/idempotency_test.go
T

39 lines
1.1 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestIdempotencyKeySyntax(t *testing.T) {
for _, key := range []string{"a", "A._:-012", strings.Repeat("x", 128)} {
if !idempotencyPattern.MatchString(key) {
t.Fatalf("valid key rejected: %q", key)
}
}
for _, key := range []string{"", "space key", "ключ", strings.Repeat("a", 129), "a\nb"} {
if idempotencyPattern.MatchString(key) {
t.Fatalf("invalid key accepted: %q", key)
}
}
}
func TestDrainingReadinessAndBusiness(t *testing.T) {
a := &app{instanceID: "replica-under-test", limits: loadLimits()}
a.draining.Store(true)
for _, path := range []string{"/health/ready", "/api/v1/users/me"} {
w := httptest.NewRecorder()
a.routes().ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
if w.Code != 503 || w.Header().Get("X-Instance-Id") != a.instanceID {
t.Fatalf("draining request %s: %d", path, w.Code)
}
}
w := httptest.NewRecorder()
a.routes().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/health/live", nil))
if w.Code != 200 {
t.Fatal("liveness lost during drain")
}
}