From 880ecd90963885ba707892f4b91f6ee498c56931 Mon Sep 17 00:00:00 2001 From: FlameWolf Date: Wed, 24 Apr 2024 14:31:06 +0530 Subject: [PATCH] Refactor tests; update package version to `3.2.10` --- package-lock.json | 4 ++-- package.json | 2 +- test/buffer-storage.js | 5 +---- test/callback-storage.js | 5 +---- test/disc-storage.js | 9 ++------- test/multifile.js | 29 ++--------------------------- test/no-schema.js | 4 +--- test/setup-multifile.js | 20 ++++++++++++++++---- test/setup.js | 22 ++++++++++------------ test/stream-storage.js | 5 +---- 10 files changed, 37 insertions(+), 68 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0d9ff16..28deb69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "formzilla", - "version": "3.2.9", + "version": "3.2.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "formzilla", - "version": "3.2.9", + "version": "3.2.10", "license": "ISC", "dependencies": { "busboy": "^1.6.0", diff --git a/package.json b/package.json index d2debe5..ed4e4a8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/buffer-storage.js b/test/buffer-storage.js index d98f0f3..1e70d3c 100644 --- a/test/buffer-storage.js +++ b/test/buffer-storage.js @@ -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(); @@ -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); } diff --git a/test/callback-storage.js b/test/callback-storage.js index 7c8ef73..405ca97 100644 --- a/test/callback-storage.js +++ b/test/callback-storage.js @@ -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(); @@ -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 = []; @@ -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); } diff --git a/test/disc-storage.js b/test/disc-storage.js index cb59dbe..d1e5195 100644 --- a/test/disc-storage.js +++ b/test/disc-storage.js @@ -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(); @@ -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); } @@ -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); } diff --git a/test/multifile.js b/test/multifile.js index 94d04ef..872a8d4 100644 --- a/test/multifile.js +++ b/test/multifile.js @@ -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(); @@ -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); } diff --git a/test/no-schema.js b/test/no-schema.js index 317fc66..7e9b83c 100644 --- a/test/no-schema.js +++ b/test/no-schema.js @@ -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); } diff --git a/test/setup-multifile.js b/test/setup-multifile.js index 72d0298..67cc27b 100644 --- a/test/setup-multifile.js +++ b/test/setup-multifile.js @@ -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: { @@ -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 + }); }; \ No newline at end of file diff --git a/test/setup.js b/test/setup.js index b06caf0..931d331 100644 --- a/test/setup.js +++ b/test/setup.js @@ -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"); @@ -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", @@ -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 + }); }; \ No newline at end of file diff --git a/test/stream-storage.js b/test/stream-storage.js index 62aca6f..99d00b1 100644 --- a/test/stream-storage.js +++ b/test/stream-storage.js @@ -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(); @@ -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); }