Skip to content

Commit

Permalink
Refactor tests; update package version to 3.2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameWolf committed Apr 24, 2024
1 parent 070af2e commit 880ecd9
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 68 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formzilla",
"version": "3.2.9",
"version": "3.2.10",
"description": "Fastify plugin for parsing multipart/form data",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 1 addition & 4 deletions test/buffer-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const setup = require("./setup");
const test = require("ava");
const { Buffer } = require("buffer");
const { BufferStorage } = require("../BufferStorage");
const { once } = require("events");

test("should store file as buffer and populate request body", async t => {
const instance = require("fastify").fastify();
Expand All @@ -20,9 +19,7 @@ test("should store file as buffer and populate request body", async t => {
t.is(typeof requestBody.address, "object");
t.is(reply.statusCode, 200);
});
const req = await setup(instance, { storage: new BufferStorage() });
const [res] = await once(req, "response");
res.resume();
await setup(instance, { storage: new BufferStorage() });
} catch (err) {
t.fail(err.message);
}
Expand Down
5 changes: 1 addition & 4 deletions test/callback-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const setup = require("./setup");
const test = require("ava");
const { CallbackStorage } = require("../CallbackStorage");
const { FileInternal } = require("../FileInternal");
const { once } = require("events");

test("should pass file stream to callback and populate request body", async t => {
const instance = require("fastify").fastify();
Expand All @@ -20,7 +19,7 @@ test("should pass file stream to callback and populate request body", async t =>
t.is(typeof requestBody.address, "object");
t.is(reply.statusCode, 200);
});
const req = await setup(instance, {
await setup(instance, {
storage: new CallbackStorage((name, stream, info) => {
const file = new FileInternal(name, info);
const data = [];
Expand All @@ -31,8 +30,6 @@ test("should pass file stream to callback and populate request body", async t =>
return file;
})
});
const [res] = await once(req, "response");
res.resume();
} catch (err) {
t.fail(err.message);
}
Expand Down
9 changes: 2 additions & 7 deletions test/disc-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const setup = require("./setup");
const test = require("ava");
const { DiscStorage } = require("../DiscStorage");
const { once } = require("events");

test("should save file to disc and populate request body", async t => {
const instance = require("fastify").fastify();
Expand All @@ -19,9 +18,7 @@ test("should save file to disc and populate request body", async t => {
t.is(typeof requestBody.address, "object");
t.is(reply.statusCode, 200);
});
const req = await setup(instance, { storage: new DiscStorage() });
const [res] = await once(req, "response");
res.resume();
await setup(instance, { storage: new DiscStorage() });
} catch (err) {
t.fail(err.message);
}
Expand All @@ -40,15 +37,13 @@ test("should read file save target from function", async t => {
t.is(typeof requestBody.address, "object");
t.is(reply.statusCode, 200);
});
const req = await setup(instance, {
await setup(instance, {
storage: new DiscStorage(file => {
return {
fileName: `${Date.now()}_${file.originalName}`
};
})
});
const [res] = await once(req, "response");
res.resume();
} catch (err) {
t.fail(err.message);
}
Expand Down
29 changes: 2 additions & 27 deletions test/multifile.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
"use strict";

const setup = require("./setup-multifile");
const setupMultifile = require("./setup-multifile");
const test = require("ava");
const { Readable } = require("stream");
const { once } = require("events");
const formData = require("form-data");
const http = require("http");
const path = require("path");
const fs = require("fs");

async function sendRequest(instance) {
const form = new formData();
const req = http.request({
protocol: "http:",
hostname: "localhost",
port: instance.server.address().port,
path: "/",
headers: form.getHeaders(),
method: "POST"
});
const filePath = path.join(__dirname, "chequer.png");
form.append("file", fs.createReadStream(filePath));
form.append("files", fs.createReadStream(filePath));
form.append("files", fs.createReadStream(filePath));
return form.pipe(req);
}

test("should allow multiple files in one field", async t => {
const instance = require("fastify").fastify();
Expand All @@ -39,10 +17,7 @@ test("should allow multiple files in one field", async t => {
t.true(requestBody.files[1].stream instanceof Readable);
t.is(reply.statusCode, 200);
});
await setup(instance, undefined, false);
const req = await sendRequest(instance);
const [res] = await once(req, "response");
res.resume();
await setupMultifile(instance);
} catch (err) {
t.fail(err.message);
}
Expand Down
4 changes: 1 addition & 3 deletions test/no-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ test("should parse fields as strings when there is no schema", async t => {
t.is(typeof requestBody.address, "string");
t.is(reply.statusCode, 200);
});
const req = await setup(instance, undefined, false);
const [res] = await once(req, "response");
res.resume();
await setup(instance, undefined, false);
} catch (err) {
t.fail(err.message);
}
Expand Down
20 changes: 16 additions & 4 deletions test/setup-multifile.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"use strict";

const formDataParser = require("../index");
const formData = require("form-data");
const path = require("path");
const fs = require("fs");

const requestSchema = {
consumes: ["multipart/form-data"],
body: {
type: "object",
properties: {
file: {
type: "string",
format: "binary"
},
files: {
type: "array",
items: {
Expand All @@ -33,4 +32,17 @@ module.exports = async function (instance, options = undefined, includeSchema =
}
);
await instance.listen({ port: 0, host: "::" });
const form = new formData();
const stream = fs.createReadStream(path.join(__dirname, "chequer.png"));
form.append("files", stream);
form.append("files", stream);
return await instance.inject({
protocol: "http:",
hostname: "localhost",
port: instance.server.address().port,
path: "/",
headers: form.getHeaders(),
method: "POST",
payload: form
});
};
22 changes: 10 additions & 12 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const formDataParser = require("../index");
const formData = require("form-data");
const http = require("http");
const path = require("path");
const fs = require("fs");

Expand Down Expand Up @@ -45,17 +44,8 @@ module.exports = async function (instance, options = undefined, includeSchema =
);
await instance.listen({ port: 0, host: "::" });
const form = new formData();
const req = http.request({
protocol: "http:",
hostname: "localhost",
port: instance.server.address().port,
path: "/",
headers: form.getHeaders(),
method: "POST"
});
const filePath = path.join(__dirname, "chequer.png");
form.append("name", "Jane Doe");
form.append("avatar", fs.createReadStream(filePath));
form.append("avatar", fs.createReadStream(path.join(__dirname, "chequer.png")));
form.append("age", 31);
form.append(
"address",
Expand All @@ -64,5 +54,13 @@ module.exports = async function (instance, options = undefined, includeSchema =
street: "First Street"
})
);
return form.pipe(req);
return await instance.inject({
protocol: "http:",
hostname: "localhost",
port: instance.server.address().port,
path: "/",
headers: form.getHeaders(),
method: "POST",
payload: form
});
};
5 changes: 1 addition & 4 deletions test/stream-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const setup = require("./setup");
const test = require("ava");
const { Readable } = require("stream");
const { once } = require("events");

test("should store file as stream and populate request body", async t => {
const instance = require("fastify").fastify();
Expand All @@ -19,9 +18,7 @@ test("should store file as stream and populate request body", async t => {
t.is(typeof requestBody.address, "object");
t.is(reply.statusCode, 200);
});
const req = await setup(instance);
const [res] = await once(req, "response");
res.resume();
await setup(instance);
} catch (err) {
t.fail(err.message);
}
Expand Down

0 comments on commit 880ecd9

Please sign in to comment.