/
handler_spec.rb
310 lines (247 loc) · 10.6 KB
/
handler_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require 'spec_helper'
require 'ostruct'
require 'stringio'
require 'uri'
require 'openssl'
module Seahorse
module Client
module NetHttp
describe Handler do
before(:each) { WebMock.disable_net_connect! }
let(:config) { OpenStruct.new }
let(:handler) { Handler.new }
let(:make_request) { handler.call(context) }
let(:context) do
RequestContext.new.tap do |context|
context.config = config
context.http_request.endpoint = endpoint
end
end
def endpoint
@endpoint ||= 'http://test.endpoint.api'
end
describe '#session_for' do
it 're-uses session for endpoints that share port, scheme and host' do
session = double('http-session', last_used: Aws::Util.monotonic_milliseconds).as_null_object
pool = ConnectionPool.for({})
expect(pool).to receive(:start_session).
exactly(1).times.
with('http://foo.com').
and_return(session)
endpoint1 = URI.parse('http://foo.com/path?query')
endpoint2 = URI.parse('http://foo.com/different')
endpoint3 = URI.parse('http://foo.com')
sessions = []
pool.session_for(endpoint1) { |https| sessions << https }
pool.session_for(endpoint2) { |https| sessions << https }
pool.session_for(endpoint3) { |https| sessions << https }
expect(sessions).to eq([session, session, session])
end
it 're-uses session for slow request that are taking more time than the configured idle timeout' do
session = double('http-session').as_null_object
allow(session).to receive(:request) { sleep 2 }
pool = ConnectionPool.for(http_idle_timeout: 1)
expect(pool).to receive(:start_session).
exactly(1).times.
and_return(ConnectionPool::ExtendedSession.new(session))
endpoint = URI.parse('http://foo.com')
sessions = []
pool.session_for(endpoint) { |http| http.request; sessions << http }
pool.session_for(endpoint) { |http| http.request; sessions << http }
expect(sessions).to eq([session, session])
end
end
describe '#pool' do
let(:handler_pool) { handler.pool_for(config) }
it 'constructs a ConnectionPool' do
expect(handler_pool).to be_kind_of(ConnectionPool)
end
it 'configures the pool#http_proxy' do
config.http_proxy = 'http://proxy.com'
expect(handler_pool.http_proxy).to eq(URI.parse('http://proxy.com'))
end
it 'configures the pool#http_continue_timeout' do
config.http_continue_timeout = 123
expect(handler_pool.http_continue_timeout).to eq(123)
end
it 'configures the pool#http_open_timeout' do
config.http_open_timeout = 123
expect(handler_pool.http_open_timeout).to eq(123)
end
it 'configures the pool#http_read_timeout' do
config.http_read_timeout = 123
expect(handler_pool.http_read_timeout).to eq(123)
end
it 'configures the pool#http_idle_timeout' do
config.http_idle_timeout = 123
expect(handler_pool.http_idle_timeout).to eq(123)
end
it 'configures the Net::HTTP#keep_alive_timeout' do
config.http_idle_timeout = 123
handler_pool.session_for(URI.parse('http://foo.com')) do |http|
skip 'not supported' unless http.respond_to?(:keep_alive_timeout)
expect(http.keep_alive_timeout).to eq(123)
end
end
it 'configures the pool#http_wire_trace' do
config.http_wire_trace = true
expect(handler_pool.http_wire_trace).to eq(true)
end
it 'configures the pool#logger' do
config.http_wire_trace = true
config.logger = Object.new
expect(handler_pool.logger).to be(config.logger)
end
it 'configures the pool#ssl_verify_peer' do
config.ssl_verify_peer = false
expect(handler_pool.ssl_verify_peer).to eq(false)
end
it 'configures the pool#ssl_ca_bundle' do
config.ssl_ca_bundle = '/path/to/ca-bundle.crt'
expect(handler_pool.ssl_ca_bundle).to eq('/path/to/ca-bundle.crt')
end
it 'configures the pool#ssl_ca_directory' do
config.ssl_ca_bundle = '/path/to/certs'
expect(handler_pool.ssl_ca_bundle).to eq('/path/to/certs')
end
end
describe '#call' do
let(:http_request) { context.http_request }
it 'returns a Response object from #call' do
stub_request(:any, endpoint)
resp = make_request
expect(resp).to be_kind_of(Response)
end
it 'populates the #context of the returned response' do
stub_request(:any, endpoint)
resp = make_request
expect(resp.context).to be(context)
end
describe 'request endpoint' do
it 'makes a request against the given endpoint' do
@endpoint = 'http://foo.bar.com'
stub_request(:any, 'http://foo.bar.com')
make_request
end
it 'observes the Endpoint#port' do
@endpoint = 'http://foo.bar.com:9876'
stub_request(:any, 'http://foo.bar.com:9876')
make_request
end
it 'observes the Endpoint#scheme' do
@endpoint = 'https://foo.bar.com'
stub_request(:any, 'https://foo.bar.com')
make_request
end
end
describe 'request http method' do
it 'uses the http_request#http_method to make the request' do
http_request.http_method = 'POST'
stub_request(:post, endpoint)
make_request
end
it 'raises a helpful error if the request method is invalid' do
message = '`abc` is not a valid http verb'
http_request.http_method = 'abc'
resp = make_request
expect(resp.error.message).to eq(message)
end
end
describe 'request headers' do
it 'passes along http_request#headers' do
http_request.headers['abc'] = 'xyz'
stub_request(:any, endpoint).with(:headers => { 'abc' => 'xyz' })
make_request
end
it 'populates a default content-type header' do
# this prevents net/http from setting content-type on our behalf
stub_request(:any, endpoint).
with(:headers => { 'content-type' => '' })
make_request
end
it 'does not clobber a custom content-type' do
http_request.headers['Content-Type'] = 'application/json'
stub_request(:any, endpoint).
with(:headers => { 'content-type' => 'application/json' })
make_request
end
it 'populates a default accept-encoding header' do
# this prevents net/http from setting accept-encoding on our behalf
stub_request(:any, endpoint).
with(:headers => { 'accept-encoding' => '' })
make_request
end
it 'does not clobber a custom accept-encoding' do
http_request.headers['Accept-Encoding'] = 'text/plain'
stub_request(:any, endpoint).
with(:headers => { 'accept-encoding' => 'text/plain' })
make_request
end
end
describe 'request path' do
it 'sends the request with the correct request uri' do
http_request.endpoint.path = '/path'
stub_request(:any, http_request.endpoint.to_s)
make_request
end
it 'sends the request with the querystring' do
http_request.endpoint.path = '/abc'
http_request.endpoint.query = 'mno=xyz'
stub_request(:any, http_request.endpoint.to_s)
make_request
end
end
describe 'request body' do
it 'sends the body' do
http_request.body = StringIO.new('request-body')
stub_request(:any, endpoint).with(body: 'request-body')
make_request
end
end
describe 'response' do
it 'populates the status code' do
stub_request(:any, endpoint).to_return(status: 200)
resp = make_request
expect(resp.context.http_response.status_code).to eq(200)
end
it 'populates the headers' do
stub_request(:any, endpoint).to_return(headers: { foo: 'bar' })
resp = make_request
expect(resp.context.http_response.headers['foo']).to eq('bar')
end
it 'populates the response body' do
stub_request(:any, endpoint).to_return(body: 'response-body')
resp_body = make_request.context.http_response.body
resp_body.rewind
expect(resp_body.read).to eq('response-body')
end
it 'wraps errors with a NetworkingError' do
stub_request(:any, endpoint).to_raise(EOFError)
resp = make_request
expect(resp.error).to be_a(Seahorse::Client::NetworkingError)
end
if OpenSSL::SSL.const_defined?(:SSLErrorWaitReadable)
it 'wraps OpenSSL::SSL::SSLErrorWaitReadable w/NetworkingError' do
stub_request(:any, endpoint).to_raise(
OpenSSL::SSL::SSLErrorWaitReadable)
resp = make_request
expect(resp.error).to be_a(Seahorse::Client::NetworkingError)
expect(resp.error.original_error).to(
be_kind_of(OpenSSL::SSL::SSLErrorWaitReadable))
end
end
end
describe 'DNS errors' do
it 'gives additional information about DNS errors' do
msg = 'getaddrinfo: nodename nor servname provided, or not known'
stub_request(:any, endpoint).to_raise(SocketError.new(msg))
resp = make_request
expect(resp.error).to be_a(Seahorse::Client::NetworkingError)
expect(resp.error.message).to match("unable to connect to `#{URI.parse(endpoint).host}`; SocketError: #{msg}")
end
end
end
end
end
end
end