From e1788b2874ab7eab9026c6856b5bbd603aa1b092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Hurtado?= Date: Tue, 7 Apr 2020 12:22:15 +0200 Subject: [PATCH] Default handlers for NotFound and MethodNotAllowed returning json reason added to control y data servers --- internal/server/control/control.go | 12 ++++++++++++ internal/server/control/control_test.go | 16 ++++++++++++---- internal/server/data/server.go | 9 +++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/internal/server/control/control.go b/internal/server/control/control.go index b775a30..693b14e 100644 --- a/internal/server/control/control.go +++ b/internal/server/control/control.go @@ -33,6 +33,7 @@ import ( // server exposes list, get, delete and add route endpoints. func configRouter() *mux.Router { r := mux.NewRouter() + r.HandleFunc("/routes/{id}", removeRoute). Methods(http.MethodDelete) r.HandleFunc("/routes/{id}", getRoute). @@ -41,9 +42,20 @@ func configRouter() *mux.Router { Methods(http.MethodGet) r.HandleFunc("/routes", addRoute). Methods(http.MethodPost) + r.NotFoundHandler = http.HandlerFunc(defNotFoundHandler) + r.MethodNotAllowedHandler = http.HandlerFunc(defMethodNotAllowedHandler) + return r } +func defNotFoundHandler(w http.ResponseWriter, h *http.Request) { + httperror.ErrorJSON(w, "Data server: Not found", http.StatusNotFound) +} + +func defMethodNotAllowedHandler(w http.ResponseWriter, h *http.Request) { + httperror.ErrorJSON(w, "Data server: Method not allowed", http.StatusMethodNotAllowed) +} + // funcRemove Method used to ask the route model module to delete a route var funcRemove func(id string) error = user.Routes.Delete diff --git a/internal/server/control/control_test.go b/internal/server/control/control_test.go index d1bf571..52747e1 100644 --- a/internal/server/control/control_test.go +++ b/internal/server/control/control_test.go @@ -66,13 +66,21 @@ func TestConfigRouterHasRoutesWellConfigured(t *testing.T) { vars []string }{ {"/routes/FOO", http.MethodGet, reflect.ValueOf(getRoute).Pointer(), true, []string{"id"}}, - {"/routes/FOO", http.MethodPut, 0, false, []string{}}, - {"/routes/FOO", http.MethodPost, 0, false, []string{}}, + {"/routes/FOO", http.MethodPut, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}}, + {"/routes/FOO", http.MethodPost, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}}, {"/routes/FOO", http.MethodDelete, reflect.ValueOf(removeRoute).Pointer(), true, []string{"id"}}, {"/routes", http.MethodGet, reflect.ValueOf(listRoutes).Pointer(), true, []string{}}, - {"/routes", http.MethodPut, 0, false, []string{}}, + {"/routes", http.MethodPut, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}}, {"/routes", http.MethodPost, reflect.ValueOf(addRoute).Pointer(), true, []string{}}, - {"/routes", http.MethodDelete, 0, false, []string{}}, + {"/routes", http.MethodDelete, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}}, + {"/", http.MethodGet, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/", http.MethodPut, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/", http.MethodPost, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/", http.MethodDelete, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/FOO", http.MethodGet, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/FOO", http.MethodPut, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/FOO", http.MethodPost, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, + {"/FOO", http.MethodDelete, reflect.ValueOf(defNotFoundHandler).Pointer(), true, []string{}}, } r := configRouter() diff --git a/internal/server/data/server.go b/internal/server/data/server.go index 84913b0..2cba06a 100644 --- a/internal/server/data/server.go +++ b/internal/server/data/server.go @@ -42,6 +42,15 @@ func configRouter(rs []routeSpec) (r *mux.Router) { func(w http.ResponseWriter, r *http.Request) { httperror.ErrorJSON(w, "Invalid Resource Path", http.StatusBadRequest) }) + + r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, h *http.Request) { + httperror.ErrorJSON(w, "Data server: Not found", http.StatusNotFound) + }) + + r.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, h *http.Request) { + httperror.ErrorJSON(w, "Data server: Method not allowed", http.StatusMethodNotAllowed) + }) + return r }