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

Commit

Permalink
fix: allow empty form-data requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandra Kirchrath authored and KnisterPeter committed May 24, 2018
1 parent b9f0f36 commit 323ed3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ function execute(instance: Instance, method: string, tmpl: string, args: any[],
const formData = new FormData();
parameters.forEach(parameter => {
const value = args[parameter.parameter];
formData.append(parameter.name, value, value.name);
if (value) {
formData.append(parameter.name, value, value.name);
}
});
return formData;
}
Expand Down
28 changes: 23 additions & 5 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Test {
postWithQueryAndBody(_query: any, _body: any): Promise<any>;
postWithFormData(_formData: any): Promise<any>;
postWithFormDataAndQuery(_query: any, _formData: any): Promise<any>;
postWithEmptyFormDataAndQuery(_query: any, _formData: any): Promise<any>;
put(): Promise<any>;
putWithQuery(_parameters: any): Promise<any>;
delete(_id: string): Promise<any>;
Expand All @@ -41,6 +42,8 @@ class TestImpl implements Test {
public postWithFormData(@FormData('name') _formData: any): any { /* */ }
@Post('/path/withFormData', true)
public postWithFormDataAndQuery(_query: any, @FormData('name') _formData: any): any { /* */ }
@Post('/path/withFormData', true)
public postWithEmptyFormDataAndQuery(_query: any, @FormData('name') _formData: any): any { /* */ }
@Put('/path')
public put(): any { /* */ }
@Put('/path', true)
Expand Down Expand Up @@ -159,13 +162,28 @@ test('Pretend should call a post method with FormData', t => {
test('Pretend should call a post method with FormData and query', t => {
const test = setup();
nock('http://host:port/', {
reqheaders: {
'Content-Type': /^multipart\/form-data/
}
})
reqheaders: {
'Content-Type': /^multipart\/form-data/
}
})
.post('/path/withFormData?query=params', /Content-Disposition: form-data; name="name"/)
.reply(200, mockResponse);
return test.postWithFormDataAndQuery({query: 'params'}, Buffer.alloc(10).toString('UTF-8') )
return test.postWithFormDataAndQuery({ query: 'params' }, Buffer.alloc(10).toString('UTF-8'))
.then(response => {
t.deepEqual(response, mockResponse);
});
});

test('Pretend should call a post method with empty FormData and query', t => {
const test = setup();
nock('http://host:port/', {
reqheaders: {
'Content-Type': /^multipart\/form-data/
}
})
.post('/path/withFormData?query=params', undefined)
.reply(200, mockResponse);
return test.postWithEmptyFormDataAndQuery({ query: 'params' }, undefined)
.then(response => {
t.deepEqual(response, mockResponse);
});
Expand Down

0 comments on commit 323ed3a

Please sign in to comment.