http becomes an internal package
This commit is contained in:
@@ -3,7 +3,7 @@ package client
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/BBVA/kapow/http"
|
||||
"github.com/BBVA/kapow/internal/http"
|
||||
)
|
||||
|
||||
//GetData will perform the request and write the results on the provided writer
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetReason(r *http.Response) string {
|
||||
if i := strings.IndexByte(r.Status, ' '); i != -1 {
|
||||
return r.Status[i+1:]
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
nethttp "net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmptyReasonWhenEmptyString(t *testing.T) {
|
||||
r := &nethttp.Response{Status: ""}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an empty status line to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyReasonWhenOnlyCode(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200"}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an status line with just the status code to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyReasonWhenOnlyCodePlusSpace(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 "}
|
||||
if GetReason(r) != "" {
|
||||
t.Errorf("We consider an status line with just the status code to have an empty reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReasonOfOneWord(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 FOO"}
|
||||
if GetReason(r) != "FOO" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReasonOfMultipleWords(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "200 FOO BAR BAZ"}
|
||||
if GetReason(r) != "FOO BAR BAZ" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOddSizeStatusCode(t *testing.T) {
|
||||
r := &nethttp.Response{Status: "2 FOO BAR BAZ"}
|
||||
if GetReason(r) != "FOO BAR BAZ" {
|
||||
t.Errorf("Unexpected reason found")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var devnull io.Writer = ioutil.Discard
|
||||
|
||||
//Do 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 Do(method string, url string, r io.Reader, w io.Writer) error {
|
||||
req, err := http.NewRequest(method, url, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(req)
|
||||
|
||||
if res != nil {
|
||||
defer res.Body.Close()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return errors.New(GetReason(res))
|
||||
}
|
||||
|
||||
if w == nil {
|
||||
_, err = io.Copy(devnull, res.Body)
|
||||
} else {
|
||||
_, err = io.Copy(w, res.Body)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
gock "gopkg.in/h2non/gock.v1"
|
||||
)
|
||||
|
||||
func TestReturnErrorOnInvalidURL(t *testing.T) {
|
||||
defer gock.Off()
|
||||
gock.New("").Reply(200)
|
||||
|
||||
err := Do("GET", "://", nil, nil)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error not returned")
|
||||
}
|
||||
|
||||
if gock.IsDone() {
|
||||
t.Errorf("Request was performed anyway")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestGivenMethod(t *testing.T) {
|
||||
defer gock.Off()
|
||||
mock := gock.New("http://localhost")
|
||||
mock.Method = "FOO"
|
||||
mock.Reply(200)
|
||||
|
||||
err := Do("FOO", "http://localhost", nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error on request")
|
||||
}
|
||||
|
||||
if gock.IsDone() == false {
|
||||
t.Errorf("Expected request not performed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnHTTPErrorAsIs(t *testing.T) {
|
||||
defer gock.Off()
|
||||
customError := errors.New("FOO")
|
||||
gock.New("http://localhost").ReplyError(customError)
|
||||
|
||||
err := Do("GET", "http://localhost", nil, nil)
|
||||
if errors.Unwrap(err) != customError {
|
||||
t.Errorf("Returned error is not the expected error")
|
||||
}
|
||||
|
||||
if gock.IsDone() == false {
|
||||
t.Errorf("Expected request not performed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReturnHTTPReasonAsErrorWhenUnsuccessful(t *testing.T) {
|
||||
defer gock.Off()
|
||||
gock.New("http://localhost").Reply(http.StatusTeapot)
|
||||
|
||||
err := Do("GET", "http://localhost", nil, nil)
|
||||
if err == nil || err.Error() != http.StatusText(http.StatusTeapot) {
|
||||
t.Errorf("Reason should be returned as an error")
|
||||
}
|
||||
|
||||
if gock.IsDone() == false {
|
||||
t.Errorf("Expected request not performed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyResponseBodyToWriter(t *testing.T) {
|
||||
defer gock.Off()
|
||||
|
||||
gock.New("http://localhost").Reply(200).BodyString("FOO")
|
||||
|
||||
rw := new(bytes.Buffer)
|
||||
|
||||
err := Do("GET", "http://localhost", nil, rw)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
|
||||
res := rw.String()
|
||||
|
||||
if res != "FOO" {
|
||||
t.Errorf("Unexpected output %v", res)
|
||||
}
|
||||
|
||||
if gock.IsDone() == false {
|
||||
t.Error("No expected endpoint called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteToDevNullWhenNoWriter(t *testing.T) {
|
||||
defer gock.Off()
|
||||
|
||||
gock.New("http://localhost").Reply(200).BodyString("FOO")
|
||||
|
||||
original := devnull
|
||||
devnull = new(bytes.Buffer)
|
||||
|
||||
defer func() { devnull = original }()
|
||||
|
||||
err := Do("GET", "http://localhost", nil, nil)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %v", err)
|
||||
}
|
||||
|
||||
res := devnull.(*bytes.Buffer).String()
|
||||
|
||||
if res != "FOO" {
|
||||
t.Errorf("Unexpected output %v", res)
|
||||
}
|
||||
|
||||
if gock.IsDone() == false {
|
||||
t.Error("No expected endpoint called")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user