Skip to content

Commit 81ab96c

Browse files
authored
Default values for HTTP Client Timeout (#298)
* Set default timeouts * Use Faraday::RequestOptions * Pin Faraday version * Use positional parameter * Set default timeouts on Ruby 3.2+ * Run timeout tests with Ruby 3.2+ only * Use Faraday v2.8.1
1 parent a26075e commit 81ab96c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ source 'https://rubygems.org'
44

55
ruby '>= 2.7.0'
66

7-
gem 'faraday'
7+
gem 'faraday', '2.8.1'
88

99
gem 'activesupport', group: :development
1010
gem 'bundler', group: :development

lib/adyen/client.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ def initialize(ws_user: nil, ws_password: nil, api_key: nil, oauth_token: nil, e
3232
end
3333
@mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
3434
@live_url_prefix = live_url_prefix
35-
@connection_options = connection_options || Faraday::ConnectionOptions.new
35+
if RUBY_VERSION >= '3.2'
36+
# set default timeouts
37+
@connection_options = connection_options || Faraday::ConnectionOptions.new(
38+
request: Faraday::RequestOptions.new(
39+
open_timeout: 30,
40+
timeout: 60
41+
)
42+
)
43+
else
44+
@connection_options = connection_options || Faraday::ConnectionOptions.new
45+
end
46+
3647
@terminal_region = terminal_region
3748
end
3849

spec/client_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,20 @@
113113
Adyen::Client.new(env: :test, connection_options: connection_options)
114114
end
115115

116+
# test with Ruby 3.2+ only (where Faraday requestOptions timeout is supported)
116117
it 'initiates a Faraday connection with the provided options' do
117-
connection_options = Faraday::ConnectionOptions.new
118+
skip "Only runs on Ruby >= 3.2" unless RUBY_VERSION >= '3.2'
119+
connection_options = Faraday::ConnectionOptions.new(
120+
request: {
121+
open_timeout: 5,
122+
timeout: 10
123+
}
124+
)
118125
expect(Faraday::ConnectionOptions).not_to receive(:new)
119126
client = Adyen::Client.new(api_key: 'api_key', env: :mock, connection_options: connection_options)
127+
# verify custom options
128+
expect(client.connection_options[:request][:open_timeout]).to eq(5)
129+
expect(client.connection_options[:request][:timeout]).to eq(10)
120130

121131
mock_faraday_connection = double(Faraday::Connection)
122132
client.service_url(@shared_values[:service], 'payments/details', client.checkout.version)
@@ -130,6 +140,14 @@
130140
client.checkout.payments_api.payments_details(request_body)
131141
end
132142

143+
# test with Ruby 3.2+ only (where Faraday requestOptions timeout is supported)
144+
it 'initiates a Faraday connection with the expected default timeouts' do
145+
skip "Only runs on Ruby >= 3.2" unless RUBY_VERSION >= '3.2'
146+
client = Adyen::Client.new(env: :test)
147+
expect(client.connection_options[:request][:open_timeout]).to eq(30)
148+
expect(client.connection_options[:request][:timeout]).to eq(60)
149+
end
150+
133151
it "checks the creation of checkout url" do
134152
client = Adyen::Client.new(api_key: "api_key", env: :test)
135153
expect(client.service_url("Checkout", "paymentMethods", "71")).

0 commit comments

Comments
 (0)