-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from asynch8/allow-multiple-files
Updated codebase with functionality and tests to support multiple files being uploaded in the same form variable
- Loading branch information
Showing
4 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use strict"; | ||
|
||
const setup = require("./setup-multifile"); | ||
const tap = require("tap"); | ||
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)); | ||
form.append("files", fs.createReadStream(filePath)); | ||
return form.pipe(req); | ||
}; | ||
|
||
tap.test("should allow multiple files in one field", async t => { | ||
const instance = require("fastify").fastify(); | ||
t.teardown(async () => { | ||
await instance.close(); | ||
}); | ||
try { | ||
instance.addHook("onResponse", async (request, reply) => { | ||
const requestBody = request.body; | ||
t.isArray(requestBody.files); | ||
t.ok(requestBody.files[0].stream instanceof Readable); | ||
t.ok(requestBody.files[1].stream instanceof Readable); | ||
t.equal(reply.statusCode, 200); | ||
}); | ||
await setup(instance, undefined, false); | ||
const req = await sendRequest(instance); | ||
const [res] = await once(req, "response"); | ||
res.resume(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const formDataParser = require("../index"); | ||
|
||
const requestSchema = { | ||
consumes: ["multipart/form-data"], | ||
body: { | ||
type: "object", | ||
properties: { | ||
file: { | ||
type: "string", | ||
format: "binary" | ||
}, | ||
files: { | ||
type: "array", | ||
items: { | ||
type: "string", | ||
format: "binary" | ||
} | ||
}, | ||
} | ||
} | ||
}; | ||
module.exports = async function (instance, options = undefined, includeSchema = true) { | ||
instance.register(formDataParser, options); | ||
instance.post( | ||
"/", | ||
{ | ||
schema: includeSchema && requestSchema | ||
}, | ||
async (request, reply) => { | ||
reply.status(200).send(); | ||
} | ||
); | ||
await instance.listen({ port: 0, host: "::" }); | ||
}; |