From d203fd741ec52c8c99a48f4978539b3b2bb792a4 Mon Sep 17 00:00:00 2001 From: chvarkov Date: Tue, 10 Sep 2024 18:10:30 +0000 Subject: [PATCH] fix: network error test --- .../http-recaptcha-enterprice.spec.ts | 4 +--- test/utils/mocked-recaptcha-api.ts | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/test/integrations/http-recaptcha-enterprice.spec.ts b/test/integrations/http-recaptcha-enterprice.spec.ts index c86bbe8..e2a01ce 100644 --- a/test/integrations/http-recaptcha-enterprice.spec.ts +++ b/test/integrations/http-recaptcha-enterprice.spec.ts @@ -204,9 +204,7 @@ describe('HTTP Recaptcha Enterprise', () => { }); test('Enterprise Network error', async () => { - mockedRecaptchaApi.addError('test_enterprise_network_err', { - code: 'ECONNRESET', - }); + mockedRecaptchaApi.addNetworkError('test_enterprise_network_err', 'ECONNRESET'); const res: request.Response = await http.post( '/test/submit', diff --git a/test/utils/mocked-recaptcha-api.ts b/test/utils/mocked-recaptcha-api.ts index ce719b4..79358f9 100644 --- a/test/utils/mocked-recaptcha-api.ts +++ b/test/utils/mocked-recaptcha-api.ts @@ -5,6 +5,7 @@ import * as qs from 'qs'; export class MockedRecaptchaApi { private readonly responseMap: Map = new Map(); + private readonly networkErrorMap: Map = new Map(); private readonly errorMap: Map = new Map< string, { statusCode?: number; code?: string; payload?: LiteralObject } @@ -12,6 +13,7 @@ export class MockedRecaptchaApi { getAxios(): AxiosInstance { const responseMap = this.responseMap; + const networkErrorMap = this.networkErrorMap; const errorMap = this.errorMap; const instance = axios.create({}); return Object.assign(instance, { @@ -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) { @@ -75,4 +93,10 @@ export class MockedRecaptchaApi { return this; } + + addNetworkError(token: string, errorCode: string): this { + this.networkErrorMap.set(token, errorCode); + + return this; + } }