Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/RestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class RestClient {
resources: any,
options?: IRequestOptions): Promise<IRestResponse<T>> {

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);
Expand All @@ -123,7 +123,7 @@ export class RestClient {
resources: any,
options?: IRequestOptions): Promise<IRestResponse<T>> {

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);
Expand All @@ -142,7 +142,7 @@ export class RestClient {
resources: any,
options?: IRequestOptions): Promise<IRestResponse<T>> {

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);
Expand Down
96 changes: 94 additions & 2 deletions test/tests/resttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpBinData> = await _rest.create<HttpBinData>('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<HttpBinData> = await _restBin.create<HttpBinData>('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);

Expand All @@ -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<HttpBinData> = await _rest.replace<HttpBinData>('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<HttpBinData> = await _restBin.replace<HttpBinData>('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<HttpBinData> = await _rest.update<HttpBinData>('https://httpbin.org/patch', res);
Expand All @@ -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<HttpBinData> = await _rest.update<HttpBinData>('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<HttpBinData> = await _restBin.update<HttpBinData>('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<HttpBinData> = await _rest.del<HttpBinData>('https://httpbin.org/delete');
assert(restRes.statusCode == 200, "statusCode should be 200");
Expand Down Expand Up @@ -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");
}
});

Expand All @@ -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");
}
});

Expand Down