Skip to content

Commit 3a96b3a

Browse files
authored
Allow using custom endpoints for Async::HTTP::Internet. (#51)
1 parent 63409fa commit 3a96b3a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lib/async/http/endpoint.rb

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def self.for(scheme, hostname, path = "/", **options)
3333
)
3434
end
3535

36+
# Coerce the given object into an endpoint.
37+
# @parameter url [String | Endpoint] The URL or endpoint to convert.
38+
def self.[](url)
39+
if url.is_a?(Endpoint)
40+
return url
41+
else
42+
Endpoint.parse(url.to_str)
43+
end
44+
end
45+
3646
# @option scheme [String] the scheme to use, overrides the URL scheme.
3747
# @option hostname [String] the hostname to connect to (or bind to), overrides the URL hostname (used for SNI).
3848
# @option port [Integer] the port to bind to, overrides the URL port.

lib/async/http/internet.rb

+2-2
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 = Endpoint[url]
4343
client = self.client_for(endpoint)
4444

4545
body = Body::Buffered.wrap(body)
@@ -60,7 +60,7 @@ def close
6060

6161
::Protocol::HTTP::Methods.each do |name, verb|
6262
define_method(verb.downcase) do |url, headers = nil, body = nil|
63-
self.call(verb, url.to_str, headers, body)
63+
self.call(verb, url, headers, body)
6464
end
6565
end
6666

test/async/http/internet.rb

+14
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)