I have an app built on Express.js and I\'d like to test the file upload functionality. I\'m trying to reproduce the object parsed to req.files (when using express.bodyParser
If using request promise you can simply do this, sending formData is built into it:
var options = {
method: 'POST',
uri: base + '/api/image/upload',
formData: {
someField: 'someValue',
anotherField: 'anotherValue',
file: {
// path to image
value: fs.createReadStream('./testImg.png'),
options: {
filename: 'testImg.png',
contentType: 'image/png'
}
}
}
};
let resp;
try {
resp = await request.post(options);
body = JSON.parse(resp);
} catch (e) {
console.log(e);
resp = e.response;
code = resp.statusCode;
body = JSON.parse(resp.body);
}
// access what is returned here, check what needs to be
assert.equal(code, 200, 'Status: ' + code + ': ' + body.error);
assert.equal(body.success, true);
assert.exists(body.image);