Skip to content

Commit 5c08a66

Browse files
authored
Merge pull request #60 from snyk/fix/upload-revision-request-body
fix(uploadrevision): load request body into memory
2 parents e51829d + 674c951 commit 5c08a66

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

internal/fileupload/client_integration_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ func TestUploadDirectoryIntegration(t *testing.T) {
5050
assert.NotEqual(t, uuid.Nil, fileuploadRevisionID)
5151
}
5252

53+
func TestUploadLargeFileIntegration(t *testing.T) {
54+
fileUploadClient := setupFileUploadClient(t)
55+
56+
content := generateFileOfSizeMegabytes(t, 30)
57+
files := []uploadrevision.LoadedFile{
58+
{Path: "src/main.go", Content: content},
59+
}
60+
61+
dir, dirCleanup := createDirWithFiles(t, files)
62+
defer dirCleanup()
63+
64+
fileuploadRevisionID, err := fileUploadClient.CreateRevisionFromFile(t.Context(), filepath.Join(dir.Name(), files[0].Path), fileupload.UploadOptions{})
65+
if err != nil {
66+
t.Errorf("failed to create fileupload revision: %s", err.Error())
67+
}
68+
assert.NotEqual(t, uuid.Nil, fileuploadRevisionID)
69+
}
70+
5371
func setupFileUploadClient(t *testing.T) fileupload.Client {
5472
t.Helper()
5573

@@ -73,6 +91,12 @@ func setupFileUploadClient(t *testing.T) fileupload.Client {
7391
)
7492
}
7593

94+
func generateFileOfSizeMegabytes(t *testing.T, megabytes int) string {
95+
t.Helper()
96+
content := make([]byte, megabytes*1024*1024)
97+
return string(content)
98+
}
99+
76100
type testConfig struct {
77101
BaseURL string
78102
OrgID uuid.UUID

internal/fileupload/uploadrevision/client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ func NewClient(cfg Config, opts ...Opt) *HTTPSealableClient {
5656
opt(&c)
5757
}
5858

59-
crt := NewCompressionRoundTripper(c.httpClient.Transport)
60-
c.httpClient.Transport = crt
61-
6259
return &c
6360
}
6461

@@ -127,13 +124,22 @@ func (c *HTTPSealableClient) UploadFiles(ctx context.Context, orgID OrgID, revis
127124
mpartWriter := multipart.NewWriter(pipeWriter)
128125

129126
go streamFilesToPipe(pipeWriter, mpartWriter, files)
127+
body := compressRequestBody(pipeReader)
128+
129+
// Load body bytes into memmory so go can determine the Content-Length
130+
// and not send the request chunked
131+
bts, err := io.ReadAll(body)
132+
if err != nil {
133+
return fmt.Errorf("failed to create upload files request: %w", err)
134+
}
130135

131136
url := fmt.Sprintf("%s/hidden/orgs/%s/upload_revisions/%s/files?version=%s", c.cfg.BaseURL, orgID, revisionID, apiVersion)
132-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, pipeReader)
137+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bts))
133138
if err != nil {
134139
return fmt.Errorf("failed to create upload files request: %w", err)
135140
}
136141
req.Header.Set(ContentType, mpartWriter.FormDataContentType())
142+
req.Header.Set(ContentEncoding, "gzip")
137143

138144
res, err := c.httpClient.Do(req)
139145
if err != nil {

internal/fileupload/uploadrevision/client_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestClient_CreateRevision(t *testing.T) {
2626
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2727
assert.Equal(t, http.MethodPost, r.Method)
2828
assert.Equal(t, "application/vnd.api+json", r.Header.Get("Content-Type"))
29-
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
3029
assert.Equal(t, fmt.Sprintf("/hidden/orgs/%s/upload_revisions", orgID), r.URL.Path)
3130
assert.Equal(t, "2024-10-15", r.URL.Query().Get("version"))
3231

@@ -383,7 +382,6 @@ func TestClient_SealRevision(t *testing.T) {
383382
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
384383
assert.Equal(t, http.MethodPatch, r.Method)
385384
assert.Equal(t, "application/vnd.api+json", r.Header.Get("Content-Type"))
386-
assert.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
387385
assert.Equal(t, fmt.Sprintf("/hidden/orgs/%s/upload_revisions/%s", orgID, revID), r.URL.Path)
388386
assert.Equal(t, "2024-10-15", r.URL.Query().Get("version"))
389387

0 commit comments

Comments
 (0)