Skip to content

Commit ec04033

Browse files
BayheckBayheck
andauthored
Upload file fixed (#8211)
<!-- Thank you for your contribution. Before making a PR, please read our contributing guidelines at https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution We recommend creating a *draft* PR, so that you can mark it as 'ready for review' when you are done. --> ## Purpose Protocol.Fetch.RequestPausedEvent postData is now deprecated resulting in file corruption in file uploads. ## Approach Replace postData with postDataEntries. ## References https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-Request closes #8198 ## Pre-Merge TODO - [ ] Write tests for your proposed changes - [ ] Make sure that existing tests do not fail --------- Co-authored-by: Bayheck <[email protected]>
1 parent ff478f3 commit ec04033

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

src/native-automation/request-pipeline/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,17 @@ export default class NativeAutomationRequestPipeline extends NativeAutomationApi
358358
}
359359

360360
private _getUploadPostData (event: Protocol.Fetch.RequestPausedEvent): string | undefined {
361-
if (!event.request.postData)
361+
if (!event.request.postDataEntries || !event.request.postDataEntries.length)
362362
return void 0;
363363

364364
const contentTypeHeader = event.request.headers['Content-Type'] as string;
365-
const postData = Buffer.from(event.request.postData, 'utf-8');
366-
const bodyWithUploads = injectUpload(contentTypeHeader, postData);
365+
const dataBuffers = [];
366+
367+
for (const dataEntry of event.request.postDataEntries)
368+
dataBuffers.push(Buffer.from(dataEntry.bytes ?? '', 'base64'));
369+
370+
const postData = Buffer.concat(dataBuffers);
371+
const bodyWithUploads = injectUpload(contentTypeHeader, postData);
367372

368373
return bodyWithUploads ? bodyWithUploads.toString('base64') : void 0;
369374
}

test/functional/fixtures/api/es-next/upload/pages/index.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,32 @@
1313
<input type="file" id="fileRequired" name="fileRequired" required />
1414
<input type="submit" id="submitRequired" />
1515
</form>
16+
<form>
17+
<input type="file" id="fileAlternative" />
18+
</form>
19+
<div id="uploadedContent"></div>
20+
<script>
21+
const fileInput = document.querySelector("#fileAlternative");
22+
const form = document.querySelector("form");
23+
const resultContainer = document.querySelector("#uploadedContent");
24+
25+
fileInput.addEventListener("change", async (e) => {
26+
const file = e.target.files[0];
27+
const formData = new FormData();
28+
formData.append("files", file);
29+
30+
try {
31+
const res = await fetch("/file-upload-size", {
32+
method: "post",
33+
body: formData,
34+
});
35+
const text = await res.text();
36+
resultContainer.textContent = text;
37+
form.reset();
38+
} catch (error) {
39+
console.error(error);
40+
}
41+
});
42+
</script>
1643
</body>
1744
</html>
6.65 MB
Binary file not shown.

test/functional/fixtures/api/es-next/upload/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ describe('[API] Upload', function () {
77
return runTests('./testcafe-fixtures/upload-test.js', 'Upload the file', { only: 'chrome' });
88
});
99

10+
it('Should upload the specified xls file', function () {
11+
return runTests('./testcafe-fixtures/upload-test.js', 'Upload the xls file', { only: 'chrome' });
12+
});
13+
1014
it('Should upload the specified file with required input', function () {
1115
return runTests('./testcafe-fixtures/upload-test.js', 'Upload the file with required input', { only: 'chrome' });
1216
});

test/functional/fixtures/api/es-next/upload/testcafe-fixtures/upload-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ test('Upload the file with required input', async t => {
4747

4848
expect(await getUploadedText()).equals('File 1 is uploaded!');
4949
});
50+
51+
test('Upload the xls file', async t => {
52+
const file1SizeBytes = 6971392;
53+
54+
await t
55+
.setFilesToUpload('#fileAlternative', '../test-data/file1.xls');
56+
57+
expect(Number(await getUploadedText())).to.be.at.least(file1SizeBytes);
58+
});

test/functional/site/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ Server.prototype._setupRoutes = function (apiRouter) {
230230
res.end(Mustache.render(UPLOAD_SUCCESS_PAGE_TEMPLATE, { uploadedDataArray: filesData }));
231231
});
232232

233+
this.app.post('/file-upload-size', upload.any(), function (req, res) {
234+
const filesData = req.files.map(function (file) {
235+
return file.size;
236+
});
237+
238+
res.end(`${filesData[0]}`);
239+
});
240+
233241
this.app.post('/xhr/test-header', function (req, res) {
234242
res.send(req.headers.test);
235243
});

0 commit comments

Comments
 (0)