Skip to content

Allow using custom endpoints for Async::HTTP::Internet #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/async/http/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def self.for(scheme, hostname, path = "/", **options)
)
end

# Coerce the given object into an endpoint.
# @parameter url [String | Endpoint] The URL or endpoint to convert.
def self.[](url)
if url.is_a?(Endpoint)
return url
else
Endpoint.parse(url.to_str)
end
end

# @option scheme [String] the scheme to use, overrides the URL scheme.
# @option hostname [String] the hostname to connect to (or bind to), overrides the URL hostname (used for SNI).
# @option port [Integer] the port to bind to, overrides the URL port.
Expand Down
4 changes: 2 additions & 2 deletions lib/async/http/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def client_for(endpoint)
# @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
# @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
def call(method, url, headers = nil, body = nil)
endpoint = Endpoint.parse(url)
endpoint = Endpoint[url]
client = self.client_for(endpoint)

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

::Protocol::HTTP::Methods.each do |name, verb|
define_method(verb.downcase) do |url, headers = nil, body = nil|
self.call(verb, url.to_str, headers, body)
self.call(verb, url, headers, body)
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/async/http/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@
expect(response).to be(:success?)
expect{JSON.parse(response.read)}.not.to raise_exception
end

it 'can fetch remote website when given custom endpoint instead of url' do
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)

# example of site with invalid certificate that will fail to be fetched without custom SSL options
endpoint = Async::HTTP::Endpoint.parse('https://expired.badssl.com', ssl_context: ssl_context)

response = internet.get(endpoint, headers)

expect(response).to be(:success?)
ensure
response&.close
end
end
Loading