diff --git a/Request.go b/Request.go index 5ffa025..1b9c3d0 100644 --- a/Request.go +++ b/Request.go @@ -2,12 +2,14 @@ package web import ( "bufio" + "io" "git.urbach.dev/go/router" ) // Request is an interface for HTTP requests. type Request interface { + io.Reader Header(string) string Host() string Method() string @@ -18,14 +20,13 @@ type Request interface { // request represents the HTTP request used in the given context. type request struct { - reader *bufio.Reader + bufio.Reader scheme string host string method string path string query string headers []Header - body []byte params []router.Parameter } diff --git a/Request_test.go b/Request_test.go index 1d705d6..b21a8bd 100644 --- a/Request_test.go +++ b/Request_test.go @@ -2,6 +2,7 @@ package web_test import ( "fmt" + "strings" "testing" "git.urbach.dev/go/assert" @@ -25,6 +26,25 @@ func TestRequest(t *testing.T) { assert.Equal(t, string(response.Body()), "GET http example.com /request") } +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) + + if err != nil { + return err + } + + return ctx.Bytes(body[:n]) + }) + + response := s.Request("GET", "/", nil, strings.NewReader("Hello")) + assert.Equal(t, response.Status(), 200) + assert.Equal(t, string(response.Body()), "Hello") +} + func TestRequestHeader(t *testing.T) { s := web.NewServer() diff --git a/Server.go b/Server.go index bf2bd98..a851fc4 100644 --- a/Server.go +++ b/Server.go @@ -1,7 +1,6 @@ package web import ( - "bufio" "bytes" "io" "log" @@ -79,6 +78,7 @@ func (s *server) Ready() chan struct{} { func (s *server) Request(method string, url string, headers []Header, body io.Reader) Response { ctx := s.newContext() ctx.request.headers = headers + ctx.request.Reader.Reset(body) s.handleRequest(ctx, method, url, io.Discard) return ctx.Response() } @@ -133,14 +133,14 @@ func (s *server) handleConnection(conn net.Conn) { close bool ) - ctx.reader.Reset(conn) + ctx.Reader.Reset(conn) defer conn.Close() defer s.contextPool.Put(ctx) for !close { // Read the HTTP request line - message, err := ctx.reader.ReadString('\n') + message, err := ctx.Reader.ReadString('\n') if err != nil { return @@ -177,7 +177,7 @@ func (s *server) handleConnection(conn net.Conn) { // Add headers until we meet an empty line for { - message, err = ctx.reader.ReadString('\n') + message, err = ctx.Reader.ReadString('\n') if err != nil { return @@ -215,7 +215,6 @@ func (s *server) handleConnection(conn net.Conn) { // Clean up the context ctx.request.headers = ctx.request.headers[:0] - ctx.request.body = ctx.request.body[:0] ctx.response.headers = ctx.response.headers[:0] ctx.response.body = ctx.response.body[:0] ctx.params = ctx.params[:0] @@ -259,8 +258,6 @@ func (s *server) newContext() *context { return &context{ server: s, request: request{ - reader: bufio.NewReader(nil), - body: make([]byte, 0), headers: make([]Header, 0, 8), params: make([]router.Parameter, 0, 8), }, diff --git a/readme.md b/readme.md index b9bd107..66bfe4e 100644 --- a/readme.md +++ b/readme.md @@ -57,6 +57,7 @@ PASS: TestError PASS: TestErrorMultiple PASS: TestRedirect PASS: TestRequest +PASS: TestRequestBody PASS: TestRequestHeader PASS: TestRequestParam PASS: TestWrite