From c744c93215f656f9fdc169351572523137f96c89 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 22 Jul 2023 11:36:28 +0200 Subject: [PATCH] Added more HTTP methods --- Server.go | 17 ++++++++++++++++- Server_test.go | 32 ++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Server.go b/Server.go index 13544a7..90ce281 100644 --- a/Server.go +++ b/Server.go @@ -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. diff --git a/Server_test.go b/Server_test.go index e274bfd..185f5b6 100644 --- a/Server_test.go +++ b/Server_test.go @@ -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)