forked from mtusi2026/mtusi2026lab2
Перенести Go API и контейнер из лабораторной 1
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateUserInvalidJSON(t *testing.T) {
|
||||
store := newUserStore()
|
||||
handler := createUserHandler(store)
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{"username":`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("Content-Type") != "application/json" {
|
||||
t.Fatalf(
|
||||
"expected Content-Type application/json, got %q",
|
||||
response.Header().Get("Content-Type"),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
body := response.Body.String()
|
||||
|
||||
if !strings.Contains(body, `"code":"invalid_request"`) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
|
||||
if !strings.Contains(body, "request_id") {
|
||||
t.Fatalf(
|
||||
"expected request_id in error body, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateUserDuplicateUsername(t *testing.T) {
|
||||
store := newUserStore()
|
||||
handler := createUserHandler(store)
|
||||
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Алиса"
|
||||
}`),
|
||||
)
|
||||
firstRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusCreated {
|
||||
t.Fatalf(
|
||||
"expected first request status %d, got %d; body: %s",
|
||||
http.StatusCreated,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Другая Алиса"
|
||||
}`),
|
||||
)
|
||||
secondRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusConflict {
|
||||
t.Fatalf(
|
||||
"expected duplicate username status %d, got %d; body: %s",
|
||||
http.StatusConflict,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if secondResponse.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
secondResponse.Body.String(),
|
||||
`"code":"conflict"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected conflict error code, got body: %s",
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if len(store.byID) != 1 {
|
||||
t.Fatalf(
|
||||
"expected exactly one user in store, got %d",
|
||||
len(store.byID),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user