diff --git a/lib/transbank/common/options.ts b/lib/transbank/common/options.ts index 99e9454..55f74a4 100644 --- a/lib/transbank/common/options.ts +++ b/lib/transbank/common/options.ts @@ -23,10 +23,11 @@ class Options { * @param timeout Timeout for requests in milliseconds */ constructor(commerceCode: string, apiKey: string, environment: string, timeout?: number) { + const defaultTimeout = 1000 * 60 * 10; this.commerceCode = commerceCode; this.apiKey = apiKey; this.environment = environment; - this.timeout = timeout ?? 60000; + this.timeout = timeout ?? defaultTimeout; } } diff --git a/lib/transbank/common/request_service.ts b/lib/transbank/common/request_service.ts index 095fb95..ca09ed7 100644 --- a/lib/transbank/common/request_service.ts +++ b/lib/transbank/common/request_service.ts @@ -57,7 +57,7 @@ const RequestService = { method: request.method, url: options.environment + request.endpoint, headers: requestHeaders, - timeout: 10000, + timeout: options.timeout, data: request.toJson(), }) .then((response) => { diff --git a/tests/common/options.test.ts b/tests/common/options.test.ts new file mode 100644 index 0000000..0324bbb --- /dev/null +++ b/tests/common/options.test.ts @@ -0,0 +1,50 @@ +import axios from 'axios'; +import RequestService from '../../lib/transbank/common/request_service'; +import { CreateRequest } from '../../lib/transbank/webpay/webpay_plus/requests'; +import { Options, Environment } from '../../lib'; + +test('creates Options object', () => { + let options = new Options('123456', 'asdfg', Environment.Integration); + expect(options.commerceCode).toBe('123456'); + expect(options.apiKey).toBe('asdfg'); + expect(options.environment).toBe(Environment.Integration); +}); + +test('creates Options object with default timeout', () => { + let options = new Options('123456', 'asdfg', Environment.Integration); + expect(options.timeout).toBe(600000); +}); + +test('the timeout parameter is set successfully', async () => { + const request = new CreateRequest( + 'ordenCompra12345678', + 'sesion1234557545', + 10000, + 'https://return.url' + ); + const options = new Options( + '597055555532', + '579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C', + Environment.Integration, + 20 + ); + + const handleTimeout = ( + resolve: (value?: unknown) => void, + reject: (reason?: any) => void + ): void => { + setTimeout(() => { + reject({ code: 'ECONNABORTED', message: 'timeout of 20ms exceeded' }); + }, 2000); + }; + + jest.spyOn(axios, 'request').mockImplementation(() => { + return new Promise(handleTimeout); + }); + + await expect(RequestService.perform(request, options)).rejects.toThrow( + 'AxiosError: timeout of 20ms exceeded' + ); + + (axios.request as jest.Mock).mockRestore(); +}); diff --git a/tests/webpay/common/environment.test.ts b/tests/webpay/common/environment.test.ts new file mode 100644 index 0000000..4429172 --- /dev/null +++ b/tests/webpay/common/environment.test.ts @@ -0,0 +1,9 @@ +import { Environment } from '../../../lib'; + +test('correct integration environment url', () => { + expect(Environment.Integration).toBe('https://webpay3gint.transbank.cl'); +}); + +test('correct production environment url', () => { + expect(Environment.Production).toBe('https://webpay3g.transbank.cl'); +}); diff --git a/tests/webpay_plus/common.test.ts b/tests/webpay_plus/common.test.ts deleted file mode 100644 index c7b20fc..0000000 --- a/tests/webpay_plus/common.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Options } from '../../lib/'; -import { Environment } from '../../lib/'; - -test('creates Options object', () => { - let options = new Options('123456', 'asdfg', Environment.Integration); - expect(options.commerceCode).toBe('123456'); - expect(options.apiKey).toBe('asdfg'); - expect(options.environment).toBe(Environment.Integration); -}); - -test('correct integration environment url', () => { - expect(Environment.Integration).toBe('https://webpay3gint.transbank.cl'); -}); - -test('correct production environment url', () => { - expect(Environment.Production).toBe('https://webpay3g.transbank.cl'); -});