Skip to content

Commit

Permalink
Merge pull request #221 from JacobLey/fileUploader
Browse files Browse the repository at this point in the history
Update deprecated multerOpts reference, update test
  • Loading branch information
cdimascio authored Jan 16, 2020
2 parents b7936e0 + d07c8de commit 3ec58bb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
52 changes: 34 additions & 18 deletions test/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
(<any>app).server.close();
});
Expand Down Expand Up @@ -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')
Expand All @@ -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`)
Expand Down

0 comments on commit 3ec58bb

Please sign in to comment.