Skip to content

Commit

Permalink
fix(request-executer): remove body from GET/DELETE requests (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
marlosin authored and KutsenkoA committed May 28, 2019
1 parent c9832a4 commit ea8b510
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
69 changes: 48 additions & 21 deletions src/internal/executer.spec.ts
Expand Up @@ -168,14 +168,14 @@ describe("QueryExecuter class", () => {
});
});

describe("on calling getMethod()", () => {
const actions = [
{ action: QueryAction.insert, expected: Methods.POST },
{ action: QueryAction.delete, expected: Methods.DELETE },
{ action: QueryAction.select, expected: Methods.GET },
{ action: QueryAction.update, expected: Methods.PATCH },
];
const actions = [
{ action: QueryAction.insert, expected: Methods.POST, hasBody: true },
{ action: QueryAction.delete, expected: Methods.DELETE, hasBody: false },
{ action: QueryAction.select, expected: Methods.GET, hasBody: false },
{ action: QueryAction.update, expected: Methods.PATCH, hasBody: true },
];

describe("on calling getMethod()", () => {
actions.forEach(({ action, expected }) => {
it(`should return ${expected} for ${action}`, async () => {
const { subject } = createSubject();
Expand All @@ -194,27 +194,54 @@ describe("QueryExecuter class", () => {
restUrl,
{
headers: { Authorization: `Bearer ${validToken}` },
body: mockRequest.body,
method: subject.getMethod(QUERY_ACTION),
},
);
});

it("should pass the proper options down to the request adapter", async () => {
const { subject, reqAdapterMock } = createSubject();
const fakeBody = {
someObject: faker.random.objectElement(),
};
await subject.executeRequest({
action: QUERY_ACTION,
body: fakeBody,
const withoutBody = actions.filter((action) => !action.hasBody);

withoutBody.forEach(({ action, expected }) => {
it("should pass no body for GET requests", async () => {
const { subject, reqAdapterMock } = createSubject();

await subject.executeRequest({
action,
body: {},
});

expect(reqAdapterMock.execute).toHaveBeenCalledWith(
restUrl,
{
headers: { Authorization: `Bearer ${validToken}` },
method: expected,
},
);
});
expect(reqAdapterMock.execute).toHaveBeenCalledWith(
restUrl,
expect.objectContaining({
});

const withBody = actions.filter((action) => action.hasBody);

withBody.forEach(({ action, expected }) => {
it("should pass the proper options down to the request adapter", async () => {
const { subject, reqAdapterMock } = createSubject();
const fakeBody = {
someObject: faker.random.number(),
};
await subject.executeRequest({
action,
body: fakeBody,
}),
);
});

expect(reqAdapterMock.execute).toHaveBeenCalledWith(
restUrl,
{
headers: { Authorization: `Bearer ${validToken}` },
body: fakeBody,
method: expected,
}
);
});
});

it("should wait the system initialization before execute the query", async (done) => {
Expand Down
35 changes: 25 additions & 10 deletions src/internal/executer.ts
Expand Up @@ -5,7 +5,7 @@ import { DataSetName } from "../api/dataops/dataops.tokens";
import { QueryAction } from "../api/dataops/queries/baseQuery";
import { API } from "../config";
import { IRequestExecuterData, QueryParam } from "./executer.interfaces";
import { Methods, RequestAdapter } from "./requestAdapter";
import { IRequestOptions, Methods, RequestAdapter } from "./requestAdapter";

@Injectable()
export class RequestExecuter {
Expand All @@ -19,15 +19,11 @@ export class RequestExecuter {

public async executeRequest(request: IRequestExecuterData): Promise<any> {
await this.systemInit;
const token = await this.tokenManager.token(this.config.auth);
return this.requestAdapter.execute(
this.getURI(request.queryParams),
{
headers: { Authorization: `Bearer ${token}` },
body: request.body,
method: this.getMethod(request.action)
},
);

const URI = this.getURI(request.queryParams);
const options = await this.getRequestOptions(request);

return this.requestAdapter.execute(URI, options);
}

public getMethod(action: QueryAction): Methods {
Expand All @@ -40,6 +36,25 @@ export class RequestExecuter {
}
}

private async getRequestOptions(request: IRequestExecuterData): Promise<IRequestOptions> {
const token = await this.tokenManager.token(this.config.auth);

const options: IRequestOptions = {
headers: { Authorization: `Bearer ${token}` },
method: this.getMethod(request.action)
};

if (this.hasBody(request)) {
options.body = request.body;
}

return options;
}

private hasBody(request: IRequestExecuterData): boolean {
return ![Methods.GET, Methods.DELETE].includes(this.getMethod(request.action));
}

/**
* Gets the URL concatenated with query params, when available.
*
Expand Down

0 comments on commit ea8b510

Please sign in to comment.