Default handlers for NotFound and MethodNotAllowed returning json reason added to control y data servers

This commit is contained in:
Héctor Hurtado
2020-04-07 12:22:15 +02:00
parent 4988c48e03
commit e1788b2874
3 changed files with 33 additions and 4 deletions
+12
View File
@@ -33,6 +33,7 @@ import (
// server exposes list, get, delete and add route endpoints. // server exposes list, get, delete and add route endpoints.
func configRouter() *mux.Router { func configRouter() *mux.Router {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/routes/{id}", removeRoute). r.HandleFunc("/routes/{id}", removeRoute).
Methods(http.MethodDelete) Methods(http.MethodDelete)
r.HandleFunc("/routes/{id}", getRoute). r.HandleFunc("/routes/{id}", getRoute).
@@ -41,9 +42,20 @@ func configRouter() *mux.Router {
Methods(http.MethodGet) Methods(http.MethodGet)
r.HandleFunc("/routes", addRoute). r.HandleFunc("/routes", addRoute).
Methods(http.MethodPost) Methods(http.MethodPost)
r.NotFoundHandler = http.HandlerFunc(defNotFoundHandler)
r.MethodNotAllowedHandler = http.HandlerFunc(defMethodNotAllowedHandler)
return r 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 // funcRemove Method used to ask the route model module to delete a route
var funcRemove func(id string) error = user.Routes.Delete var funcRemove func(id string) error = user.Routes.Delete
+12 -4
View File
@@ -66,13 +66,21 @@ func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
vars []string vars []string
}{ }{
{"/routes/FOO", http.MethodGet, reflect.ValueOf(getRoute).Pointer(), true, []string{"id"}}, {"/routes/FOO", http.MethodGet, reflect.ValueOf(getRoute).Pointer(), true, []string{"id"}},
{"/routes/FOO", http.MethodPut, 0, false, []string{}}, {"/routes/FOO", http.MethodPut, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}},
{"/routes/FOO", http.MethodPost, 0, false, []string{}}, {"/routes/FOO", http.MethodPost, reflect.ValueOf(defMethodNotAllowedHandler).Pointer(), true, []string{}},
{"/routes/FOO", http.MethodDelete, reflect.ValueOf(removeRoute).Pointer(), true, []string{"id"}}, {"/routes/FOO", http.MethodDelete, reflect.ValueOf(removeRoute).Pointer(), true, []string{"id"}},
{"/routes", http.MethodGet, reflect.ValueOf(listRoutes).Pointer(), true, []string{}}, {"/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.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() r := configRouter()
+9
View File
@@ -42,6 +42,15 @@ func configRouter(rs []routeSpec) (r *mux.Router) {
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
httperror.ErrorJSON(w, "Invalid Resource Path", http.StatusBadRequest) 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 return r
} }