Files
mtusi2026lab6/app/internal/api/http/idempotency_test.go
T

120 lines
3.9 KiB
Go

package http
import (
"bytes"
"io"
"net/http"
"sync"
"testing"
"github.com/google/uuid"
)
func (r *testRig) postKeyed(actorID, chatID, key, body string) (int, []byte) {
r.t.Helper()
resp := r.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", actorID,
map[string]string{"Content-Type": "application/json", "Idempotency-Key": key},
bytes.NewReader([]byte(body)))
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
r.t.Fatal(err)
}
return resp.StatusCode, data
}
func TestIdempotency_ReplayReturnsSameBody(t *testing.T) {
r := newTestRig(t)
alice := r.createUser("alice_"+randomSuffix(), "Alice")
chat := r.createChat(alice, "replay", alice.ID)
key := uuid.NewString()
code, first := r.postKeyed(alice.ID, chat.ID, key, `{"text":"привет"}`)
if code != http.StatusCreated {
t.Fatalf("first: status = %d, body = %s", code, first)
}
// Иное форматирование JSON — тот же запрос.
code, replay := r.postKeyed(alice.ID, chat.ID, key, "{ \"text\" : \"привет\" }")
if code != http.StatusCreated {
t.Fatalf("replay: status = %d, body = %s", code, replay)
}
if !bytes.Equal(first, replay) {
t.Errorf("replay differs:\n%s\n%s", first, replay)
}
if items, _ := r.listMessages(alice, chat.ID, 100); len(items) != 1 {
t.Errorf("messages = %d, want 1", len(items))
}
}
func TestIdempotency_DifferentBodyConflicts(t *testing.T) {
r := newTestRig(t)
alice := r.createUser("alice_"+randomSuffix(), "Alice")
chat := r.createChat(alice, "conflict", alice.ID)
key := uuid.NewString()
if code, body := r.postKeyed(alice.ID, chat.ID, key, `{"text":"раз"}`); code != http.StatusCreated {
t.Fatalf("first: status = %d, body = %s", code, body)
}
if code, body := r.postKeyed(alice.ID, chat.ID, key, `{"text":"два"}`); code != http.StatusConflict {
t.Fatalf("conflict: status = %d, body = %s", code, body)
}
if items, _ := r.listMessages(alice, chat.ID, 100); len(items) != 1 {
t.Errorf("messages = %d, want 1", len(items))
}
}
func TestIdempotency_ScopeAndAccess(t *testing.T) {
r := newTestRig(t)
alice := r.createUser("alice_"+randomSuffix(), "Alice")
bob := r.createUser("bob_"+randomSuffix(), "Bob")
eve := r.createUser("eve_"+randomSuffix(), "Eve")
chat := r.createChat(alice, "shared", alice.ID, bob.ID)
other := r.createChat(alice, "other", alice.ID)
key := uuid.NewString()
if code, body := r.postKeyed(alice.ID, chat.ID, key, `{"text":"от alice"}`); code != http.StatusCreated {
t.Fatalf("alice: status = %d, body = %s", code, body)
}
if code, body := r.postKeyed(bob.ID, chat.ID, key, `{"text":"от bob"}`); code != http.StatusCreated {
t.Fatalf("bob: status = %d, body = %s", code, body)
}
if code, body := r.postKeyed(alice.ID, other.ID, key, `{"text":"другой чат"}`); code != http.StatusCreated {
t.Fatalf("other chat: status = %d, body = %s", code, body)
}
if code, body := r.postKeyed(eve.ID, chat.ID, key, `{"text":"от alice"}`); code != http.StatusNotFound {
t.Fatalf("stranger: status = %d, body = %s", code, body)
}
}
func TestIdempotency_ConcurrentCreatesOne(t *testing.T) {
r := newTestRig(t)
alice := r.createUser("alice_"+randomSuffix(), "Alice")
chat := r.createChat(alice, "race", alice.ID)
key := uuid.NewString()
const workers = 20
bodies := make([][]byte, workers)
codes := make([]int, workers)
var wg sync.WaitGroup
for i := range workers {
wg.Add(1)
go func() {
defer wg.Done()
codes[i], bodies[i] = r.postKeyed(alice.ID, chat.ID, key, `{"text":"гонка"}`)
}()
}
wg.Wait()
for i := range workers {
if codes[i] != http.StatusCreated {
t.Fatalf("worker %d: status = %d, body = %s", i, codes[i], bodies[i])
}
if !bytes.Equal(bodies[i], bodies[0]) {
t.Errorf("worker %d: body differs", i)
}
}
if items, _ := r.listMessages(alice, chat.ID, 100); len(items) != 1 {
t.Errorf("messages = %d, want 1", len(items))
}
}