|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const Fastify = require('fastify') |
| 5 | +const FormData = require('form-data') |
| 6 | +const http = require('http') |
| 7 | +const multipart = require('..') |
| 8 | +const { once } = require('events') |
| 9 | +const fs = require('fs') |
| 10 | +const path = require('path') |
| 11 | + |
| 12 | +const filePath = path.join(__dirname, '../README.md') |
| 13 | + |
| 14 | +test('show modify the generated schema', async function (t) { |
| 15 | + t.plan(4) |
| 16 | + |
| 17 | + const fastify = Fastify({ |
| 18 | + ajv: { |
| 19 | + plugins: [multipart.ajvFilePlugin] |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + t.teardown(fastify.close.bind(fastify)) |
| 24 | + |
| 25 | + await fastify.register(multipart, { attachFieldsToBody: true }) |
| 26 | + await fastify.register(require('@fastify/swagger'), { |
| 27 | + mode: 'dynamic', |
| 28 | + |
| 29 | + openapi: { |
| 30 | + openapi: '3.1.0' |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + await fastify.post( |
| 35 | + '/', |
| 36 | + { |
| 37 | + schema: { |
| 38 | + operationId: 'test', |
| 39 | + consumes: ['multipart/form-data'], |
| 40 | + body: { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + field: { isFile: true } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + }, |
| 48 | + async function (req, reply) { |
| 49 | + reply.code(200).send() |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + await fastify.ready() |
| 54 | + |
| 55 | + t.match(fastify.swagger(), { |
| 56 | + paths: { |
| 57 | + '/': { |
| 58 | + post: { |
| 59 | + operationId: 'test', |
| 60 | + requestBody: { |
| 61 | + content: { |
| 62 | + 'multipart/form-data': { |
| 63 | + schema: { |
| 64 | + type: 'object', |
| 65 | + properties: { |
| 66 | + field: { type: 'string', format: 'binary' } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + responses: { |
| 73 | + 200: { description: 'Default Response' } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + await fastify.listen({ port: 0 }) |
| 81 | + |
| 82 | + // request without file |
| 83 | + { |
| 84 | + const form = new FormData() |
| 85 | + const req = http.request({ |
| 86 | + protocol: 'http:', |
| 87 | + hostname: 'localhost', |
| 88 | + port: fastify.server.address().port, |
| 89 | + path: '/', |
| 90 | + headers: form.getHeaders(), |
| 91 | + method: 'POST' |
| 92 | + }) |
| 93 | + |
| 94 | + form.append('field', JSON.stringify({}), { contentType: 'application/json' }) |
| 95 | + form.pipe(req) |
| 96 | + |
| 97 | + const [res] = await once(req, 'response') |
| 98 | + res.resume() |
| 99 | + await once(res, 'end') |
| 100 | + t.equal(res.statusCode, 400) // body/field should be a file |
| 101 | + } |
| 102 | + |
| 103 | + // request with file |
| 104 | + { |
| 105 | + const form = new FormData() |
| 106 | + const req = http.request({ |
| 107 | + protocol: 'http:', |
| 108 | + hostname: 'localhost', |
| 109 | + port: fastify.server.address().port, |
| 110 | + path: '/', |
| 111 | + headers: form.getHeaders(), |
| 112 | + method: 'POST' |
| 113 | + }) |
| 114 | + |
| 115 | + form.append('field', fs.createReadStream(filePath), { contentType: 'multipart/form-data' }) |
| 116 | + form.pipe(req) |
| 117 | + |
| 118 | + const [res] = await once(req, 'response') |
| 119 | + res.resume() |
| 120 | + await once(res, 'end') |
| 121 | + t.equal(res.statusCode, 200) |
| 122 | + } |
| 123 | + t.pass('res ended successfully') |
| 124 | +}) |
0 commit comments