Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[desktop] fix not sending Content-Length header in native file upload #7956

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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