Added set command fuction to client package
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user