Skip to content

Commit

Permalink
feat: add command for changing response time (#46)
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
antoinechalifour authored Aug 25, 2019
1 parent 03174e2 commit 63bcbd6
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RefreshRequest,
ListRequest,
GetRequestDetails,
SetResponseTime,
} from './domain/usecase';
import { getRequestDirectory } from './utils/path';

Expand All @@ -37,6 +38,9 @@ export function createCli({ container }: CreateCliOptions) {
const getRequestDetailsUseCase = container.resolve<GetRequestDetails>(
'getRequestDetailsUseCase'
);
const setResponseTimeUseCase = container.resolve<SetResponseTime>(
'setResponseTimeUseCase'
);
const appVersion = container.resolve<string>('appVersion');

const requestsAutocomplete = {
Expand Down Expand Up @@ -177,6 +181,22 @@ export function createCli({ container }: CreateCliOptions) {
}
});

vorpal
.command(
'set response-time <requestId> <responseTimeInMs>',
'Sets the response time for the provided request id'
)
.action(async function(
this: Vorpal.CommandInstance,
{ requestId, responseTimeInMs }
) {
this.log(
chalk`Setting response time to {yellow ${responseTimeInMs}ms} for request {yellow ${requestId}}...`
);
setResponseTimeUseCase.execute(requestId, responseTimeInMs);
this.log('Done.');
});

console.log(chalk`{green
__ __ _______ __ __ _______ __ _ _______ _______
| |_| || || |_| || || | | || || |
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entity/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class Response {
public readonly status: number,
public readonly headers: Headers,
public readonly body: Buffer,
public readonly responseTimeInMs: number
public responseTimeInMs: number
) {
this.headers = this.buildHeaders(headers);
}
Expand Down
91 changes: 91 additions & 0 deletions src/domain/usecase/SetResponseTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { getTestRequestRepository } from '../../test-utils/infrastructure';
import { RequestRepository } from '../repository';
import { Request, Response } from '../entity';
import { SetResponseTime } from './SetResponseTime';

let requestRepository: RequestRepository;

beforeEach(() => {
requestRepository = getTestRequestRepository();
});

describe('when the request is not found', () => {
it('should throw an error', async () => {
expect.assertions(1);

// Given
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(null);

const useCase = new SetResponseTime({ requestRepository });

// When
try {
await useCase.execute('', 0);
} catch (err) {
//Then
expect(err).toEqual(new Error('Request not found'));
}
});
});

describe('when the response is not found', () => {
it('should throw an error', async () => {
expect.assertions(1);

// Given
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(
new Request('get', '/test', {}, '')
);
(requestRepository.getResponseByRequestId as jest.Mock).mockResolvedValue(
null
);

const useCase = new SetResponseTime({ requestRepository });

// When
try {
await useCase.execute('', 0);
} catch (err) {
//Then
expect(err).toEqual(new Error('Response not found'));
}
});
});

describe('when the request is found', () => {
it('should set the response time', async () => {
// Given
const request = new Request('PUT', '/put', {}, '');
const response = new Response(
201,
{ 'content-type': 'text/plain' },
Buffer.from('Hello world'),
66
);
(requestRepository.getRequestById as jest.Mock).mockResolvedValue(request);
(requestRepository.getResponseByRequestId as jest.Mock).mockResolvedValue(
response
);

const requestId = 'request-id';
const responseTime = 666;
const useCase = new SetResponseTime({ requestRepository });

// When
await useCase.execute(requestId, responseTime);

//Then
expect(requestRepository.persistResponseForRequest).toHaveBeenCalledTimes(
1
);
expect(requestRepository.persistResponseForRequest).toHaveBeenCalledWith(
request,
new Response(
201,
{ 'content-type': 'text/plain' },
Buffer.from('Hello world'),
666
)
);
});
});
32 changes: 32 additions & 0 deletions src/domain/usecase/SetResponseTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { RequestRepository } from '../repository';

interface Depenencies {
requestRepository: RequestRepository;
}

export class SetResponseTime {
private requestRepository: RequestRepository;

public constructor({ requestRepository }: Depenencies) {
this.requestRepository = requestRepository;
}

public async execute(requestId: string, responseTimeInMs: number) {
const [request, response] = await Promise.all([
this.requestRepository.getRequestById(requestId),
this.requestRepository.getResponseByRequestId(requestId),
]);

if (!request) {
throw new Error('Request not found');
}

if (!response) {
throw new Error('Response not found');
}

response.responseTimeInMs = responseTimeInMs;

return this.requestRepository.persistResponseForRequest(request, response);
}
}
1 change: 1 addition & 0 deletions src/domain/usecase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './ClearRequest';
export * from './RefreshRequest';
export * from './ListRequests';
export * from './GetRequestDetails';
export * from './SetResponseTime';
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
RefreshRequest,
ListRequest,
GetRequestDetails,
SetResponseTime,
} from './domain/usecase';
import { NetworkServiceAxios } from './infrastructure/service';
import { RequestRepositoryFile } from './infrastructure/repository';
Expand All @@ -34,6 +35,7 @@ container.register({
refreshRequestUseCase: asClass(RefreshRequest),
listRequestsUseCase: asClass(ListRequest),
getRequestDetailsUseCase: asClass(GetRequestDetails),
setResponseTimeUseCase: asClass(SetResponseTime),

// Repositories
requestRepository: asClass(RequestRepositoryFile).singleton(),
Expand Down

0 comments on commit 63bcbd6

Please sign in to comment.