Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix: prepend origin if missing in http request uri (#44)
Browse files Browse the repository at this point in the history
* fix: prepend origin if missing in http request uri

Signed-off-by: Deyan Valkanov <dvaklanov@vmware.com>
Signed-off-by: Deyan Valkanov <dvalkanov@vmware.com>

* Update changelog and version

Signed-off-by: Deyan Valkanov <dvalkanov@vmware.com>

* Add unit tests

Signed-off-by: Deyan Valkanov <dvalkanov@vmware.com>
  • Loading branch information
valkanov authored May 25, 2021
1 parent a0becaa commit b9374d6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
9 changes: 9 additions & 0 deletions build/npm/transport.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
"license": "BSD-2-Clause",
"changelogHistory": [
{
"date": "5/14/21",
"version": "1.2.9",
"notes": [
{
"description": "Fix missing protocol and host in httpclient uri",
"review_uri": "https://github.com/vmware/transport-typescript/pull/44"
}
]
},{
"date": "5/10/21",
"version": "1.2.8",
"notes": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vmw/transport",
"version": "1.2.8",
"version": "1.2.9",
"private": false,
"license": "BSD-2-Clause",
"repository": "git@github.com:vmware/transport-typescript.git",
Expand Down
2 changes: 1 addition & 1 deletion src/bus.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { FabricApi } from './fabric.api';
import { BrokerConnector } from './bridge';

// current version
const version = '1.2.8';
const version = '1.2.9';

export type ChannelName = string;
export type SentFrom = string;
Expand Down
49 changes: 49 additions & 0 deletions src/core/services/rest/rest.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,55 @@ describe('Fake Service [services/rest/rest.service.spec]', () => {
}
);

it('Should simulate an autogenerated API call when no base url is provided',
() => {
let channel = bus.api.getResponseChannel(FakeChannel.request);
const id = GeneralUtil.genUUIDShort();
const packet: any = {};
Object.assign(packet, restPayloadGet);

expect(channel)
.not
.toBeUndefined();
channel.subscribe(
(message: Message) => {
const responseObject = message.payload as FakeRestRelayResponseObject;
const payload = responseObject.payload as string;
expect(payload).toBe('GET called');
}
);

packet['op'] = 'GET';
packet['uri'] = '/foo';
const requestObject = new FakeRestRelayRequestObject(FakeChannel.request, packet);
bus.sendRequestMessageWithId(FakeChannel.request, requestObject, id);
}
);

it('Should fail on a bad url passed to the Rest service (negative test).',
() => {
let channel = bus.api.getResponseChannel(FakeChannel.request);
const id = GeneralUtil.genUUIDShort();
const packet: any = {};
Object.assign(packet, restPayloadGet);

expect(channel)
.not
.toBeUndefined();
channel.subscribe(
(message: Message) => {
expect(message.id).toEqual(id);
expect(message.isError()).toBeTruthy();
}
);

packet['op'] = 'GET';
packet['uri'] = 'https://';
const requestObject = new FakeRestRelayRequestObject(FakeChannel.request, packet);
bus.sendRequestMessageWithId(FakeChannel.request, requestObject, id);
}
);

it('Should update headers.',
() => {
let channel = bus.api.getResponseChannel(FakeChannel.request);
Expand Down
27 changes: 20 additions & 7 deletions src/core/services/rest/rest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,41 @@ export class RestService extends AbstractCore implements EventBusEnabled, Fabric
const requestHeadersObject = new Headers(requestHeaders);
const requestInit: RequestInit = this.generateRequestInitObject(restObject, requestHeadersObject);

const uri: string = this.globalBaseUri + restObject.uri;
this.log.debug(`Rest Service: Preparing Fetch Request for URI: ${uri}`, this.getName());
let httpRequest;

// try to create fetch request.
try {
const uri: string = this.globalBaseUri + restObject.uri;
this.log.debug(`Rest Service: Preparing Fetch Request for URI: ${uri}`, this.getName());
httpRequest = new Request(uri);
} catch (e) {
this.log.error(`Rest Service: Cannot create request: ${e}`, this.getName());
this.handleError(
new RestError('Invalid HTTP request.', RestErrorType.UnknownMethod, restObject.uri),
restObject,
args);
return;
}

switch (restObject.request) {
case HttpRequest.Get:
this.httpClient.get(uri, requestInit, successHandler, errorHandler);
this.httpClient.get(httpRequest.url, requestInit, successHandler, errorHandler);
break;

case HttpRequest.Post:
this.httpClient.post(uri, requestInit, successHandler, errorHandler);
this.httpClient.post(httpRequest.url, requestInit, successHandler, errorHandler);
break;

case HttpRequest.Patch:
this.httpClient.patch(uri, requestInit, successHandler, errorHandler);
this.httpClient.patch(httpRequest.url, requestInit, successHandler, errorHandler);
break;

case HttpRequest.Put:
this.httpClient.put(uri, requestInit, successHandler, errorHandler);
this.httpClient.put(httpRequest.url, requestInit, successHandler, errorHandler);
break;

case HttpRequest.Delete:
this.httpClient.delete(uri, requestInit, successHandler, errorHandler);
this.httpClient.delete(httpRequest.url, requestInit, successHandler, errorHandler);
break;

default:
Expand Down

0 comments on commit b9374d6

Please sign in to comment.