Skip to content

Commit

Permalink
#47 retry request body fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Dec 29, 2016
1 parent 27219c0 commit f593e5e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
1 change: 1 addition & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {
var bodyBytes []byte
contentType := r.Header.Get(hdrContentTypeKey)
kind := kindOf(r.Body)
r.bodyBuf = nil

if reader, ok := r.Body.(io.Reader); ok {
r.bodyBuf = &bytes.Buffer{}
Expand Down
27 changes: 13 additions & 14 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,20 +885,6 @@ func TestClientTimeout(t *testing.T) {
assertEqual(t, true, strings.Contains(err.Error(), "i/o timeout"))
}

func TestClientRetry(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc()
c.SetHTTPMode().
SetTimeout(time.Duration(time.Second * 3)).
SetRetryCount(3)

_, err := c.R().Get(ts.URL + "/set-retrycount-test")

assertError(t, err)
}

func TestClientTimeoutInternalError(t *testing.T) {
c := dc()
c.SetHTTPMode()
Expand Down Expand Up @@ -1545,6 +1531,7 @@ func createPostServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
t.Logf("RawQuery: %v", r.URL.RawQuery)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))

if r.Method == MethodPost {
Expand All @@ -1555,6 +1542,18 @@ func createPostServer(t *testing.T) *httptest.Server {
if r.URL.Path == "/usersmap" {
// JSON
if IsJSONType(r.Header.Get(hdrContentTypeKey)) {
if r.URL.Query().Get("status") == "500" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error: could not read post body: %s", err.Error())
}
t.Logf("Got query param: status=500 so we're returning the post body as response and a 500 status code. body: %s", string(body))
w.Header().Set(hdrContentTypeKey, jsonContentType)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write(body)
return
}

var users []map[string]interface{}
jd := json.NewDecoder(r.Body)
err := jd.Decode(&users)
Expand Down
62 changes: 62 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package resty

import (
"encoding/json"
"errors"
"net/http"
"reflect"
"runtime"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -158,6 +161,65 @@ func TestConditionalGetDefaultClient(t *testing.T) {
logResponse(t, resp)
}

func TestClientRetryGet(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc()
c.SetHTTPMode().
SetTimeout(time.Duration(time.Second * 3)).
SetRetryCount(3)

_, err := c.R().Get(ts.URL + "/set-retrycount-test")

assertError(t, err)
}

func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

func TestClientRetryPost(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

usersmap := map[string]interface{}{
"user1": map[string]interface{}{"FirstName": "firstname1", "LastName": "lastname1", "ZipCode": "10001"},
}

var users []map[string]interface{}
users = append(users, usersmap)

c := dc()
c.SetRetryCount(3)
c.AddRetryCondition(RetryConditionFunc(func(r *Response) (bool, error) {
if r.StatusCode() >= http.StatusInternalServerError {
return false, errors.New("error")
}
return true, nil
}))

resp, _ := c.R().
SetBody(&users).
Post(ts.URL + "/usersmap?status=500")

if resp != nil {
if resp.StatusCode() == http.StatusInternalServerError {
t.Logf("Got response body: %s", string(resp.body))
var usersResponse []map[string]interface{}
err := json.Unmarshal(resp.body, &usersResponse)
assertError(t, err)

if !reflect.DeepEqual(users, usersResponse) {
t.Errorf("Expected request body to be echoed back as response body. Instead got: %s", string(resp.body))
}

return
}
t.Errorf("Got unexpected response code: %d with body: %s", resp.StatusCode(), string(resp.body))
}
}

func filler(*Response) (bool, error) {
return false, nil
}

0 comments on commit f593e5e

Please sign in to comment.