Skip to content

Commit 7c4ed31

Browse files
authored
Better handling of supported schemes. (#166)
1 parent 004dff5 commit 7c4ed31

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/async/http/endpoint.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ module Async
1818
module HTTP
1919
# Represents a way to connect to a remote HTTP server.
2020
class Endpoint < ::IO::Endpoint::Generic
21+
SCHEMES = ['HTTP', 'HTTPS', 'WS', 'WSS'].to_h do |scheme|
22+
[scheme.downcase, URI.scheme_list[scheme]]
23+
end
24+
2125
def self.parse(string, endpoint = nil, **options)
2226
url = URI.parse(string).normalize
2327

2428
return self.new(url, endpoint, **options)
2529
end
2630

2731
# Construct an endpoint with a specified scheme, hostname, optional path, and options.
32+
#
33+
# @parameter scheme [String] The scheme to use, e.g. "http" or "https".
34+
# @parameter hostname [String] The hostname to connect to (or bind to).
35+
# @parameter *options [Hash] Additional options, passed to {#initialize}.
2836
def self.for(scheme, hostname, path = "/", **options)
2937
# TODO: Consider using URI.for once it becomes available:
30-
uri_klass = URI.scheme_list[scheme.upcase] || URI::HTTP
38+
uri_klass = SCHEMES.fetch(scheme.downcase) do
39+
raise ArgumentError, "Unsupported scheme: #{scheme}"
40+
end
3141

3242
self.new(
3343
uri_klass.new(scheme, nil, hostname, nil, nil, path, nil, nil, nil).normalize,

test/async/http/endpoint.rb

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181
expect(subject).not.to be(:secure?)
8282
end
8383
end
84+
85+
with 'invalid scheme' do
86+
it "should raise an argument error" do
87+
expect do
88+
Async::HTTP::Endpoint.for("foo", "localhost")
89+
end.to raise_exception(ArgumentError, message: be =~ /scheme/)
90+
91+
expect do
92+
Async::HTTP::Endpoint.for(:http, "localhost", "/foo")
93+
end.to raise_exception(ArgumentError, message: be =~ /scheme/)
94+
end
95+
end
8496
end
8597

8698
with '#secure?' do

0 commit comments

Comments
 (0)