Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update deprecated multerOpts reference, update test #221

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the best pattern here is (e.g. should fileNames really be unchanged on all tests, and cleared before each? Or maybe it somehow only exists in this test?)
The "filter" function just seemed like an easy way to check that multer is actually receiving the options + file

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine for now. i don't have better idea atm. we can improve the test later if needed

});
it('should throw 405 get method not allowed', async () =>
request(app)
.get(`${app.basePath}/sample_2`)
Expand Down