Files
mtusi2026lab3/backend/internal/adapter/handler/utf8_test.go

22 lines
707 B
Go

package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestRejectsInvalidUTF8BeforeJSONDecoding(t *testing.T) {
body := append([]byte(`{"display_name":"`), 0xff)
body = append(body, []byte(`"}`)...)
request := httptest.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewReader(body))
response := httptest.NewRecorder()
h := &API{limits: Limits{MaxBodyBytes: 65536}}
called := false
requestID(h.jsonBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true; w.WriteHeader(204) }))).ServeHTTP(response, request)
if response.Code != 400 || called {
t.Fatalf("invalid UTF-8 reached decoder: status=%d called=%t", response.Code, called)
}
}