mirror of
https://github.com/ProtonMail/go-proton-api.git
synced 2025-12-23 23:57:50 -05:00
fix(GODT-2867): do not crash on timeout or context cancel.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user