Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
fix: add the ability to pass an array to a formData parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosijava authored and KnisterPeter committed Mar 28, 2023
1 parent 2d55e11 commit af37fd0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ test('Pretend should call a post method with FormData', async () => {
expect(response).toEqual(mockResponse);
});

test('Pretend should call a post method with Array of FormData', async () => {
const test = setup();
nock('http://host:port/', {
reqheaders: {
'Content-Type': /^multipart\/form-data/
}
})
.post('/path/withFormData', /Content-Disposition: form-data; name="name"/)
.reply(200, mockResponse);

const response = await test.postWithFormData([
Buffer.alloc(10).toString('utf-8'),
Buffer.alloc(10).toString('utf-8')
]);

expect(response).toEqual(mockResponse);
});

test('Pretend should call a post method with FormData and query', async () => {
const test = setup();
nock('http://host:port/', {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ function execute(
.forEach((parameter: FormDataParameter) => {
const value = args[parameter.parameter];
if (value) {
formData.append(parameter.name, value, value.name);
if (Array.isArray(value)) {
value.forEach(val => {
formData.append(parameter.name, val, val.name);
});
} else {
formData.append(parameter.name, value, value.name);
}
}
});
return formData;
Expand Down

0 comments on commit af37fd0

Please sign in to comment.