From 332d897927e877025c43af219a0462ec70a2bebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Hurtado?= Date: Thu, 3 Oct 2019 10:37:29 +0200 Subject: [PATCH] Added set command fuction to client package --- client/set.go | 36 ++++++++++++++++ client/set_test.go | 103 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 client/set.go create mode 100644 client/set_test.go diff --git a/client/set.go b/client/set.go new file mode 100644 index 0000000..32d8dc0 --- /dev/null +++ b/client/set.go @@ -0,0 +1,36 @@ +package client + +import ( + "fmt" + "io" + "net/http" +) + +const ( + errMandatoryParam = "Mandatory parameter %s missing" + errInvalidURL = "kapowURL, handlerId or path has invalid format" + errNotFound = "Resource Item Not Found" + errNotValidResource = "Invalid Resource Path" + serverURLTemplate = "%s/%s%s" +) + +func SetData(kapowURL, handlerId, path string, r io.Reader) error { + + req, err := http.NewRequest("PUT", fmt.Sprintf(serverURLTemplate, kapowURL, handlerId, path), r) + if err != nil { + return err + } + + kpowClient := &http.Client{} + if resp, err := kpowClient.Do(req); err != nil { + return err + } else if resp.StatusCode == http.StatusNoContent { + return fmt.Errorf(errNotFound) + } else if resp.StatusCode == http.StatusBadRequest { + return fmt.Errorf(errNotValidResource) + } else if resp.StatusCode >= http.StatusNotFound { + return fmt.Errorf(resp.Status[4:]) + } + + return nil +} diff --git a/client/set_test.go b/client/set_test.go new file mode 100644 index 0000000..e57fb1d --- /dev/null +++ b/client/set_test.go @@ -0,0 +1,103 @@ +package client_test + +import ( + "net/http" + "strings" + "testing" + + "github.com/BBVA/kapow/client" + + gock "gopkg.in/h2non/gock.v1" +) + +// 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()) + } +} + +// 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()) + } +} + +// Test that not found errors are detected as invalid handler id +func TestNotFound(t *testing.T) { + expectedErr := "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.StatusNotFound) + + 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()) + } +} + +// 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()) + } +} + +// Test a http ok request +func TestOkRequest(t *testing.T) { + host := "http://localhost:8080" + hid := "xxxxxxxxxxxxxx" + path := "/response/status/code" + reader := strings.NewReader("200") + + defer gock.Off() + + gock.New(host).Put("/" + hid + path).Reply(http.StatusOK) + + if err := client.SetData(host, hid, path, reader); err != nil { + t.Error("Unexpected error") + } +}