Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test for retrying POST requests #48

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ func TestClientTimeout(t *testing.T) {
assertEqual(t, true, strings.Contains(err.Error(), "i/o timeout"))
}

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

Expand All @@ -899,6 +899,49 @@ func TestClientRetry(t *testing.T) {
assertError(t, err)
}

func TestClientRetryPost(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails with the following output:

RESTY 2016/12/04 10:33:35 ERROR [error] Attempt [1]
RESTY 2016/12/04 10:33:35 ERROR [error] Attempt [2]
RESTY 2016/12/04 10:33:35 ERROR [error] Attempt [3]
	resty_test.go:1589: Method: POST
	resty_test.go:1590: Path: /usersmap
	resty_test.go:1591: Content-Type: application/json; charset=utf-8
	resty_test.go:1606: Got query param: status=500 so we're returning the post body as response and a 500 status code. body: [{"user1":{"frist_name":"firstname1","last_name":"lastname1","zip_code":"10001"}}]
	resty_test.go:1589: Method: POST
	resty_test.go:1590: Path: /usersmap
	resty_test.go:1591: Content-Type: application/json; charset=utf-8
	resty_test.go:1606: Got query param: status=500 so we're returning the post body as response and a 500 status code. body: 
	resty_test.go:1589: Method: POST
	resty_test.go:1590: Path: /usersmap
	resty_test.go:1591: Content-Type: application/json; charset=utf-8
	resty_test.go:1606: Got query param: status=500 so we're returning the post body as response and a 500 status code. body: 
	resty_test.go:1846: Error occurred [error]
	resty_test.go:929: Got response body: 
	resty_test.go:933: Could not decode json response: unexpected end of JSON input
	resty_test.go:936: Expected request body to be echoed back as response body. Instead got: 
exit status 1
FAIL	github.com/drichelson/resty	0.551s```

ts := createPostServer(t)
defer ts.Close()

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

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

c := dc()
c.SetRetryCount(3)
c.OnAfterResponse(func(c *Client, r *Response) error {
if r.StatusCode() >= 500 {
return errors.New("error")
}
return nil
})

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

assertError(t, err)
if resp.RawResponse != nil {
if resp.RawResponse.StatusCode == 500 {
t.Logf("Got response body: %s", string(resp.body))
var usersResponse []map[string]interface{}
err := json.Unmarshal(resp.body, usersResponse)
if err != nil {
t.Errorf("Could not decode json response: %s", err.Error())
}
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 TestClientTimeoutInternalError(t *testing.T) {
c := dc()
c.SetHTTPMode()
Expand Down Expand Up @@ -1555,6 +1598,17 @@ 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.WriteHeader(http.StatusInternalServerError)
w.Write(body)
return
}

var users []map[string]interface{}
jd := json.NewDecoder(r.Body)
err := jd.Decode(&users)
Expand Down