Validate url_pattern format in control API append operation

Closes: #38

Co-authored-by: César Gallego Rodríguez <cesar.gallego.next@bbva.com>
This commit is contained in:
pancho horrillo
2019-10-24 14:10:58 +02:00
parent 84a71815e4
commit b0c16ca684
2 changed files with 57 additions and 0 deletions
+47
View File
@@ -72,6 +72,22 @@ func TestConfigRouterHasRoutesWellConfigured(t *testing.T) {
}
}
func TestPathValidatorNoErrorWhenCorrectPath(t *testing.T) {
err := pathValidator("/routes/{routeID}")
if err != nil {
t.Error(err)
}
}
func TestPathValidatorErrorWhenInvalidPath(t *testing.T) {
err := pathValidator("/routes/{routeID{")
if err == nil {
t.FailNow()
}
}
func TestAddRouteReturnsBadRequestWhenMalformedJSONBody(t *testing.T) {
reqPayload := `{
method": "GET",
@@ -181,6 +197,9 @@ func TestAddRouteGeneratesRouteID(t *testing.T) {
input.Index = 0
return input
}
origPathValidator := pathValidator
defer func() { pathValidator = origPathValidator }()
pathValidator = func(path string) error { return nil }
handler.ServeHTTP(resp, req)
@@ -200,6 +219,10 @@ func TestAddRoute500sWhenIDGeneratorFails(t *testing.T) {
resp := httptest.NewRecorder()
handler := http.HandlerFunc(addRoute)
origPathValidator := pathValidator
defer func() { pathValidator = origPathValidator }()
pathValidator = func(path string) error { return nil }
idGenOrig := idGenerator
defer func() { idGenerator = idGenOrig }()
idGenerator = func() (uuid.UUID, error) {
@@ -237,6 +260,9 @@ func TestAddRouteReturnsCreated(t *testing.T) {
return model.Route{}
}
origPathValidator := pathValidator
defer func() { pathValidator = origPathValidator }()
pathValidator = func(path string) error { return nil }
handler.ServeHTTP(resp, req)
@@ -259,6 +285,27 @@ func TestAddRouteReturnsCreated(t *testing.T) {
}
}
func TestAddRoute422sWhenInvalidRoute(t *testing.T) {
reqPayload := `{
"method": "GET",
"url_pattern": "/he{{o",
"entrypoint": "/bin/sh -c",
"command": "echo Hello World | kapow set /response/body"
}`
req := httptest.NewRequest(http.MethodPost, "/routes", strings.NewReader(reqPayload))
resp := httptest.NewRecorder()
handler := http.HandlerFunc(addRoute)
origPathValidator := pathValidator
defer func() { pathValidator = origPathValidator }()
pathValidator = func(path string) error { return errors.New("Invalid route") }
handler.ServeHTTP(resp, req)
if resp.Code != http.StatusUnprocessableEntity {
t.Error("Invalid route registered")
}
}
func TestRemoveRouteReturnsNotFound(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/routes/ROUTE_XXXXXXXXXXXXXXXXXX", nil)
resp := httptest.NewRecorder()