-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command for changing response time (#46)
Fixes #45
- Loading branch information
1 parent
03174e2
commit 63bcbd6
Showing
6 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters