Shorthand functions request with common HTTP methods

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-10-03 15:06:26 +02:00
parent 43abacbb2f
commit b1c2db057c
3 changed files with 30 additions and 10 deletions
+23 -3
View File
@@ -7,13 +7,33 @@ import (
"net/http"
)
var devnull io.Writer = ioutil.Discard
//Get perform a request using Request with the GET method
func Get(url string, r io.Reader, w io.Writer) error {
return Request("GET", url, r, w)
}
//Do will perform the request to the given url and method sending the
//Post perform a request using Request with the POST method
func Post(url string, r io.Reader, w io.Writer) error {
return Request("POST", url, r, w)
}
//Put perform a request using Request with the PUT method
func Put(url string, r io.Reader, w io.Writer) error {
return Request("PUT", url, r, w)
}
//Delete perform a request using Request with the DELETE method
func Delete(url string, r io.Reader, w io.Writer) error {
return Request("DELETE", url, 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.
func Do(method string, url string, r io.Reader, w io.Writer) error {
func Request(method string, url string, r io.Reader, w io.Writer) error {
req, err := http.NewRequest(method, url, r)
if err != nil {
return err
+6 -6
View File
@@ -13,7 +13,7 @@ func TestReturnErrorOnInvalidURL(t *testing.T) {
defer gock.Off()
gock.New("").Reply(200)
err := Do("GET", "://", nil, nil)
err := Request("GET", "://", nil, nil)
if err == nil {
t.Errorf("Expected error not returned")
}
@@ -29,7 +29,7 @@ func TestRequestGivenMethod(t *testing.T) {
mock.Method = "FOO"
mock.Reply(200)
err := Do("FOO", "http://localhost", nil, nil)
err := Request("FOO", "http://localhost", nil, nil)
if err != nil {
t.Errorf("Unexpected error on request")
}
@@ -44,7 +44,7 @@ func TestReturnHTTPErrorAsIs(t *testing.T) {
customError := errors.New("FOO")
gock.New("http://localhost").ReplyError(customError)
err := Do("GET", "http://localhost", nil, nil)
err := Request("GET", "http://localhost", nil, nil)
if errors.Unwrap(err) != customError {
t.Errorf("Returned error is not the expected error")
}
@@ -58,7 +58,7 @@ func TestReturnHTTPReasonAsErrorWhenUnsuccessful(t *testing.T) {
defer gock.Off()
gock.New("http://localhost").Reply(http.StatusTeapot)
err := Do("GET", "http://localhost", nil, nil)
err := Request("GET", "http://localhost", nil, nil)
if err == nil || err.Error() != http.StatusText(http.StatusTeapot) {
t.Errorf("Reason should be returned as an error")
}
@@ -75,7 +75,7 @@ func TestCopyResponseBodyToWriter(t *testing.T) {
rw := new(bytes.Buffer)
err := Do("GET", "http://localhost", nil, rw)
err := Request("GET", "http://localhost", nil, rw)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
@@ -101,7 +101,7 @@ func TestWriteToDevNullWhenNoWriter(t *testing.T) {
defer func() { devnull = original }()
err := Do("GET", "http://localhost", nil, nil)
err := Request("GET", "http://localhost", nil, nil)
if err != nil {
t.Errorf("Unexpected error %v", err)
}