New error handling added to server and decorator module in data package
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func checkHandler(fn resourceHandler) func(http.ResponseWriter, *http.Request) {
|
||||
if h, ok := Handlers.Get(handlerID); ok {
|
||||
fn(w, r, h)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, "Handler ID Not Found", w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,9 +127,8 @@ func TestCheckHandlerReturnsAFunctionsThat404sWhenHandlerDoesNotExist(t *testing
|
||||
|
||||
fn(w, r)
|
||||
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("Status code mismatch. Expected 404. Got %d", res.StatusCode)
|
||||
for _, e := range checkErrorResponse(w.Result(), http.StatusNotFound, "Handler ID Not Found") {
|
||||
t.Error(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,9 @@ func configRouter(rs []routeSpec) (r *mux.Router) {
|
||||
}
|
||||
r.HandleFunc(
|
||||
"/handlers/{handlerID}/{resource:.*}",
|
||||
func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) })
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
srverrors.WriteErrorResponse(http.StatusBadRequest, "Invalid Resource Path", w)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -17,13 +17,40 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
)
|
||||
|
||||
func checkErrorResponse(r *http.Response, expectedErrcode int, expectedReason string) []error {
|
||||
errList := make([]error, 0)
|
||||
|
||||
if r.StatusCode != expectedErrcode {
|
||||
errList = append(errList, fmt.Errorf("HTTP status mismatch. Expected: %d, got: %d", expectedErrcode, r.StatusCode))
|
||||
}
|
||||
|
||||
if v := r.Header.Get("Content-Type"); v != "application/json; charset=utf-8" {
|
||||
errList = append(errList, fmt.Errorf("Content-Type header mismatch. Expected: %q, got: %q", "application/json; charset=utf-8", v))
|
||||
}
|
||||
|
||||
errMsg := srverrors.ServerErrMessage{}
|
||||
if bodyBytes, err := ioutil.ReadAll(r.Body); err != nil {
|
||||
errList = append(errList, fmt.Errorf("Unexpected error reading response body: %v", err))
|
||||
} else if err := json.Unmarshal(bodyBytes, &errMsg); err != nil {
|
||||
errList = append(errList, fmt.Errorf("Response body contains invalid JSON entity: %v", err))
|
||||
} else if errMsg.Reason != expectedReason {
|
||||
errList = append(errList, fmt.Errorf("Unexpected reason in response. Expected: %q, got: %q", expectedReason, errMsg.Reason))
|
||||
}
|
||||
|
||||
return errList
|
||||
}
|
||||
|
||||
func TestConfigRouterReturnsRouterWithDecoratedRoutes(t *testing.T) {
|
||||
var handlerID string
|
||||
rs := []routeSpec{
|
||||
@@ -50,8 +77,7 @@ func TestConfigRouterReturnsRouterThat400sOnUnconfiguredResources(t *testing.T)
|
||||
|
||||
m.ServeHTTP(w, httptest.NewRequest("GET", "/handlers/FOO/dummy", nil))
|
||||
|
||||
res := w.Result()
|
||||
if res.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("Status code mismatch. Expected 400. Got %d", res.StatusCode)
|
||||
for _, e := range checkErrorResponse(w.Result(), http.StatusBadRequest, "Invalid Resource Path") {
|
||||
t.Error(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user