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

Commit

Permalink
feat: add query parameter on form data request
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Krause authored and KnisterPeter committed May 7, 2018
1 parent 828a26f commit 4314952
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function createQuery(parameters: object): string {
.replace(/^&/, '?');
}

function filterFormData(args: any[], parameters?: FormDataParameter[]): any[] {
return args.filter((_, index) => {
return !parameters || parameters.every(param => param.parameter !== index);
});
}

function buildUrl(tmpl: string, args: any[], appendQuery: boolean): [string, number] {
const [url, queryOrBodyIndex] = createUrl(tmpl, args);
const query = createQuery(appendQuery && queryOrBodyIndex > -1 ? args[queryOrBodyIndex] : {});
Expand Down Expand Up @@ -86,8 +92,8 @@ function execute(instance: Instance, method: string, tmpl: string, args: any[],
}
return sendBody ? JSON.stringify(args[appendQuery ? queryOrBodyIndex + 1 : queryOrBodyIndex]) : undefined;
};

const createUrlResult = buildUrl(tmpl, args, appendQuery && !parameters);
const urlParams = filterFormData(args, parameters);
const createUrlResult = buildUrl(tmpl, urlParams, appendQuery);
const url = createUrlResult[0];
const queryOrBodyIndex = createUrlResult[1];
const headers = prepareHeaders(instance);
Expand Down
18 changes: 18 additions & 0 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Test {
post(_body: any): Promise<any>;
postWithQueryAndBody(_query: any, _body: any): Promise<any>;
postWithFormData(_formData: any): Promise<any>;
postWithFormDataAndQuery(_query: any, _formData: any): Promise<any>;
put(): Promise<any>;
putWithQuery(_parameters: any): Promise<any>;
delete(_id: string): Promise<any>;
Expand All @@ -37,6 +38,8 @@ class TestImpl implements Test {
public postWithQueryAndBody(): any { /* */ }
@Post('/path/withFormData', true)
public postWithFormData(@FormData('name') _formData: any): any { /* */ }
@Post('/path/withFormData', true)
public postWithFormDataAndQuery(_query: any, @FormData('name') _formData: any): any { /* */ }
@Put('/path')
public put(): any { /* */ }
@Put('/path', true)
Expand Down Expand Up @@ -152,6 +155,21 @@ 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/
}
})
.post('/path/withFormData?query=params', /Content-Disposition: form-data; name="name"/)
.reply(200, mockResponse);
return test.postWithFormDataAndQuery({query: 'params'}, new Buffer(10).toString('UTF-8') )
.then(response => {
t.deepEqual(response, mockResponse);
});
});

test('Pretend should call a put method', t => {
const test: Test = Pretend.builder().target(TestImpl, 'http://host:port');
nock('http://host:port/').put('/path').reply(200, mockResponse);
Expand Down

0 comments on commit 4314952

Please sign in to comment.