diff --git a/src/index.ts b/src/index.ts index 3132091c..595306ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -150,7 +150,7 @@ export class OpenApiValidator { app: Application | Router, context: OpenApiContext, ): void { - app.use(middlewares.multipart(context, this.options.multerOpts)); + app.use(middlewares.multipart(context, this.options.fileUploader)); } private installSecurityMiddleware( diff --git a/test/multipart.spec.ts b/test/multipart.spec.ts index 65d58358..ec0b424e 100644 --- a/test/multipart.spec.ts +++ b/test/multipart.spec.ts @@ -7,24 +7,39 @@ import * as packageJson from '../package.json'; describe(packageJson.name, () => { let app = null; + const fileNames = []; before(async () => { const apiSpec = path.join('test', 'resources', 'multipart.yaml'); - app = await createApp({ apiSpec }, 3003, app => - app.use( - `${app.basePath}`, - express - .Router() - .post(`/sample_2`, (req, res) => { - const files = req.files; - res.status(200).json({ - files, - metadata: req.body.metadata, - }); - }) - .post(`/sample_1`, (req, res) => res.json(req.body)), - ), + app = await createApp( + { + apiSpec, + fileUploader: { + fileFilter: (req, file, cb) => { + fileNames.push(file.originalname); + cb(null, true); + }, + }, + }, + 3003, + app => + app.use( + `${app.basePath}`, + express + .Router() + .post(`/sample_2`, (req, res) => { + const files = req.files; + res.status(200).json({ + files, + metadata: req.body.metadata, + }); + }) + .post(`/sample_1`, (req, res) => res.json(req.body)), + ), ); }); + beforeEach(() => { + fileNames.length = 0; + }); after(() => { (app).server.close(); }); @@ -52,8 +67,8 @@ describe(packageJson.name, () => { .attach('file', 'package.json') .expect(400)); - it('should validate multipart file and metadata', async () => - request(app) + it('should validate multipart file and metadata', async () => { + await request(app) .post(`${app.basePath}/sample_2`) .set('Content-Type', 'multipart/form-data') .set('Accept', 'application/json') @@ -69,8 +84,9 @@ describe(packageJson.name, () => { .to.have.property('fieldname') .to.equal('file'); expect(b.metadata).to.equal('some-metadata'); - })); - + }); + expect(fileNames).to.deep.equal(['package.json']); + }); it('should throw 405 get method not allowed', async () => request(app) .get(`${app.basePath}/sample_2`)