diff --git a/src/infrastructure/repository/RequestRepositoryFile.test.ts b/src/infrastructure/repository/RequestRepositoryFile.test.ts index 23de7aa..4964611 100644 --- a/src/infrastructure/repository/RequestRepositoryFile.test.ts +++ b/src/infrastructure/repository/RequestRepositoryFile.test.ts @@ -177,6 +177,38 @@ describe('persistResponseForRequest', () => { }); expect(bodyContent).toEqual('Hello world'); }); + + it('should persist no content-type as txt with their meta data', async () => { + // Given + const requestRepository = getRequestRepository(); + const inputRequest = new Request('GET', '/text', {}, ''); + const inputResponse = new Response(200, {}, 'Hello world'); + + // When + await requestRepository.persistResponseForRequest( + inputRequest, + inputResponse + ); + + const metadataContent = await fs.readJSON( + `${OUTPUT_DIRECTORY}/get__text-${inputRequest.id}/metadata.json` + ); + const bodyContent = await fs.readFile( + `${OUTPUT_DIRECTORY}/get__text-${inputRequest.id}/body.txt`, + 'utf-8' + ); + + //Then + expect(metadataContent).toEqual({ + method: 'GET', + status: 200, + url: '/text', + requestBody: '', + requestHeaders: {}, + responseHeaders: {}, + }); + expect(bodyContent).toEqual('Hello world'); + }); }); describe('getResponseByRequestId', () => { @@ -228,6 +260,19 @@ describe('getResponseByRequestId', () => { ) ); }); + + it('should return null when the request does not exist', async () => { + // Given + const requestId = 'does-not-exist'; + + // When + const response = await requestRepositorysitory.getResponseByRequestId( + requestId + ); + + //Then + expect(response).toBeNull(); + }); }); describe('getAllRequests', () => { diff --git a/src/infrastructure/repository/RequestRepositoryFile.ts b/src/infrastructure/repository/RequestRepositoryFile.ts index b63d91d..a9b507e 100644 --- a/src/infrastructure/repository/RequestRepositoryFile.ts +++ b/src/infrastructure/repository/RequestRepositoryFile.ts @@ -19,6 +19,7 @@ function isXml(contentType: string | undefined) { if (!contentType) { return false; } + const lowerCaseContentType = contentType.toLowerCase(); return ( diff --git a/src/infrastructure/repository/RequestRepositoryMemory.ts b/src/infrastructure/repository/RequestRepositoryMemory.ts index 3db1a26..340beb1 100644 --- a/src/infrastructure/repository/RequestRepositoryMemory.ts +++ b/src/infrastructure/repository/RequestRepositoryMemory.ts @@ -1,3 +1,5 @@ +/* istanbul ignore file */ + import { RequestRepository } from '../../domain/repository'; import { Request, Response } from '../../domain/entity'; diff --git a/src/infrastructure/service/NetworkServiceAxios.test.ts b/src/infrastructure/service/NetworkServiceAxios.test.ts index 10ae3ae..6e4abec 100644 --- a/src/infrastructure/service/NetworkServiceAxios.test.ts +++ b/src/infrastructure/service/NetworkServiceAxios.test.ts @@ -194,3 +194,32 @@ describe('Headers handling', () => { }); }); }); + +describe('error handling', () => { + it('should throw an error when the error does not have a response', async () => { + expect.assertions(1); + // Given + ((axios as any) as jest.Mock).mockRejectedValue( + new Error('Failed to fetch') + ); + const request = new Request( + 'GET', + '/test', + { + authorization: 'bearer token', + }, + '' + ); + const networkService = new NetworkServiceAxios({ + targetUrl: 'http://localhost', + }); + + // When + try { + await networkService.executeRequest(request); + } catch (err) { + // Then + expect(err).toBeDefined(); + } + }); +});