Skip to content

Commit

Permalink
[desktop] fix not sending Content-Length header in native file upload
Browse files Browse the repository at this point in the history
Co-authored-by: nif <nif@tutao.de>
  • Loading branch information
ganthern and nif-tutao committed Nov 15, 2024
1 parent 307e3df commit 91e239b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/common/desktop/files/DesktopFileFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export class DesktopFileFacade implements FileFacade {

async upload(fileUri: string, targetUrl: string, method: string, headers: Record<string, string>): Promise<UploadTaskResponse> {
const fileStream = this.fs.createReadStream(fileUri)
const stat = await this.fs.promises.stat(fileUri)
headers["Content-Length"] = `${stat.size}`
const response = await this.net.executeRequest(new URL(targetUrl), { method, headers, timeout: 20000 }, fileStream)

let responseBody: Uint8Array
Expand Down
28 changes: 25 additions & 3 deletions test/tests/desktop/files/DesktopFileFacadeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ o.spec("DesktopFileFacade", function () {
fs = object()
tfs = object()
fs.promises = object()
when(fs.promises.stat(matchers.anything())).thenResolve({ size: 42 })
electron = object()
// @ts-ignore read-only prop
electron.shell = object()
Expand Down Expand Up @@ -210,7 +211,13 @@ o.spec("DesktopFileFacade", function () {
const fileToUploadPath = "/tutnaota/tmp/path/encrypted/toUpload.txt"
const targetUrl = "https://test.tutanota.com/rest/for/a/bit"

function mockResponse(statusCode: number, resOpts: { responseBody?: Uint8Array; responseHeaders?: Record<string, string> }): http.IncomingMessage {
function mockResponse(
statusCode: number,
resOpts: {
responseBody?: Uint8Array
responseHeaders?: Record<string, string>
},
): http.IncomingMessage {
const { responseBody, responseHeaders } = resOpts
const response: http.IncomingMessage = object()
response.statusCode = statusCode
Expand All @@ -237,7 +244,17 @@ o.spec("DesktopFileFacade", function () {
}
const fileStreamMock = mockReadStream()
when(fs.createReadStream(fileToUploadPath)).thenReturn(fileStreamMock)
when(net.executeRequest(urlMatches(new URL(targetUrl)), { method: "POST", headers, timeout: 20000 }, fileStreamMock)).thenResolve(response)
when(
net.executeRequest(
urlMatches(new URL(targetUrl)),
{
method: "POST",
headers,
timeout: 20000,
},
fileStreamMock,
),
).thenResolve(response)
const uploadResult = await ff.upload(fileToUploadPath, targetUrl, "POST", headers)

o(uploadResult.statusCode).equals(200)
Expand Down Expand Up @@ -332,7 +349,12 @@ o.spec("DesktopFileFacade", function () {

o("open on windows", async function () {
n.setPlatform("win32")
when(electron.dialog.showMessageBox(matchers.anything())).thenReturn(Promise.resolve({ response: 1, checkboxChecked: false }))
when(electron.dialog.showMessageBox(matchers.anything())).thenReturn(
Promise.resolve({
response: 1,
checkboxChecked: false,
}),
)
await ff.open("exec.exe")
verify(electron.shell.openPath(matchers.anything()), { times: 0 })
})
Expand Down

0 comments on commit 91e239b

Please sign in to comment.