Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timeout option in request #99

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/transbank/common/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/transbank/common/request_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
50 changes: 50 additions & 0 deletions tests/common/options.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
9 changes: 9 additions & 0 deletions tests/webpay/common/environment.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
17 changes: 0 additions & 17 deletions tests/webpay_plus/common.test.ts

This file was deleted.

Loading