Skip to content

Commit

Permalink
fix: network error test
Browse files Browse the repository at this point in the history
  • Loading branch information
chvarkov committed Sep 10, 2024
1 parent dad24b7 commit d203fd7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 1 addition & 3 deletions test/integrations/http-recaptcha-enterprice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ describe('HTTP Recaptcha Enterprise', () => {
});

test('Enterprise Network error', async () => {
mockedRecaptchaApi.addError<VerifyResponseV2>('test_enterprise_network_err', {
code: 'ECONNRESET',
});
mockedRecaptchaApi.addNetworkError('test_enterprise_network_err', 'ECONNRESET');

const res: request.Response = await http.post(
'/test/submit',
Expand Down
24 changes: 24 additions & 0 deletions test/utils/mocked-recaptcha-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import * as qs from 'qs';

export class MockedRecaptchaApi {
private readonly responseMap: Map<string, LiteralObject> = new Map<string, LiteralObject>();
private readonly networkErrorMap: Map<string, string> = new Map<string, string>();
private readonly errorMap: Map<string, LiteralObject> = new Map<
string,
{ statusCode?: number; code?: string; payload?: LiteralObject }
>();

getAxios(): AxiosInstance {
const responseMap = this.responseMap;
const networkErrorMap = this.networkErrorMap;
const errorMap = this.errorMap;
const instance = axios.create({});
return Object.assign(instance, {
Expand All @@ -34,6 +36,22 @@ export class MockedRecaptchaApi {
return Promise.resolve(res);
}

const networkError = networkErrorMap.get(token);

if (networkError) {
const err: AxiosError = {
request: data,
message: 'Request was failed',
isAxiosError: true,
stack: new Error().stack,
name: 'AxiosError',
code: networkError,
toJSON: () => ({}),
};

return Promise.reject(err);
}

const errData = errorMap.get(token);

if (errData) {
Expand Down Expand Up @@ -75,4 +93,10 @@ export class MockedRecaptchaApi {

return this;
}

addNetworkError(token: string, errorCode: string): this {
this.networkErrorMap.set(token, errorCode);

return this;
}
}

0 comments on commit d203fd7

Please sign in to comment.