fix(GODT-2867): do not crash on timeout or context cancel.

This commit is contained in:
Jakub
2023-08-31 07:20:19 +02:00
committed by cuthix
parent d12b1baf9b
commit 0e3a549b3f
2 changed files with 39 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"sync"
"testing"
"github.com/ProtonMail/go-proton-api"
@@ -42,3 +43,40 @@ func TestAttachment_429Response(t *testing.T) {
require.Equal(t, proton.InvalidValue, apiErr.Code)
require.Equal(t, "Request failed with status 429", apiErr.Message)
}
func TestAttachment_ContextCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
s := server.New()
defer s.Close()
m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
_, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
c, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass"))
require.NoError(t, err)
wg := sync.WaitGroup{}
wg.Add(1)
s.AddStatusHook(func(r *http.Request) (int, bool) {
wg.Wait()
return http.StatusTooManyRequests, true
})
go func() {
_, err = c.GetAttachment(ctx, "someID")
wg.Done()
}()
cancel()
wg.Wait()
require.Error(t, err)
require.True(t, errors.Is(err, context.Canceled))
}

View File

@@ -192,7 +192,7 @@ func catchDropError(_ *resty.Response, err error) bool {
//
// This function also closes the response body.
func parseResponse(res *resty.Response, err error) (*resty.Response, error) {
if res.StatusCode() == 200 {
if err != nil || res.StatusCode() == 200 {
return res, err
}