Allow user specify Content-Type header
This commit is contained in:
+15
-10
@@ -8,23 +8,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//Get perform a request using Request with the GET method
|
//Get perform a request using Request with the GET method
|
||||||
func Get(url string, r io.Reader, w io.Writer) error {
|
func Get(url string, contentType string, r io.Reader, w io.Writer) error {
|
||||||
return Request("GET", url, r, w)
|
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, r io.Reader, w io.Writer) error {
|
func Post(url string, contentType string, r io.Reader, w io.Writer) error {
|
||||||
return Request("POST", url, r, w)
|
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, r io.Reader, w io.Writer) error {
|
func Put(url string, contentType string, r io.Reader, w io.Writer) error {
|
||||||
return Request("PUT", url, r, w)
|
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, r io.Reader, w io.Writer) error {
|
func Delete(url string, contentType string, r io.Reader, w io.Writer) error {
|
||||||
return Request("DELETE", url, r, w)
|
return Request("DELETE", url, contentType, r, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
var devnull = ioutil.Discard
|
var devnull = ioutil.Discard
|
||||||
@@ -33,12 +33,16 @@ var devnull = ioutil.Discard
|
|||||||
//content of the given reader as the body and writing all the contents
|
//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
|
//of the response to the given writer. The reader and writer are
|
||||||
//optional.
|
//optional.
|
||||||
func Request(method string, url string, r io.Reader, w io.Writer) error {
|
func Request(method string, url string, contentType string, r io.Reader, w io.Writer) error {
|
||||||
req, err := http.NewRequest(method, url, r)
|
req, err := http.NewRequest(method, url, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if contentType != "" {
|
||||||
|
req.Header.Add("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
|
|
||||||
@@ -60,6 +64,7 @@ func Request(method string, url string, r io.Reader, w io.Writer) error {
|
|||||||
_, err = io.Copy(w, res.Body)
|
_, err = io.Copy(w, res.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: close the connection, otherwise we'll have a port leak in the server
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
|
// TODO: close the connection, otherwise we'll have a port leak in the server
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func TestReturnErrorOnInvalidURL(t *testing.T) {
|
|||||||
defer gock.Off()
|
defer gock.Off()
|
||||||
gock.New("").Reply(200)
|
gock.New("").Reply(200)
|
||||||
|
|
||||||
err := Request("GET", "://", nil, nil)
|
err := Request("GET", "://", "", nil, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error not returned")
|
t.Errorf("Expected error not returned")
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ func TestRequestGivenMethod(t *testing.T) {
|
|||||||
mock.Method = "FOO"
|
mock.Method = "FOO"
|
||||||
mock.Reply(200)
|
mock.Reply(200)
|
||||||
|
|
||||||
err := Request("FOO", "http://localhost", nil, nil)
|
err := Request("FOO", "http://localhost", "", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error on request")
|
t.Errorf("Unexpected error on request")
|
||||||
}
|
}
|
||||||
@@ -44,9 +44,9 @@ func TestReturnHTTPErrorAsIs(t *testing.T) {
|
|||||||
customError := errors.New("FOO")
|
customError := errors.New("FOO")
|
||||||
gock.New("http://localhost").ReplyError(customError)
|
gock.New("http://localhost").ReplyError(customError)
|
||||||
|
|
||||||
err := Request("GET", "http://localhost", nil, nil)
|
err := Request("GET", "http://localhost", "", nil, nil)
|
||||||
if errors.Unwrap(err) != customError {
|
if errors.Unwrap(err) != customError {
|
||||||
t.Errorf("Returned error is not the expected error")
|
t.Errorf("Returned error is not the expected error: '%v'", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gock.IsDone() == false {
|
if gock.IsDone() == false {
|
||||||
@@ -58,7 +58,7 @@ func TestReturnHTTPReasonAsErrorWhenUnsuccessful(t *testing.T) {
|
|||||||
defer gock.Off()
|
defer gock.Off()
|
||||||
gock.New("http://localhost").Reply(http.StatusTeapot)
|
gock.New("http://localhost").Reply(http.StatusTeapot)
|
||||||
|
|
||||||
err := Request("GET", "http://localhost", nil, nil)
|
err := Request("GET", "http://localhost", "", nil, nil)
|
||||||
if err == nil || err.Error() != http.StatusText(http.StatusTeapot) {
|
if err == nil || err.Error() != http.StatusText(http.StatusTeapot) {
|
||||||
t.Errorf("Reason should be returned as an error")
|
t.Errorf("Reason should be returned as an error")
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ func TestCopyResponseBodyToWriter(t *testing.T) {
|
|||||||
|
|
||||||
rw := new(bytes.Buffer)
|
rw := new(bytes.Buffer)
|
||||||
|
|
||||||
err := Request("GET", "http://localhost", nil, rw)
|
err := Request("GET", "http://localhost", "", nil, rw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error %v", err)
|
t.Errorf("Unexpected error %v", err)
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ func TestWriteToDevNullWhenNoWriter(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { devnull = original }()
|
defer func() { devnull = original }()
|
||||||
|
|
||||||
err := Request("GET", "http://localhost", nil, nil)
|
err := Request("GET", "http://localhost", "", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error %v", err)
|
t.Errorf("Unexpected error %v", err)
|
||||||
}
|
}
|
||||||
@@ -116,3 +116,20 @@ func TestWriteToDevNullWhenNoWriter(t *testing.T) {
|
|||||||
t.Error("No expected endpoint called")
|
t.Error("No expected endpoint called")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendContentType(t *testing.T) {
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New("http://localhost").
|
||||||
|
MatchHeader("Content-Type", "foo/bar").
|
||||||
|
HeaderPresent("Content-Type").
|
||||||
|
Reply(http.StatusOK)
|
||||||
|
|
||||||
|
err := Request("GET", "http://localhost", "foo/bar", nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error '%v'", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if gock.IsDone() == false {
|
||||||
|
t.Error("No expected endpoint called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user