diff --git a/lib/RestClient.ts b/lib/RestClient.ts index f846d85..ef67038 100644 --- a/lib/RestClient.ts +++ b/lib/RestClient.ts @@ -104,7 +104,7 @@ export class RestClient { resources: any, options?: IRequestOptions): Promise> { - let url: string = util.getUrl(resource, this._baseUrl); + let url: string = util.getUrl(resource, this._baseUrl, (options || {}).queryParameters); let headers: ifm.IHeaders = this._headersFromOptions(options, true); let data: string = JSON.stringify(resources, null, 2); @@ -123,7 +123,7 @@ export class RestClient { resources: any, options?: IRequestOptions): Promise> { - let url: string = util.getUrl(resource, this._baseUrl); + let url: string = util.getUrl(resource, this._baseUrl, (options || {}).queryParameters); let headers: ifm.IHeaders = this._headersFromOptions(options, true); let data: string = JSON.stringify(resources, null, 2); @@ -142,7 +142,7 @@ export class RestClient { resources: any, options?: IRequestOptions): Promise> { - let url: string = util.getUrl(resource, this._baseUrl); + let url: string = util.getUrl(resource, this._baseUrl, (options || {}).queryParameters); let headers: ifm.IHeaders = this._headersFromOptions(options, true); let data: string = JSON.stringify(resources, null, 2); diff --git a/test/tests/resttests.ts b/test/tests/resttests.ts index 067caea..66a00f0 100644 --- a/test/tests/resttests.ts +++ b/test/tests/resttests.ts @@ -101,6 +101,36 @@ describe('Rest Tests', function () { assert(restRes.result && restRes.result.json.name === 'foo'); }); + it('creates a resource passing Query Parameters', async () => { + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _rest.create('https://httpbin.org/post', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result.url === 'https://httpbin.org/post?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + + it('creates a resource with baseUrl passing Query Parameters', async () => { + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _restBin.create('post', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result && restRes.result.url === 'https://httpbin.org/post?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + it('replaces a resource', async() => { this.timeout(3000); @@ -119,6 +149,40 @@ describe('Rest Tests', function () { assert(restRes.result && restRes.result.json.name === 'foo'); }); + it('puts a resource passing Query Parameters', async () => { + this.timeout(3000); + + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _rest.replace('https://httpbin.org/put', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result && restRes.result.url === 'https://httpbin.org/put?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + + it('puts a resource with baseUrl passing Query Parameters', async () => { + this.timeout(3000); + + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _restBin.replace('put', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result && restRes.result.url === 'https://httpbin.org/put?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + it('updates a resource', async() => { let res: any = { name: 'foo' }; let restRes: restm.IRestResponse = await _rest.update('https://httpbin.org/patch', res); @@ -135,6 +199,36 @@ describe('Rest Tests', function () { assert(restRes.result && restRes.result.json.name === 'foo'); }); + it('updates a resource passing Query Parameters', async () => { + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _rest.update('https://httpbin.org/patch', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result && restRes.result.url === 'https://httpbin.org/patch?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + + it('updates a resource with baseUrl passing Query Parameters', async () => { + let res: any = { name: 'foo' }; + let restRes: restm.IRestResponse = await _restBin.update('patch', res, _options); + assert(restRes.statusCode == 200, "statusCode should be 200"); + assert(restRes.result && restRes.result.url === 'https://httpbin.org/patch?id=1&type=compact'); + assert(restRes.result && restRes.result.json.name === 'foo'); + + Object.keys(_options.queryParameters.params).forEach(key => { + const actual = restRes.result.args[key]; + const expected = _options.queryParameters.params[key]; + + assert(expected == actual); + }) + }); + it('deletes a resource', async() => { let restRes: restm.IRestResponse = await _rest.del('https://httpbin.org/delete'); assert(restRes.statusCode == 200, "statusCode should be 200"); @@ -221,7 +315,6 @@ describe('Rest Tests', function () { catch(err) { assert(err['statusCode'] == 401, "statusCode should be 401"); assert(err.message && err.message.length > 0, "should have error message"); - assert(err['responseHeaders'], "err must contain responseHeaders"); } }); @@ -238,7 +331,6 @@ describe('Rest Tests', function () { catch(err) { assert(err['statusCode'] == 500, "statusCode should be 500"); assert(err.message && err.message.length > 0, "should have error message"); - assert(err['responseHeaders'], "err must contain responseHeaders"); } });