Added support for io.ReadAll on requests
All checks were successful
/ test (push) Successful in 18s

This commit is contained in:
Eduard Urbach 2025-06-21 11:37:39 +02:00
parent 9a781d2e64
commit 4c19cd0e99
Signed by: eduard
GPG key ID: 49226B848C78F6C8
4 changed files with 65 additions and 18 deletions

View file

@ -2,6 +2,8 @@ package web_test
import (
"fmt"
"io"
"strconv"
"strings"
"testing"
@ -30,19 +32,39 @@ func TestRequestBody(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
body := make([]byte, 4096)
n, err := ctx.Request().Read(body)
body, err := io.ReadAll(ctx.Request())
if err != nil {
return err
}
return ctx.Bytes(body[:n])
return ctx.Bytes(body)
})
response := s.Request("GET", "/", nil, strings.NewReader("Hello"))
body := strings.Repeat("Hello", 1000)
headers := []web.Header{{Key: "Content-Length", Value: strconv.Itoa(len(body))}}
response := s.Request("GET", "/", headers, strings.NewReader(body))
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
assert.Equal(t, string(response.Body()), body)
}
func TestRequestBodyMissingLength(t *testing.T) {
s := web.NewServer()
s.Get("/", func(ctx web.Context) error {
body, err := io.ReadAll(ctx.Request())
if err != nil {
return err
}
return ctx.Bytes(body)
})
body := strings.Repeat("Hello", 1000)
response := s.Request("GET", "/", nil, strings.NewReader(body))
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "")
}
func TestRequestHeader(t *testing.T) {