Added gock.IsDone() to route list and remove tests. Remove content type in route remove command. Removed disturbing data for texts. Remove unneeded test in set command.

This commit is contained in:
Héctor Hurtado
2019-10-04 13:55:36 +02:00
parent b93a5a0c98
commit 6b34047d75
6 changed files with 31 additions and 80 deletions
+1
View File
@@ -8,6 +8,7 @@ import (
// ListRoutes queries the kapow! instance for the routes that are registered // ListRoutes queries the kapow! instance for the routes that are registered
func ListRoutes(host string, w io.Writer) error { func ListRoutes(host string, w io.Writer) error {
url := host + "/routes/" url := host + "/routes/"
return http.Get(url, "", nil, w) return http.Get(url, "", nil, w)
} }
+8
View File
@@ -25,6 +25,10 @@ func TestListRoutesEmpty(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("%s: unexpected error %q", descr, err) t.Errorf("%s: unexpected error %q", descr, err)
} }
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
} }
func TestListRoutesSome(t *testing.T) { func TestListRoutesSome(t *testing.T) {
@@ -45,4 +49,8 @@ func TestListRoutesSome(t *testing.T) {
} else if got := buf.String(); got != want { } else if got := buf.String(); got != want {
t.Errorf("%s: got %q, expected %q", descr, buf, want) t.Errorf("%s: got %q, expected %q", descr, buf, want)
} }
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
} }
+3 -1
View File
@@ -4,7 +4,9 @@ import (
"github.com/BBVA/kapow/internal/http" "github.com/BBVA/kapow/internal/http"
) )
// RemoveRoute removes a registered route in Kapow! server
func RemoveRoute(host, id string) error { func RemoveRoute(host, id string) error {
url := host + "/routes/" + id url := host + "/routes/" + id
return http.Delete(url, "application/json", nil, nil) return http.Delete(url, "", nil, nil)
} }
+9 -1
View File
@@ -14,12 +14,16 @@ func TestRemoveRouteExistent(t *testing.T) {
) )
defer gock.Off() defer gock.Off()
gock.New(host).Delete("/routes/" + routeID).MatchType("json").Reply(http.StatusNoContent) gock.New(host).Delete("/routes/" + routeID).Reply(http.StatusNoContent)
err := RemoveRoute(host, routeID) err := RemoveRoute(host, routeID)
if err != nil { if err != nil {
t.Errorf("unexpected error: %s", err) t.Errorf("unexpected error: %s", err)
} }
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
} }
func TestRemoveRouteNonExistent(t *testing.T) { func TestRemoveRouteNonExistent(t *testing.T) {
@@ -38,4 +42,8 @@ func TestRemoveRouteNonExistent(t *testing.T) {
} else if err.Error() != expected { } else if err.Error() != expected {
t.Errorf("error mismatch: expected %s, got %s", expected, err) t.Errorf("error mismatch: expected %s, got %s", expected, err)
} }
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
} }
+4 -3
View File
@@ -6,7 +6,8 @@ import (
"github.com/BBVA/kapow/internal/http" "github.com/BBVA/kapow/internal/http"
) )
// TODO: Review spec: Data API > Error responses > 204 Resource Item Not Found should not be 2xx func SetData(host, handlerID, path string, r io.Reader) error {
func SetData(kapowURL, handlerId, path string, r io.Reader) error {
return http.Put(kapowURL+"/"+handlerId+path, "", r, nil) url := host + "/handlers/" + handlerID + path
return http.Put(url, "", r, nil)
} }
+6 -75
View File
@@ -5,64 +5,18 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/BBVA/kapow/internal/client"
gock "gopkg.in/h2non/gock.v1" gock "gopkg.in/h2non/gock.v1"
"github.com/BBVA/kapow/internal/client"
) )
// Test that no content errors are detected as non-existent resource
func TestNoContent(t *testing.T) {
expectedErr := "Resource Item Not Found"
host := "http://localhost:8080"
hid := "xxxxxxxxxxxxxx"
path := "/unpath"
reader := strings.NewReader("Esto es un peacho de dato pa repartir")
defer gock.Off()
gock.New(host).Put("/" + hid + path).Reply(http.StatusNoContent)
if err := client.SetData(host, hid, path, reader); err == nil {
t.Error("Expected error not present")
} else if err.Error() != expectedErr {
t.Errorf("Error don't match: expected \"%s\", got \"%s\"", expectedErr, err.Error())
}
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
}
// Test that bad request errors are detected as invalid resource
func TestBadRequest(t *testing.T) {
expectedErr := "Invalid Resource Path"
host := "http://localhost:8080"
hid := "xxxxxxxxxxxxxx"
path := "/unpath"
reader := strings.NewReader("Esto es un peacho de dato pa repartir")
defer gock.Off()
gock.New(host).Put("/" + hid + path).Reply(http.StatusBadRequest)
if err := client.SetData(host, hid, path, reader); err == nil {
t.Error("Expected error not present")
} else if err.Error() != expectedErr {
t.Errorf("Error don't match: expected \"%s\", got \"%s\"", expectedErr, err.Error())
}
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
}
// Test that not found errors are detected as invalid handler id // Test that not found errors are detected as invalid handler id
func TestNotFound(t *testing.T) { func TestNotFound(t *testing.T) {
expectedErr := "Not Found" expectedErr := "Not Found"
host := "http://localhost:8080" host := "http://localhost:8080"
hid := "xxxxxxxxxxxxxx" hid := "inventedID"
path := "/unpath" path := "/response/status/code"
reader := strings.NewReader("Esto es un peacho de dato pa repartir") reader := strings.NewReader("200")
defer gock.Off() defer gock.Off()
@@ -79,33 +33,10 @@ func TestNotFound(t *testing.T) {
} }
} }
// Test that internal server errors are detected correctly
func TestInternalServerError(t *testing.T) {
expectedErr := "Internal Server Error"
host := "http://localhost:8080"
hid := "xxxxxxxxxxxxxx"
path := "/unpath"
reader := strings.NewReader("Esto es un peacho de dato pa repartir")
defer gock.Off()
gock.New(host).Put("/" + hid + path).Reply(http.StatusInternalServerError)
if err := client.SetData(host, hid, path, reader); err == nil {
t.Error("Expected error not present")
} else if err.Error() != expectedErr {
t.Errorf("Error don't match: expected \"%s\", got \"%s\"", expectedErr, err.Error())
}
if !gock.IsDone() {
t.Errorf("No endpoint called")
}
}
// Test a http ok request // Test a http ok request
func TestOkRequest(t *testing.T) { func TestOkRequest(t *testing.T) {
host := "http://localhost:8080" host := "http://localhost:8080"
hid := "xxxxxxxxxxxxxx" hid := "HANDLER_XXXXXXXXXXXX"
path := "/response/status/code" path := "/response/status/code"
reader := strings.NewReader("200") reader := strings.NewReader("200")