forked from mtusi2026/mtusi2026lab3
work 3
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestInstanceID_OnEveryResponse(t *testing.T) {
|
||||
r := newTestRig(t)
|
||||
cases := []struct {
|
||||
name, method, path string
|
||||
want int
|
||||
}{
|
||||
{"live", http.MethodGet, "/health/live", http.StatusOK},
|
||||
{"ready", http.MethodGet, "/health/ready", http.StatusOK},
|
||||
{"unauthorized", http.MethodGet, "/api/v1/users/me", http.StatusUnauthorized},
|
||||
{"unknown route", http.MethodGet, "/api/v1/nope", http.StatusNotFound},
|
||||
{"wrong method", http.MethodPatch, "/api/v1/users", http.StatusMethodNotAllowed},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
resp := r.do(tc.method, tc.path, "", nil, nil)
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != tc.want {
|
||||
t.Fatalf("status = %d, want %d", resp.StatusCode, tc.want)
|
||||
}
|
||||
if got := resp.Header.Get("X-Instance-Id"); got != testInstanceID {
|
||||
t.Errorf("X-Instance-Id = %q, want %q", got, testInstanceID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdempotencyKey_Validation(t *testing.T) {
|
||||
r := newTestRig(t)
|
||||
alice := r.createUser("alice_key", "Alice")
|
||||
chat := r.createChat(alice, "keys", alice.ID)
|
||||
url := r.server.URL + "/api/v1/chats/" + chat.ID + "/messages"
|
||||
|
||||
post := func(setKey func(http.Header)) int {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader([]byte(`{"text":"hi"}`)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.setAuth(req, alice.ID)
|
||||
setKey(req.Header)
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
withKey := func(key string) func(http.Header) {
|
||||
return func(h http.Header) { h.Set("Idempotency-Key", key) }
|
||||
}
|
||||
|
||||
if got := post(func(http.Header) {}); got != http.StatusBadRequest {
|
||||
t.Errorf("missing key: status = %d, want 400", got)
|
||||
}
|
||||
for name, key := range map[string]string{
|
||||
"empty": "",
|
||||
"too long": strings.Repeat("a", 129),
|
||||
"space": "a b",
|
||||
"unicode": "ключ",
|
||||
"slash": "a/b",
|
||||
} {
|
||||
if got := post(withKey(key)); got != http.StatusBadRequest {
|
||||
t.Errorf("%s: status = %d, want 400", name, got)
|
||||
}
|
||||
}
|
||||
for name, key := range map[string]string{
|
||||
"all allowed chars": "aZ09._:-",
|
||||
"max length": strings.Repeat("a", 128),
|
||||
"uuid": uuid.NewString(),
|
||||
} {
|
||||
if got := post(withKey(key)); got != http.StatusCreated {
|
||||
t.Errorf("%s: status = %d, want 201", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDrain_NotReadyButStillServing(t *testing.T) {
|
||||
r := newTestRig(t)
|
||||
status := func(path string) int {
|
||||
t.Helper()
|
||||
resp := r.do(http.MethodGet, path, "", nil, nil)
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
if got := status("/health/ready"); got != http.StatusOK {
|
||||
t.Fatalf("ready before drain = %d", got)
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
r.srv.Drain(500 * time.Millisecond)
|
||||
}()
|
||||
deadline := time.Now().Add(400 * time.Millisecond)
|
||||
for status("/health/ready") != http.StatusServiceUnavailable {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("readiness did not become 503 during drain")
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
if got := status("/health/live"); got != http.StatusOK {
|
||||
t.Errorf("live during drain = %d, want 200", got)
|
||||
}
|
||||
if got := status("/api/v1/users/me"); got != http.StatusUnauthorized {
|
||||
t.Errorf("API during drain = %d, want 401: запросы должны обслуживаться", got)
|
||||
}
|
||||
<-done
|
||||
}
|
||||
Reference in New Issue
Block a user