forked from Silverfish/proton-bridge
GODT-1204: Handle importing too big messages
This commit is contained in:
committed by
Jakub Cuth
parent
1ed8e939b8
commit
e940d9f6fe
@ -27,7 +27,10 @@ import (
|
|||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxImportMessageRequestLength = 10
|
const (
|
||||||
|
MaxImportMessageRequestLength = 10
|
||||||
|
MaxImportMessageRequestSize = 25 * 1024 * 1024 // 25 MB total limit
|
||||||
|
)
|
||||||
|
|
||||||
type ImportMsgReq struct {
|
type ImportMsgReq struct {
|
||||||
Metadata *ImportMetadata // Metadata about the message to import.
|
Metadata *ImportMetadata // Metadata about the message to import.
|
||||||
@ -91,6 +94,14 @@ func (c *client) Import(ctx context.Context, reqs ImportMsgReqs) ([]*ImportMsgRe
|
|||||||
return nil, errors.New("request is too long")
|
return nil, errors.New("request is too long")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remainingSize := MaxImportMessageRequestSize
|
||||||
|
for _, req := range reqs {
|
||||||
|
remainingSize -= len(req.Message)
|
||||||
|
if remainingSize < 0 {
|
||||||
|
return nil, errors.New("request size is too big")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fields, err := reqs.buildMultipartFormData()
|
fields, err := reqs.buildMultipartFormData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
@ -115,3 +116,32 @@ func TestClient_Import(t *testing.T) { // nolint[funlen]
|
|||||||
r.Equal(t, 1, len(imported))
|
r.Equal(t, 1, len(imported))
|
||||||
r.Equal(t, testImportRes, imported[0])
|
r.Equal(t, testImportRes, imported[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClientImportBigSize(t *testing.T) {
|
||||||
|
s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
r.FailNow(t, "request is not dropped")
|
||||||
|
}))
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
const size = MaxImportMessageRequestSize + 1
|
||||||
|
msg := make([]byte, size)
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
msg[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
|
}
|
||||||
|
|
||||||
|
importRequest := []*ImportMsgReq{
|
||||||
|
{
|
||||||
|
Metadata: &ImportMetadata{
|
||||||
|
AddressID: "addressID",
|
||||||
|
Unread: Boolean(false),
|
||||||
|
Flags: FlagReceived | FlagImported,
|
||||||
|
LabelIDs: []string{ArchiveLabel},
|
||||||
|
},
|
||||||
|
Message: msg,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := c.Import(context.Background(), importRequest)
|
||||||
|
r.EqualError(t, err, "request size is too big")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user