Default handlers for NotFound and MethodNotAllowed returning json reason added to control y data servers
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user