Hi,
I'd like to make some stress test on my nodejs server with post requests that send files on the server.
Here is the code I'm testing with:
config:
target: 'https://websi.te'
phases:
- duration: 10
arrivalRate: 10
scenarios:
- flow:
- get:
url: "/"
- post:
url: "/upload/"
formData:
photos: '@./file.jpg'
token: 'myToken'
The error I have on my server is the following:
(node:27684) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 100): TypeError: Cannot read property 'token' of undefined
I have also an error 500 if I remove the token and its requirement from the server.
Note that this NodeJs Request code is working:
var options = { method: 'POST',
url: 'http://websi.te/upload',
headers:
{ 'postman-token': '4997a0b0-71c8-61cf-c434-2b458dd4320c',
'cache-control': 'no-cache',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWwktrZu0gW' },
formData:
{ photos:
{ value: 'fs.createReadStream("file.jpg")',
options: { filename: 'file.jpg', contentType: null } },
token: 'myToken' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Thanks for your help.
Artillery does not support the @./file.jpg syntax for reading a file in. That literal string will be sent to the server which is what's probably causing problems.
Thanks for your fast reply. But is there any way to test file uploads in post requests?
Yep - you can embed the contents of the file directly into the script or use a beforeRequest hook function to read the file in and attach the data to the request.
We are planning to add proper support for file uploads (using the @filename syntax most likely like in your script), but it's not there yet.
Can you point me to an example please?
Thanks.
I had the same problem and finally made it working. Here's the example:
Config file:
config:
target: 'http://localhost:8080'
phases:
- duration: 1
arrivalRate: 1
processor: './processor-utils.js'
scenarios:
- flow:
- post:
url: '/upload/ocr'
beforeRequest: 'setJSONBody'
afterResponse: 'logResponse'
formData:
email: '[email protected]'
processor-utils.js
const fs = require('fs');
module.exports = {
setJSONBody,
logResponse,
}
function setJSONBody(requestParams, context, ee, next) {
const formData = {
fileOCR: fs.createReadStream(__dirname + '/files/ocr.png'),
};
requestParams.formData = Object.assign({}, requestParams.formData, formData);
return next();
}
function logResponse(requestParams, response, context, ee, next) {
console.log('[DEBUG] Response: ', response.body);
return next();
}
Most helpful comment
I had the same problem and finally made it working. Here's the example:
Config file:
processor-utils.js