Skip to content

Commit

Permalink
Do function panics when HTTPClient returns a nil response (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
agatamigalska-newstore authored and brandur-stripe committed Nov 12, 2018
1 parent 595a6ef commit 0e4f60c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
22 changes: 14 additions & 8 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,22 @@ func (s *BackendImplementation) Do(req *http.Request, body *bytes.Buffer, v inte
break
}

resBody, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
if s.LogLevel > 0 {
s.Logger.Printf("Cannot read response: %v\n", err)
s.Logger.Printf("Request failed with error: %v\n", err)
}
} else {
resBody, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
if s.LogLevel > 0 {
s.Logger.Printf("Cannot read response: %v\n", err)
}
} else {
if s.LogLevel > 0 {
s.Logger.Printf("Request failed with body: %s (status: %v)\n", string(resBody), res.StatusCode)
}
}
return err
}

if s.LogLevel > 0 {
s.Logger.Printf("Request failed with: %s (error: %v)\n", string(resBody), err)
}

sleepDuration := s.sleepTime(retry)
Expand All @@ -381,6 +386,7 @@ func (s *BackendImplementation) Do(req *http.Request, body *bytes.Buffer, v inte
}
return err
}

defer res.Body.Close()

resBody, err := ioutil.ReadAll(res.Body)
Expand Down
49 changes: 48 additions & 1 deletion stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"regexp"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go"
. "github.com/stripe/stripe-go/testing"
)

Expand Down Expand Up @@ -118,6 +120,51 @@ func TestDo_Retry(t *testing.T) {
assert.Equal(t, 2, requestNum)
}

func TestDo_RetryOnTimeout(t *testing.T) {
type testServerResponse struct {
Message string `json:"message"`
}

timeout := time.Second
var counter uint32

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddUint32(&counter, 1)
time.Sleep(timeout)
}))
defer testServer.Close()

backend := stripe.GetBackendWithConfig(
stripe.APIBackend,
&stripe.BackendConfig{
LogLevel: 3,
MaxNetworkRetries: 1,
URL: testServer.URL,
HTTPClient: &http.Client{Timeout: timeout},
},
).(*stripe.BackendImplementation)

backend.SetNetworkRetriesSleep(false)

request, err := backend.NewRequest(
http.MethodPost,
"/hello",
"sk_test_123",
"application/x-www-form-urlencoded",
nil,
)
assert.NoError(t, err)

var body = bytes.NewBufferString("foo=bar")
var response testServerResponse

err = backend.Do(request, body, &response)

assert.Error(t, err)
// timeout should not prevent retry
assert.Equal(t, uint32(2), atomic.LoadUint32(&counter))
}

func TestFormatURLPath(t *testing.T) {
assert.Equal(t, "/resources/1/subresources/2",
stripe.FormatURLPath("/resources/%s/subresources/%s", "1", "2"))
Expand Down

0 comments on commit 0e4f60c

Please sign in to comment.