diff --git a/bin/evening_detective_server b/bin/evening_detective_server index c08cbe1..0cf2b46 100755 Binary files a/bin/evening_detective_server and b/bin/evening_detective_server differ diff --git a/internal/app/team_registration.go b/internal/app/team_registration.go index 83446ec..f3f11bb 100644 --- a/internal/app/team_registration.go +++ b/internal/app/team_registration.go @@ -5,6 +5,7 @@ import ( "crypto/subtle" "encoding/json" "errors" + "fmt" "io" "net/http" "strconv" @@ -41,11 +42,16 @@ func TeamRegistrationHandler(token string, register func(context.Context, int, s decoder.DisallowUnknownFields() var registration game_service.Registration if err := decoder.Decode(®istration); err != nil { - fail(http.StatusBadRequest, "Некорректный JSON регистрации") + fail(http.StatusBadRequest, fmt.Sprintf("Некорректный JSON регистрации: %v", err)) return } - if err := decoder.Decode(new(any)); err != io.EOF { - fail(http.StatusBadRequest, "Ожидается один JSON-объект") + var extra any + if err := decoder.Decode(&extra); err != io.EOF { + if err == nil { + fail(http.StatusBadRequest, "Некорректный JSON регистрации: после объекта обнаружены лишние данные") + } else { + fail(http.StatusBadRequest, fmt.Sprintf("Некорректный JSON регистрации: после объекта обнаружены лишние данные: %v", err)) + } return } ctx, cancel := context.WithTimeout(r.Context(), 4*time.Second) diff --git a/internal/app/team_registration_test.go b/internal/app/team_registration_test.go index e8133d9..7d8ba4a 100644 --- a/internal/app/team_registration_test.go +++ b/internal/app/team_registration_test.go @@ -61,6 +61,15 @@ func TestTeamRegistrationHandler(t *testing.T) { if strings.Contains(res.Body.String(), "private database detail") { t.Fatal("private error exposed") } + if tc.name == "malformed" && !strings.Contains(res.Body.String(), "unexpected EOF") { + t.Fatalf("malformed JSON reason missing: %s", res.Body.String()) + } + if tc.name == "unknown field" && !strings.Contains(res.Body.String(), "unknown field") { + t.Fatalf("unknown field reason missing: %s", res.Body.String()) + } + if tc.name == "extra object" && !strings.Contains(res.Body.String(), "лишние данные") { + t.Fatalf("extra data reason missing: %s", res.Body.String()) + } }) } }