package httpapi_test import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/httpapi" "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model" "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service" "gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store" ) func newAPI() http.Handler { return httpapi.NewHandler( service.NewMessenger(store.NewMemoryStore()), ) } func request( handler http.Handler, method string, path string, body string, userID string, ) *httptest.ResponseRecorder { req := httptest.NewRequest(method, path, strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") if userID != "" { req.Header.Set("X-User-Id", userID) } response := httptest.NewRecorder() handler.ServeHTTP(response, req) return response } func checkError( t *testing.T, response *httptest.ResponseRecorder, status int, code string, ) { t.Helper() if response.Code != status { t.Fatalf( "status: got %d, want %d; body=%s", response.Code, status, response.Body.String(), ) } var body struct { Error struct { Code string `json:"code"` RequestID string `json:"request_id"` } `json:"error"` } if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil { t.Fatalf("decode error response: %v", err) } if body.Error.Code != code { t.Errorf("error code: got %q, want %q", body.Error.Code, code) } id := response.Header().Get("X-Request-Id") if id == "" || body.Error.RequestID != id { t.Error("missing or mismatched request ID") } } func TestUserHTTPFlow(t *testing.T) { handler := newAPI() body := `{"username":"alice","display_name":"Алиса 👋"}` created := request(handler, http.MethodPost, "/api/v1/users", body, "") if created.Code != http.StatusCreated { t.Fatalf("registration: %d %s", created.Code, created.Body.String()) } if created.Header().Get("X-Request-Id") == "" { t.Fatal("registration response has no request ID") } var user model.User if err := json.Unmarshal(created.Body.Bytes(), &user); err != nil { t.Fatal(err) } if user.ID == "" || user.CreatedAt.IsZero() || user.Username != "alice" || user.DisplayName != "Алиса 👋" { t.Fatalf("invalid user: %+v", user) } me := request(handler, http.MethodGet, "/api/v1/users/me", "", user.ID) if me.Code != http.StatusOK { t.Fatalf("current user: %d %s", me.Code, me.Body.String()) } var current model.User if err := json.Unmarshal(me.Body.Bytes(), ¤t); err != nil { t.Fatal(err) } if current != user { t.Fatalf("current user differs: %+v", current) } duplicate := request(handler, http.MethodPost, "/api/v1/users", body, "") checkError(t, duplicate, http.StatusConflict, "conflict") for _, id := range []string{ "", "invalid", "00000000-0000-4000-8000-000000000000", } { response := request( handler, http.MethodGet, "/api/v1/users/me", "", id, ) checkError(t, response, http.StatusUnauthorized, "unauthorized") } } func TestRegistrationInvalidJSON(t *testing.T) { cases := []string{ ``, `{`, `null`, `[]`, `{}`, `{"username":"alice"}`, `{"username":null,"display_name":"Alice"}`, `{"username":123,"display_name":"Alice"}`, `{"username":"alice","display_name":"Alice","extra":true}`, `{"username":"alice","display_name":"Alice"} {}`, `{"username":"Alice","display_name":"Alice"}`, `{"username":"alice","display_name":" "}`, } for _, body := range cases { t.Run(body, func(t *testing.T) { response := request( newAPI(), http.MethodPost, "/api/v1/users", body, "", ) checkError(t, response, http.StatusBadRequest, "invalid_request") }) } } func TestRegistrationContentType(t *testing.T) { req := httptest.NewRequest( http.MethodPost, "/api/v1/users", strings.NewReader(`{"username":"alice","display_name":"Alice"}`), ) req.Header.Set("Content-Type", "text/plain") response := httptest.NewRecorder() newAPI().ServeHTTP(response, req) checkError( t, response, http.StatusUnsupportedMediaType, "unsupported_media_type", ) }