Skip to content

Commit

Permalink
Merge pull request #145 from jdolce/fix-retry-query
Browse files Browse the repository at this point in the history
fix: do not set query string on request retries
  • Loading branch information
imjaroiswebdev authored Dec 12, 2023
2 parents b4c4cc9 + f3730d5 commit b3dce1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (c *Client) newRequestDoContext(ctx context.Context, method, url string, qr
resp, err := c.do(req, v)
if err != nil {
if respErr, ok := err.(*Error); ok && respErr.needToRetry {
return c.newRequestDoContext(ctx, method, url, qryOptions, body, v)
return c.newRequestDoContext(ctx, method, url, nil, body, v)
}

return nil, err
Expand Down Expand Up @@ -368,7 +368,7 @@ func (c *Client) newRequestDoOptionsContext(ctx context.Context, method, url str
resp, err := c.do(req, v)
if err != nil {
if respErr, ok := err.(*Error); ok && respErr.needToRetry {
return c.newRequestDoOptionsContext(ctx, method, url, qryOptions, body, v)
return c.newRequestDoOptionsContext(ctx, method, url, nil, body, v)
}

return nil, err
Expand Down
44 changes: 44 additions & 0 deletions pagerduty/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pagerduty

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -100,3 +101,46 @@ func TestClientUserAgentOverwritten(t *testing.T) {
t.Errorf("got %q, want %q", client.Config.UserAgent, newUserAgent)
}
}

func TestRetryURL(t *testing.T) {

setup()
defer teardown()

timesCalled := 0
expectedURL := "/members?offset=100"

options := GetMembersOptions{
Offset: 100,
}

mux.HandleFunc("/members", func(w http.ResponseWriter, r *http.Request) {
timesCalled++
testMethod(t, r, "GET")
url := r.URL.String()
if url != expectedURL {
t.Fatalf("Request url: %v, want %v", url, expectedURL)
}

if timesCalled > 1 {
w.WriteHeader(http.StatusOK)
return
}

w.Header().Set("Ratelimit-Reset", "1")
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{"error":{"code":"2020", "message":"Rate Limit Exceeded"}}`))

})

_, err := client.newRequestDoOptionsContext(context.Background(), http.MethodGet, "/members", options, nil, nil)
if err != nil {
t.Fatalf(err.Error())
}

timesCalled = 0
_, err = client.newRequestDoContext(context.Background(), http.MethodGet, "/members", options, nil, nil)
if err != nil {
t.Fatalf(err.Error())
}
}

0 comments on commit b3dce1e

Please sign in to comment.