Added more HTTP methods

This commit is contained in:
Eduard Urbach 2023-07-22 11:36:28 +02:00
parent 16f214df95
commit 968687849f
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 40 additions and 9 deletions

View File

@ -25,7 +25,22 @@ func New() *Server {
// Get registers your function to be called when the given GET path has been requested.
func (server *Server) Get(path string, handler Handler) {
server.router.Add("GET", path, handler)
server.router.Add(http.MethodGet, path, handler)
}
// Post registers your function to be called when the given POST path has been requested.
func (server *Server) Post(path string, handler Handler) {
server.router.Add(http.MethodPost, path, handler)
}
// Delete registers your function to be called when the given DELETE path has been requested.
func (server *Server) Delete(path string, handler Handler) {
server.router.Add(http.MethodDelete, path, handler)
}
// Put registers your function to be called when the given PUT path has been requested.
func (server *Server) Put(path string, handler Handler) {
server.router.Add(http.MethodPut, path, handler)
}
// ServeHTTP responds to the given request.

View File

@ -39,23 +39,39 @@ func TestRouter(t *testing.T) {
return ctx.String("Hello")
})
s.Post("/", func(ctx server.Context) error {
return ctx.String("Post")
})
s.Delete("/", func(ctx server.Context) error {
return ctx.String("Delete")
})
s.Put("/", func(ctx server.Context) error {
return ctx.String("Put")
})
tests := []struct {
Method string
URL string
Status int
Body string
}{
{URL: "/", Status: http.StatusOK, Body: "Hello"},
{URL: "/blog/post", Status: http.StatusOK, Body: "Hello"},
{URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"},
{URL: "/error2", Status: http.StatusUnauthorized, Body: "Not logged in\nMissing auth token"},
{URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
{URL: "/reader", Status: http.StatusOK, Body: "Hello"},
{URL: "/string", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/blog/post", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/error", Status: http.StatusUnauthorized, Body: "Not logged in"},
{Method: "GET", URL: "/error2", Status: http.StatusUnauthorized, Body: "Not logged in\nMissing auth token"},
{Method: "GET", URL: "/not-found", Status: http.StatusNotFound, Body: http.StatusText(http.StatusNotFound)},
{Method: "GET", URL: "/reader", Status: http.StatusOK, Body: "Hello"},
{Method: "GET", URL: "/string", Status: http.StatusOK, Body: "Hello"},
{Method: "POST", URL: "/", Status: http.StatusOK, Body: "Post"},
{Method: "DELETE", URL: "/", Status: http.StatusOK, Body: "Delete"},
{Method: "PUT", URL: "/", Status: http.StatusOK, Body: "Put"},
}
for _, test := range tests {
t.Run("example.com"+test.URL, func(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, test.URL, nil)
request := httptest.NewRequest(test.Method, test.URL, nil)
response := httptest.NewRecorder()
s.ServeHTTP(response, request)