File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -229,5 +229,15 @@ def tcp_endpoint
229
229
::IO ::Endpoint . tcp ( self . hostname , port , **tcp_options )
230
230
end
231
231
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
232
242
end
233
243
end
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ def client_for(endpoint)
39
39
# @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
40
40
# @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
41
41
def call ( method , url , headers = nil , body = nil )
42
- endpoint = Endpoint . parse ( url )
42
+ endpoint = HTTP . Endpoint ( url )
43
43
client = self . client_for ( endpoint )
44
44
45
45
body = Body ::Buffered . wrap ( body )
@@ -59,8 +59,9 @@ def close
59
59
end
60
60
61
61
::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 )
64
65
end
65
66
end
66
67
Original file line number Diff line number Diff line change 33
33
expect ( response ) . to be ( :success? )
34
34
expect { JSON . parse ( response . read ) } . not . to raise_exception
35
35
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
36
50
end
You can’t perform that action at this time.
0 commit comments