Files
lincodex/backend/internal/adapter/primary/http/assessment_signal_body_test.go
T

53 lines
1.6 KiB
Go

package httpadapter
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestLimitAssessmentSignalBodyBoundsFixedAndChunkedBodies(t *testing.T) {
testCases := []struct {
name string
contentLength int64
}{
{name: "content length", contentLength: maxAssessmentSignalBodyBytes + 1},
{name: "chunked body", contentLength: -1},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
request := httptest.NewRequest("POST", "/", strings.NewReader(strings.Repeat("x", int(maxAssessmentSignalBodyBytes+1))))
request.ContentLength = testCase.contentLength
response := httptest.NewRecorder()
limitAssessmentSignalBody(response, request)
body, err := io.ReadAll(request.Body)
var limitError *http.MaxBytesError
if !errors.As(err, &limitError) {
t.Fatalf("ReadAll() error = %v, want MaxBytesError", err)
}
if int64(len(body)) != maxAssessmentSignalBodyBytes {
t.Fatalf("ReadAll() returned %d bytes, want %d", len(body), maxAssessmentSignalBodyBytes)
}
})
}
}
func TestLimitAssessmentSignalBodyAllowsExactLimit(t *testing.T) {
request := httptest.NewRequest("POST", "/", strings.NewReader(strings.Repeat("x", int(maxAssessmentSignalBodyBytes))))
response := httptest.NewRecorder()
limitAssessmentSignalBody(response, request)
body, err := io.ReadAll(request.Body)
if err != nil {
t.Fatalf("ReadAll() error = %v", err)
}
if int64(len(body)) != maxAssessmentSignalBodyBytes {
t.Fatalf("ReadAll() returned %d bytes, want %d", len(body), maxAssessmentSignalBodyBytes)
}
}