diff --git a/server.go b/server.go index 04b3063..a5deb2e 100644 --- a/server.go +++ b/server.go @@ -60,11 +60,31 @@ func NewServer() Server { return s } -// Registers a handler to be called when the given GET path has been requested. +// Registers a handler for the given GET path. func (s *server) Get(path string, handler Handler) { s.Router().Add("GET", path, handler) } +// Registers a handler for the given POST path. +func (s *server) Post(path string, handler Handler) { + s.Router().Add("POST", path, handler) +} + +// Registers a handler for the given PUT path. +func (s *server) Put(path string, handler Handler) { + s.Router().Add("PUT", path, handler) +} + +// Registers a handler for the given DELETE path. +func (s *server) Delete(path string, handler Handler) { + s.Router().Add("DELETE", path, handler) +} + +// Registers a handler for the given PATCH path. +func (s *server) Patch(path string, handler Handler) { + s.Router().Add("PATCH", path, handler) +} + // Performs a synthetic request and returns the response. // This function keeps the response in memory so it's slightly slower than a real request. // However it is very useful inside tests where you don't want to spin up a real web server. diff --git a/tests/server_test.go b/tests/server_test.go index fac0a9d..ae70446 100644 --- a/tests/server_test.go +++ b/tests/server_test.go @@ -4,6 +4,7 @@ import ( "io" "net" "net/http" + "strings" "syscall" "testing" @@ -206,3 +207,21 @@ func TestUnavailablePort(t *testing.T) { s := web.NewServer() s.Run(port) } + +func TestBodyContent(t *testing.T) { + s := web.NewServer() + + s.Post("/", func(ctx web.Context) error { + body := ctx.Request().Body() + return ctx.String(string(body)) + }) + + response := s.Request("POST", "/", nil, io.NopCloser(strings.NewReader("Hello"))) + if response.Status() != 200 { + t.Errorf("Error: %s", response.Body()) + } + + if string(response.Body()) != "Hello" { + t.Errorf("Error: %s", response.Body()) + } +}