Skip to content

Commit d85631e

Browse files
binarycodeioquatix
authored andcommitted
allow using custom endpoints for Async::HTTP::Internet.
1 parent 63409fa commit d85631e

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/async/http/endpoint.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,15 @@ def tcp_endpoint
229229
::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
230230
end
231231
end
232+
233+
# Coerce the given object into an endpoint.
234+
# @parameter url [String | Endpoint] The URL or endpoint to convert.
235+
def self.Endpoint(url, **options)
236+
if url.is_a?(Endpoint)
237+
return url
238+
else
239+
Endpoint.parse(url, **options)
240+
end
241+
end
232242
end
233243
end

lib/async/http/internet.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def client_for(endpoint)
3939
# @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
4040
# @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
4141
def call(method, url, headers = nil, body = nil)
42-
endpoint = Endpoint.parse(url)
42+
endpoint = HTTP.Endpoint(url)
4343
client = self.client_for(endpoint)
4444

4545
body = Body::Buffered.wrap(body)
@@ -59,8 +59,9 @@ def close
5959
end
6060

6161
::Protocol::HTTP::Methods.each do |name, verb|
62-
define_method(verb.downcase) do |url, headers = nil, body = nil|
63-
self.call(verb, url.to_str, headers, body)
62+
define_method(verb.downcase) do |url_or_endpoint, headers = nil, body = nil|
63+
url_or_endpoint = url_or_endpoint.to_str unless url_or_endpoint.is_a?(Endpoint)
64+
self.call(verb, url_or_endpoint, headers, body)
6465
end
6566
end
6667

test/async/http/internet.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@
3333
expect(response).to be(:success?)
3434
expect{JSON.parse(response.read)}.not.to raise_exception
3535
end
36+
37+
it 'can fetch remote website when given custom endpoint instead of url' do
38+
ssl_context = OpenSSL::SSL::SSLContext.new
39+
ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
40+
41+
# example of site with invalid certificate that will fail to be fetched without custom SSL options
42+
endpoint = Async::HTTP::Endpoint.parse('https://expired.badssl.com', ssl_context: ssl_context)
43+
44+
response = internet.get(endpoint, headers)
45+
46+
expect(response).to be(:success?)
47+
ensure
48+
response&.close
49+
end
3650
end

0 commit comments

Comments
 (0)