Add HTTP method funcs to server
This commit is contained in:
parent
da47e3f63d
commit
85f04865b5
22
server.go
22
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.
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user