Merge branch 'master' of ssh://github.com/BBVA/kapow

This commit is contained in:
Héctor Hurtado
2019-10-04 14:33:00 +02:00
3 changed files with 38 additions and 55 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"github.com/BBVA/kapow/internal/http"
)
//GetData will perform the request and write the results on the provided writer
// GetData will perform the request and write the results on the provided writer
func GetData(host, id, path string, wr io.Writer) error {
url := host + "/handlers/" + id + path
return http.Get(url, "", nil, wr)
+29 -44
View File
@@ -1,63 +1,48 @@
package client
import (
"bufio"
"bytes"
"net/http"
"os"
"testing"
gock "gopkg.in/h2non/gock.v1"
)
func TestGetInvalidUrl(t *testing.T) {
err := GetData("", "", "", os.Stdout)
if err == nil {
t.Error("Expected error with invalid url ''")
}
}
func TestGetInvalidWriter(t *testing.T) {
err := GetData("http://localhost:8081", "0000", "/", nil)
if err == nil {
t.Error("Expected error with no writer")
}
}
func TestGetURLNotFoundWithUnknownID(t *testing.T) {
func TestWriteContentToWriter(t *testing.T) {
defer gock.Off()
gock.New("http://localhost").
Get("/handlers/THIS-IS-THE-HANDLER-ID/request/body").
Reply(http.StatusOK).
Body(bytes.NewReader([]byte("FOO")))
gock.New("http://localhost:8081").
Get("/handlers/000/").Reply(http.StatusNotFound)
var b bytes.Buffer
buf := bufio.NewWriter(&b)
err := GetData("http://localhost", "THIS-IS-THE-HANDLER-ID", "/request/body", buf)
err := GetData("http://localhost:8081", "000", "/", os.Stdout)
if err == nil {
t.Errorf("Expect not found error but get no error")
}
if gock.IsDone() == false {
t.Error("No expected endpoint called")
}
}
func TestGetRetrieveRequestMethod(t *testing.T) {
defer gock.Off()
gock.New("http://localhost:8081").
Get("/handlers/000/request/method").
Reply(http.StatusAccepted).
BodyString("POST")
rw := new(bytes.Buffer)
err := GetData("http://localhost:8081", "000", "/request/method", rw)
if err != nil {
t.Errorf("Unexpected error %v", err)
t.Errorf("Unexpected error: %q", err)
}
strRes := rw.String()
if strRes != "POST" {
t.Errorf("POST string expected but found: '%v'", strRes)
if !bytes.Equal(b.Bytes(), []byte("FOO")) {
t.Errorf("Received content mismatch: %q != %q", b.Bytes(), []byte("FOO"))
}
if gock.IsDone() == false {
t.Error("No expected endpoint called")
}
}
func TestPropagateHTTPError(t *testing.T) {
defer gock.Off()
gock.New("http://localhost").
Get("/handlers/THIS-IS-THE-HANDLER-ID/request/body").
Reply(http.StatusTeapot)
err := GetData("http://localhost", "THIS-IS-THE-HANDLER-ID", "/request/body", nil)
if err == nil {
t.Errorf("Expected error not returned")
}
if gock.IsDone() == false {
+8 -10
View File
@@ -7,32 +7,32 @@ import (
"net/http"
)
//Get perform a request using Request with the GET method
// Get perform a request using Request with the GET method
func Get(url string, contentType string, r io.Reader, w io.Writer) error {
return Request("GET", url, contentType, r, w)
}
//Post perform a request using Request with the POST method
// Post perform a request using Request with the POST method
func Post(url string, contentType string, r io.Reader, w io.Writer) error {
return Request("POST", url, contentType, r, w)
}
//Put perform a request using Request with the PUT method
// Put perform a request using Request with the PUT method
func Put(url string, contentType string, r io.Reader, w io.Writer) error {
return Request("PUT", url, contentType, r, w)
}
//Delete perform a request using Request with the DELETE method
// Delete perform a request using Request with the DELETE method
func Delete(url string, contentType string, r io.Reader, w io.Writer) error {
return Request("DELETE", url, contentType, r, w)
}
var devnull = ioutil.Discard
//Request will perform the request to the given url and method sending the
//content of the given reader as the body and writing all the contents
//of the response to the given writer. The reader and writer are
//optional.
// Request will perform the request to the given url and method sending the
// content of the given reader as the body and writing all the contents
// of the response to the given writer. The reader and writer are
// optional.
func Request(method string, url string, contentType string, r io.Reader, w io.Writer) error {
req, err := http.NewRequest(method, url, r)
if err != nil {
@@ -65,6 +65,4 @@ func Request(method string, url string, contentType string, r io.Reader, w io.Wr
}
return err
// TODO: close the connection, otherwise we'll have a port leak in the server
}