Skip to content

Commit

Permalink
Minor refactor and better test for porter client
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Aug 9, 2024
1 parent 5a07802 commit 78b9517
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ export class PorterClient {
const localConfig = { ...config, baseURL: porterUrl.toString() };
try {
resp = await axios.request(localConfig);
if (resp.status === HttpStatusCode.Ok) {
return resp;
}
} catch (e) {
lastError = e;
continue;
}
if (resp.status === HttpStatusCode.Ok) {
return resp;
}
}
if (lastError !== undefined) {
if (lastError) {
throw lastError;
}
throw new Error(
Expand Down
16 changes: 10 additions & 6 deletions packages/shared/test/porter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ const mockGetUrsulas = (ursulas: Ursula[] = fakeUrsulas()): SpyInstance => {
status: HttpStatusCode.Ok,
data: fakePorterUrsulas(ursulas),
});
case fakePorterUris[1]:
return Promise.resolve({ status: HttpStatusCode.BadRequest, data: '' });
case fakePorterUris[0]:
throw new Error();
default:
throw Promise.resolve({ status: HttpStatusCode.BadRequest });
throw new Error(`Test error`);
}
});
};
Expand Down Expand Up @@ -107,9 +107,13 @@ describe('PorterClient', () => {
it('returns error in case all porters fail', async () => {
const ursulas = fakeUrsulas();
mockGetUrsulas(ursulas);
let porterClient = new PorterClient([fakePorterUris[0], fakePorterUris[1]]);
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError();
let porterClient = new PorterClient([fakePorterUris[1]]);
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(
Error(`Porter returns bad response: 400 - `),
);
porterClient = new PorterClient([fakePorterUris[1], fakePorterUris[0]]);
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError();
expect(porterClient.getUrsulas(ursulas.length)).rejects.toThrowError(
Error(`Test error`),
);
});
});

0 comments on commit 78b9517

Please sign in to comment.