-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include
ajvFilePlugin
into source and follow OpenAPI convention. (#443
- Loading branch information
1 parent
0c4772a
commit 180c291
Showing
6 changed files
with
175 additions
and
27 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
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
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,13 @@ | ||
import fastify from 'fastify' | ||
import { fastifyMultipart, ajvFilePlugin } from '..' | ||
|
||
const app = fastify({ | ||
ajv: { | ||
plugins: [ | ||
ajvFilePlugin, | ||
(await import('..')).ajvFilePlugin | ||
] | ||
} | ||
}) | ||
|
||
app.register(fastifyMultipart) |
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,124 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const Fastify = require('fastify') | ||
const FormData = require('form-data') | ||
const http = require('http') | ||
const multipart = require('..') | ||
const { once } = require('events') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const filePath = path.join(__dirname, '../README.md') | ||
|
||
test('show modify the generated schema', async function (t) { | ||
t.plan(4) | ||
|
||
const fastify = Fastify({ | ||
ajv: { | ||
plugins: [multipart.ajvFilePlugin] | ||
} | ||
}) | ||
|
||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
await fastify.register(multipart, { attachFieldsToBody: true }) | ||
await fastify.register(require('@fastify/swagger'), { | ||
mode: 'dynamic', | ||
|
||
openapi: { | ||
openapi: '3.1.0' | ||
} | ||
}) | ||
|
||
await fastify.post( | ||
'/', | ||
{ | ||
schema: { | ||
operationId: 'test', | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
type: 'object', | ||
properties: { | ||
field: { isFile: true } | ||
} | ||
} | ||
} | ||
}, | ||
async function (req, reply) { | ||
reply.code(200).send() | ||
} | ||
) | ||
|
||
await fastify.ready() | ||
|
||
t.match(fastify.swagger(), { | ||
paths: { | ||
'/': { | ||
post: { | ||
operationId: 'test', | ||
requestBody: { | ||
content: { | ||
'multipart/form-data': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
field: { type: 'string', format: 'binary' } | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
200: { description: 'Default Response' } | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
|
||
// request without file | ||
{ | ||
const form = new FormData() | ||
const req = http.request({ | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
}) | ||
|
||
form.append('field', JSON.stringify({}), { contentType: 'application/json' }) | ||
form.pipe(req) | ||
|
||
const [res] = await once(req, 'response') | ||
res.resume() | ||
await once(res, 'end') | ||
t.equal(res.statusCode, 400) // body/field should be a file | ||
} | ||
|
||
// request with file | ||
{ | ||
const form = new FormData() | ||
const req = http.request({ | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
}) | ||
|
||
form.append('field', fs.createReadStream(filePath), { contentType: 'multipart/form-data' }) | ||
form.pipe(req) | ||
|
||
const [res] = await once(req, 'response') | ||
res.resume() | ||
await once(res, 'end') | ||
t.equal(res.statusCode, 200) | ||
} | ||
t.pass('res ended successfully') | ||
}) |