Skip to content

Commit

Permalink
Add test to check that a big file is written completely to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed Apr 23, 2020
1 parent b956eca commit 509dce0
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions std/mime/multipart_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

const { Buffer, copy, open, test } = Deno;
const { Buffer, open, test } = Deno;
import {
assert,
assertEquals,
Expand All @@ -21,6 +21,7 @@ const e = new TextEncoder();
const boundary = "--abcde";
const dashBoundary = e.encode("--" + boundary);
const nlDashBoundary = e.encode("\r\n--" + boundary);
const testdataDir = path.resolve("mime", "testdata");

test(function multipartScanUntilBoundary1(): void {
const data = `--${boundary}`;
Expand Down Expand Up @@ -190,29 +191,45 @@ test({
});

test({
name: "[mime/multipart] readForm() should store big file in temp file",
name:
"[mime/multipart] readForm() should store big file completely in temp file",
async fn() {
const o = await open(path.resolve("./mime/testdata/sample.txt"));
const mr = new MultipartReader(
o,
"--------------------------434049563556637648550474"
);
const encoder = new TextEncoder();
const multipartFile = path.join(testdataDir, "form-data.dat");
const sampleFile = path.resolve(testdataDir, "bigfile.txt");
const writer = await open(multipartFile, { write: true, create: true });

const fileData = encoder.encode("a".repeat(1 << 24)); // 16mb
const size = fileData.length;

await Deno.writeFile(sampleFile, fileData);
const bigFile = await open(sampleFile, "r");

const mw = new MultipartWriter(writer);
await mw.writeField("deno", "land");
await mw.writeField("bar", "bar");
await mw.writeFile("file", "sample.txt", bigFile);

await mw.close();
writer.close();
bigFile.close();

const o = await Deno.open(multipartFile);
const mr = new MultipartReader(o, mw.boundary);
// use low-memory to write "file" into temp file.
const form = await mr.readForm(20);
try {
assertEquals(form.value("foo"), "foo");
assertEquals(form.value("deno"), "land");
assertEquals(form.value("bar"), "bar");
const file = form.file("file");
assert(file != null);
assertEquals(file.type, "application/octet-stream");
assert(file.tempfile != null);
const f = await open(file.tempfile);
const w = new StringWriter();
await copy(w, f);
const json = JSON.parse(w.toString());
assertEquals(json["compilerOptions"]["target"], "es2018");
f.close();
assertEquals(file.size, size);
assertEquals(file.type, "application/octet-stream");
// TODO checksum of tmp & sampleFile
} finally {
await Deno.remove(multipartFile);
await Deno.remove(sampleFile);
await form.removeAll();
o.close();
}
Expand Down

0 comments on commit 509dce0

Please sign in to comment.