apickli / apickli

apickli - REST API integration testing framework based on cucumber.js
MIT License
207 stars 94 forks source link

Multipart/form-data not handled #65

Open bialesdaniel opened 8 years ago

bialesdaniel commented 8 years ago

Hi I tried using the pipe file to body but I don't think that does exactly what I need to do. The options for the request that I am trying to test would have to look something like:

{ method: 'POST',
  url: 'http://localhost:9000/Path',
  headers: 
   {authorization: 'Basic auth'}
  formData: 
   { file: 
      { value: 'fs.createReadStream(fileName)' }
   }
 }
seymen commented 8 years ago

@bialesdaniel - multipart form data is not supported at the moment. Care to PR?

bialesdaniel commented 8 years ago

Sure I can try!

ssunil007 commented 6 years ago

@bialesdaniel Any fixes for multipart form data?

I have tried storing the contents of the excel file as form value and calling the api....I get unsupported media type.........Inspite of setting content type header to multipart form data........while calling the api it is taking as applicaiton/json.

repsonse body for formdta feature-multformdata formdata-custom

zazoomauro commented 6 years ago

@bialesdaniel any update on this?

bialesdaniel commented 6 years ago

Sorry I lost track of this. I need to write tests for the code but Here is what I had (it was a long time ago so I can't remember if it was working or not)

Apickli.prototype.setFormData = function (formDataTable) {
  var self = this;
  var formDataObject = {};
  formDataTable.forEach(function (f) {
    var formDataName = self.replaceVariables(f.name);
    var formDataValue = self.replaceVariables(f.value);
    var formDataType = self.replaceVariables(f.type);
    if (formDataType.toLowerCase() === 'file') {
      formDataObject[formDataName] = fs.createReadStream(self.fixturesDirectory + formDataValue);
    } else {
      formDataObject[formDataName] = formDataValue;
    }
  });
  this.formData = formDataObject;
}

Then you just need this.formData ={} in the constructor and add formData:this.formData in the Apickli.prototype.post method. I'll see if I can get back to this in the next week or two.

jhnferraris commented 5 years ago

Is multipart/form-data now being handled?

sri-asb commented 5 years ago

@bialesdaniel is the above code has been tested?

bialesdaniel commented 5 years ago

@sri-asb Sorry I lost the code I was working on. I never created the PR with the code and tests but I was using the code above for a while and it worked

geertendoornenbal commented 5 years ago

I was able to create the following step definition we use now in our tests:

Given('I set file {string} as multipart form-data with mime-type {string}', function(fileName, mimeType, callback)
{
  this.apickli.addRequestHeader('Content-Type', 'multipart/form-data');
  if (fs.existsSync(fileName))
  {
    const formData = fs.readFileSync(fileName);
    this.apickli.httpRequestOptions.multipart = {
      chunked: false,
      data: [
        {
          body: formData, 'content-type': mimeType
        }]
    };
    callback();
  }
  else
  {
    callback(new Error(`File ${fileName} does not exist`));
  }
});

This works, because the options are just passed on to the creating of a request (https://github.com/request/request), and this has support for multipart form data.