1

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'

user_id
  • 37
  • 1
  • 8

2 Answers2

1

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'
sandrozbinden
  • 1,577
  • 1
  • 17
  • 28
0

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"
strymsg
  • 11
  • 1
  • 3