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

fix: do not set query string on request retries #145

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
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
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())
}
}