Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinechalifour committed Aug 18, 2019
1 parent 2c23e39 commit d4906b2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/infrastructure/repository/RequestRepositoryFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions src/infrastructure/repository/RequestRepositoryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function isXml(contentType: string | undefined) {
if (!contentType) {
return false;
}

const lowerCaseContentType = contentType.toLowerCase();

return (
Expand Down
2 changes: 2 additions & 0 deletions src/infrastructure/repository/RequestRepositoryMemory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* istanbul ignore file */

import { RequestRepository } from '../../domain/repository';
import { Request, Response } from '../../domain/entity';

Expand Down
29 changes: 29 additions & 0 deletions src/infrastructure/service/NetworkServiceAxios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});

0 comments on commit d4906b2

Please sign in to comment.