Is there a way to upload files with Artillery?

1.6k Views Asked by At

Is there a good approach to test upload files (form-data) with artillery? The http-file-uploads plugin inly works with Artillery Pro. I tried the solution given in this thread https://github.com/artilleryio/artillery/issues/320 writing a beforeRequest Js method, but no success.

  const formData = {
    fileOCR: fs.createReadStream(__dirname + '/files/ocr.png'),
  };

  requestParams.formData = Object.assign({}, requestParams.formData, formData);

  return next();
}

My field in form-data for the file is called 'file'

2

There are 2 best solutions below

0
On

Based on the previous answer, this worked for me:

// processor.js
const formData = require('form-data');

function setupMultipartFormData(requestParams, context, ee, next) {
  const form = new formData();
  form.append('file', fs.createReadStream('/path/to/file'));
  requestParams.body = form;
  return next();
}

module.exports = {
  setupMultipartFormData,
}

then the yaml.

config:
  target: "https://someapi"
  processor: "processor.js"

# ...
scenarios:
 - name: File Upload
    flow:
      - post:
          url: /fileUpload
          beforeRequest: "setupMultipartFormData"
0
On

According the artillery documentation to upload a file via multipart/form-data you have to create a custom function.

const fs = require('fs');
const FormData = require('form-data');

function addMultipartFormData(requestParams, context, ee, next) {
    const form = new FormData();
    form.append('files', fs.createReadStream(__dirname + '/resources/someFile.pdf'));
    requestParams.body = form;
    return next(); 
}

module.exports = {
  addMultipartFormData,
}

Then you can execute this function in the beforeRequest hook in your test definition file.

- post:
   url: "http://server/file/upload"
   beforeRequest: 'addMultipartFormData'