tcp-client 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,308 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../helper'
4
+
5
+ RSpec.describe TCPClient::Configuration do
6
+ describe '.create' do
7
+ it 'yields a configuration' do
8
+ TCPClient::Configuration.create do |cfg|
9
+ expect(cfg).to be_a TCPClient::Configuration
10
+ end
11
+ end
12
+
13
+ it 'returns the configuration' do
14
+ yielded_cfg = nil
15
+ result = TCPClient::Configuration.create { |cfg| yielded_cfg = cfg }
16
+ expect(result).to be yielded_cfg
17
+ end
18
+ end
19
+
20
+ describe '.new' do
21
+ context 'without any parameter' do
22
+ subject(:configuration) { TCPClient::Configuration.new }
23
+
24
+ it 'allows buffering' do
25
+ expect(configuration.buffered).to be true
26
+ end
27
+
28
+ it 'allows keep alive signals' do
29
+ expect(configuration.keep_alive).to be true
30
+ end
31
+
32
+ it 'allows reverse address lokup' do
33
+ expect(configuration.reverse_lookup).to be true
34
+ end
35
+
36
+ it 'does not allow to normalize network errors' do
37
+ expect(configuration.normalize_network_errors).to be false
38
+ end
39
+
40
+ it 'does not allow SSL connections' do
41
+ expect(configuration.ssl?).to be false
42
+ end
43
+
44
+ it 'configures no timeout values' do
45
+ expect(configuration.connect_timeout).to be_nil
46
+ expect(configuration.read_timeout).to be_nil
47
+ expect(configuration.write_timeout).to be_nil
48
+ end
49
+
50
+ it 'configures default errors' do
51
+ expect(
52
+ configuration.connect_timeout_error
53
+ ).to be TCPClient::ConnectTimeoutError
54
+ expect(
55
+ configuration.read_timeout_error
56
+ ).to be TCPClient::ReadTimeoutError
57
+ expect(
58
+ configuration.write_timeout_error
59
+ ).to be TCPClient::WriteTimeoutError
60
+ end
61
+ end
62
+
63
+ context 'with valid options' do
64
+ subject(:configuration) do
65
+ TCPClient::Configuration.new(
66
+ buffered: false,
67
+ keep_alive: false,
68
+ reverse_lookup: false,
69
+ normalize_network_errors: true,
70
+ ssl: true,
71
+ timeout: 60,
72
+ timeout_error: custom_error
73
+ )
74
+ end
75
+ let(:custom_error) { Class.new(StandardError) }
76
+
77
+ it 'allows to configure buffering' do
78
+ expect(configuration.buffered).to be false
79
+ end
80
+
81
+ it 'allows to configure keep alive signals' do
82
+ expect(configuration.keep_alive).to be false
83
+ end
84
+
85
+ it 'allows to configure reverse address lokup' do
86
+ expect(configuration.reverse_lookup).to be false
87
+ end
88
+
89
+ it 'allows to configure to normalize network errors' do
90
+ expect(configuration.normalize_network_errors).to be true
91
+ end
92
+
93
+ it 'allows to configures SSL connections' do
94
+ expect(configuration.ssl?).to be true
95
+ end
96
+
97
+ it 'allows to configure no timeout values' do
98
+ expect(configuration.connect_timeout).to be 60
99
+ expect(configuration.read_timeout).to be 60
100
+ expect(configuration.write_timeout).to be 60
101
+ end
102
+
103
+ it 'allows to configure timeout errors' do
104
+ expect(configuration.connect_timeout_error).to be custom_error
105
+ expect(configuration.read_timeout_error).to be custom_error
106
+ expect(configuration.write_timeout_error).to be custom_error
107
+ end
108
+
109
+ it 'allows to configure dedicated timeout values' do
110
+ config =
111
+ TCPClient::Configuration.new(
112
+ connect_timeout: 21,
113
+ read_timeout: 42,
114
+ write_timeout: 84
115
+ )
116
+ expect(config.connect_timeout).to be 21
117
+ expect(config.read_timeout).to be 42
118
+ expect(config.write_timeout).to be 84
119
+ end
120
+
121
+ it 'allows to configure dedicated timeout errors' do
122
+ custom_connect = Class.new(StandardError)
123
+ custom_read = Class.new(StandardError)
124
+ custom_write = Class.new(StandardError)
125
+ config =
126
+ TCPClient::Configuration.new(
127
+ connect_timeout_error: custom_connect,
128
+ read_timeout_error: custom_read,
129
+ write_timeout_error: custom_write
130
+ )
131
+ expect(config.connect_timeout_error).to be custom_connect
132
+ expect(config.read_timeout_error).to be custom_read
133
+ expect(config.write_timeout_error).to be custom_write
134
+ end
135
+
136
+ it 'raises when no exception class is used to configure a timeout error' do
137
+ expect do
138
+ TCPClient::Configuration.new(
139
+ connect_timeout_error: double(:something)
140
+ )
141
+ end.to raise_error(TCPClient::NotAnExceptionError)
142
+ expect do
143
+ TCPClient::Configuration.new(read_timeout_error: double(:something))
144
+ end.to raise_error(TCPClient::NotAnExceptionError)
145
+ expect do
146
+ TCPClient::Configuration.new(write_timeout_error: double(:something))
147
+ end.to raise_error(TCPClient::NotAnExceptionError)
148
+ end
149
+ end
150
+
151
+ context 'with invalid attribte' do
152
+ it 'raises an error' do
153
+ expect { TCPClient::Configuration.new(invalid: :value) }.to raise_error(
154
+ TCPClient::UnknownAttributeError
155
+ )
156
+ end
157
+ end
158
+ end
159
+
160
+ describe '#to_hash' do
161
+ subject(:configuration) do
162
+ TCPClient::Configuration.new(
163
+ buffered: false,
164
+ connect_timeout: 1,
165
+ read_timeout: 2,
166
+ write_timeout: 3,
167
+ ssl: {
168
+ min_version: :TLS1_2,
169
+ max_version: :TLS1_3
170
+ }
171
+ )
172
+ end
173
+
174
+ it 'returns itself as an Hash' do
175
+ expect(configuration.to_hash).to eq(
176
+ buffered: false,
177
+ keep_alive: true,
178
+ reverse_lookup: true,
179
+ connect_timeout: 1,
180
+ connect_timeout_error: TCPClient::ConnectTimeoutError,
181
+ read_timeout: 2,
182
+ read_timeout_error: TCPClient::ReadTimeoutError,
183
+ write_timeout: 3,
184
+ write_timeout_error: TCPClient::WriteTimeoutError,
185
+ ssl_params: {
186
+ min_version: :TLS1_2,
187
+ max_version: :TLS1_3
188
+ }
189
+ )
190
+ end
191
+ end
192
+
193
+ describe '#to_h' do
194
+ subject(:configuration) do
195
+ TCPClient::Configuration.new(
196
+ buffered: false,
197
+ connect_timeout: 1,
198
+ read_timeout: 2,
199
+ write_timeout: 3,
200
+ ssl: {
201
+ min_version: :TLS1_2,
202
+ max_version: :TLS1_3
203
+ }
204
+ )
205
+ end
206
+
207
+ it 'returns itself as an Hash' do
208
+ expect(configuration.to_h).to eq(
209
+ buffered: false,
210
+ keep_alive: true,
211
+ reverse_lookup: true,
212
+ connect_timeout: 1,
213
+ connect_timeout_error: TCPClient::ConnectTimeoutError,
214
+ read_timeout: 2,
215
+ read_timeout_error: TCPClient::ReadTimeoutError,
216
+ write_timeout: 3,
217
+ write_timeout_error: TCPClient::WriteTimeoutError,
218
+ ssl_params: {
219
+ min_version: :TLS1_2,
220
+ max_version: :TLS1_3
221
+ }
222
+ )
223
+ end
224
+
225
+ it 'allows to specify the keys the result should contain' do
226
+ expect(
227
+ configuration.to_h(:keep_alive, :read_timeout, :write_timeout)
228
+ ).to eq(keep_alive: true, read_timeout: 2, write_timeout: 3)
229
+ end
230
+ end
231
+
232
+ describe '#dup' do
233
+ subject(:duplicate) { configuration.dup }
234
+ let(:configuration) do
235
+ TCPClient::Configuration.new(
236
+ buffered: false,
237
+ connect_timeout: 1,
238
+ read_timeout: 2,
239
+ write_timeout: 3,
240
+ ssl: {
241
+ min_version: :TLS1_2,
242
+ max_version: :TLS1_3
243
+ }
244
+ )
245
+ end
246
+
247
+ it 'returns a new instance' do
248
+ expect(duplicate).to be_a TCPClient::Configuration
249
+ expect(duplicate.__id__).not_to eq configuration.__id__
250
+ end
251
+
252
+ it 'contains same values as the original' do
253
+ expect(duplicate.buffered).to be false
254
+ expect(duplicate.connect_timeout).to be 1
255
+ expect(duplicate.read_timeout).to be 2
256
+ expect(duplicate.write_timeout).to be 3
257
+ expect(duplicate.ssl?).to be true
258
+ expect(duplicate.ssl_params).to eq(
259
+ min_version: :TLS1_2,
260
+ max_version: :TLS1_3
261
+ )
262
+ end
263
+ end
264
+
265
+ describe 'comparison' do
266
+ context 'comparing two equal instances' do
267
+ let(:config_a) { TCPClient::Configuration.new(timeout: 10) }
268
+ let(:config_b) { TCPClient::Configuration.new(timeout: 10) }
269
+
270
+ it 'compares to equal' do
271
+ expect(config_a).to eq config_b
272
+ end
273
+
274
+ context 'using the == opperator' do
275
+ it 'compares to equal' do
276
+ expect(config_a == config_b).to be true
277
+ end
278
+ end
279
+
280
+ context 'using the === opperator' do
281
+ it 'compares to equal' do
282
+ expect(config_a === config_b).to be true
283
+ end
284
+ end
285
+ end
286
+
287
+ context 'comparing two non-equal instances' do
288
+ let(:config_a) { TCPClient::Configuration.new(timeout: 10) }
289
+ let(:config_b) { TCPClient::Configuration.new(timeout: 20) }
290
+
291
+ it 'compares not to equal' do
292
+ expect(config_a).not_to eq config_b
293
+ end
294
+
295
+ context 'using the == opperator' do
296
+ it 'compares not to equal' do
297
+ expect(config_a == config_b).to be false
298
+ end
299
+ end
300
+
301
+ context 'using the === opperator' do
302
+ it 'compares not to equal' do
303
+ expect(config_a === config_b).to be false
304
+ end
305
+ end
306
+ end
307
+ end
308
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../helper'
4
+
5
+ RSpec.describe 'TCPClient.configure' do
6
+ it 'is the default configuration' do
7
+ expect(TCPClient.configure).to be TCPClient::Configuration.default
8
+ end
9
+
10
+ context 'called with parameters' do
11
+ it 'creates a new configuratiion' do
12
+ expect(TCPClient::Configuration).to receive(:create).once.with(a: 1, b: 2)
13
+ TCPClient.configure(a: 1, b: 2)
14
+ end
15
+
16
+ it 'returns the new configuratiion' do
17
+ expect(TCPClient::Configuration).to receive(:create).and_return(:a_result)
18
+ TCPClient.configure(something: :new)
19
+ expect(TCPClient::Configuration.default).to be :a_result
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../helper'
4
+
5
+ RSpec.describe 'TCPClient::VERSION' do
6
+ it 'is a valid version string' do
7
+ expect(TCPClient::VERSION).to match(/\d+\.\d+\.\d+/)
8
+ end
9
+
10
+ it 'is frozen' do
11
+ expect(TCPClient::VERSION.frozen?).to be true
12
+ end
13
+ end