Skip to content

Commit f05ee44

Browse files
authored
Merge pull request #1490 from Adyen/set-http-client-timeout
Default timeout for HTTP client
2 parents b027ee6 + da52dc0 commit f05ee44

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/__tests__/httpClient.spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import HttpClientException from "../httpClient/httpClientException";
77
import { binlookup } from "../typings";
88
import { ApiConstants } from "../constants/apiConstants";
99
import {paymentMethodsSuccess} from "../__mocks__/checkout/paymentMethodsSuccess";
10-
10+
import Config from "../config";
1111

1212
beforeEach((): void => {
1313
nock.cleanAll();
@@ -177,4 +177,26 @@ describe("HTTP Client", function (): void {
177177
expect(response.paymentMethods).toBeTruthy();
178178
});
179179

180-
});
180+
});
181+
182+
describe('Config class', () => {
183+
const DEFAULT_TIMEOUT = 30000; // Define the default timeout value
184+
185+
test('should set default timeout when no timeout is provided', () => {
186+
// Instantiate the Config class without passing a timeout
187+
const config = new Config();
188+
189+
// Expect that the timeout is set to the default value (30000)
190+
expect(config.connectionTimeoutMillis).toBe(DEFAULT_TIMEOUT);
191+
});
192+
193+
test('should set custom timeout when provided', () => {
194+
// Instantiate the Config class with a custom timeout
195+
const customTimeout = 50000;
196+
const config = new Config({ connectionTimeoutMillis: customTimeout });
197+
198+
// Expect that the timeout is set to the custom value (50000)
199+
expect(config.connectionTimeoutMillis).toBe(customTimeout);
200+
});
201+
});
202+

src/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface ConfigConstructor {
1111
terminalApiLocalEndpoint?: string;
1212
}
1313

14+
const DEFAULT_TIMEOUT = 30000; // Default timeout value (30 sec)
15+
1416
class Config {
1517
public username?: string;
1618
public password?: string;
@@ -31,7 +33,8 @@ class Config {
3133
if (options.marketPayEndpoint) this.marketPayEndpoint = options.marketPayEndpoint;
3234
if (options.applicationName) this.applicationName = options.applicationName;
3335
if (options.apiKey) this.apiKey = options.apiKey;
34-
if (options.connectionTimeoutMillis) this.connectionTimeoutMillis = options.connectionTimeoutMillis || 30000;
36+
// Set the timeout to DEFAULT_TIMEOUT if not provided
37+
this.connectionTimeoutMillis = options.connectionTimeoutMillis ?? DEFAULT_TIMEOUT;
3538
if (options.certificatePath) this.certificatePath = options.certificatePath;
3639
if (options.terminalApiCloudEndpoint) this.terminalApiCloudEndpoint = options.terminalApiCloudEndpoint;
3740
if (options.terminalApiLocalEndpoint) this.terminalApiLocalEndpoint = options.terminalApiLocalEndpoint;

src/httpClient/httpURLConnectionClient.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ class HttpURLConnectionClient implements ClientInterface {
118118
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_NAME] = LibraryConstants.LIB_NAME;
119119
requestOptions.headers[ApiConstants.ADYEN_LIBRARY_VERSION] = LibraryConstants.LIB_VERSION;
120120

121-
return httpsRequest(requestOptions);
121+
// create a new ClientRequest object
122+
const req = httpsRequest(requestOptions);
123+
124+
// set the timeout on the ClientRequest instance
125+
if (requestOptions.timeout) {
126+
req.setTimeout(requestOptions.timeout);
127+
}
128+
129+
return req;
122130
}
123131

124132
private getQuery(params: [string, string][]): string {

0 commit comments

Comments
 (0)