Skip to content

Commit 56e874d

Browse files
thekuwayamaioquatix
authored andcommitted
update SSLSocket.open to match TCPSocket.open method signature
1 parent dd87110 commit 56e874d

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

lib/openssl/ssl.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,18 +448,28 @@ class << self
448448
#
449449
# Creates a new instance of SSLSocket.
450450
# _remote\_host_ and _remote_port_ are used to open TCPSocket.
451-
# If _context_ is provided,
452-
# the SSL Sockets initial params will be taken from the context.
453451
# If _local\_host_ and _local\_port_ are specified,
454452
# then those parameters are used on the local end to establish the connection.
453+
# If _context_ is provided,
454+
# the SSL Sockets initial params will be taken from the context.
455455
#
456-
# === Example
457-
# ctx = OpenSSL::SSL::SSLContext.new
458-
# sock = OpenSSL::SSL::SSLSocket.open('localhost', 443, ctx)
456+
# === Examples
457+
#
458+
# sock = OpenSSL::SSL::SSLSocket.open('localhost', 443)
459459
# sock.connect # Initiates a connection to localhost:443
460-
def open(remote_host, remote_port, context=nil, local_host=nil, local_port=nil)
460+
#
461+
# with SSLContext:
462+
#
463+
# ctx = OpenSSL::SSL::SSLContext.new
464+
# sock = OpenSSL::SSL::SSLSocket.open('localhost', 443, context: ctx)
465+
# sock.connect # Initiates a connection to localhost:443 with SSLContext
466+
def open(remote_host, remote_port, local_host=nil, local_port=nil, context: nil)
461467
sock = ::TCPSocket.open(remote_host, remote_port, local_host, local_port)
462-
OpenSSL::SSL::SSLSocket.new(sock, context)
468+
if context.nil?
469+
return OpenSSL::SSL::SSLSocket.new(sock)
470+
else
471+
return OpenSSL::SSL::SSLSocket.new(sock, context)
472+
end
463473
end
464474
end
465475
end

test/test_ssl.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,45 @@ def test_ssl_with_server_cert
5656
}
5757
end
5858

59-
def test_ssl_socket_open
59+
def test_socket_open
60+
start_server { |port|
61+
begin
62+
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port)
63+
ssl.sync_close = true
64+
ssl.connect
65+
66+
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
67+
ensure
68+
ssl&.close
69+
end
70+
}
71+
end
72+
73+
def test_socket_open_with_context
74+
start_server { |port|
75+
begin
76+
ctx = OpenSSL::SSL::SSLContext.new
77+
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, context: ctx)
78+
ssl.sync_close = true
79+
ssl.connect
80+
81+
assert_equal ssl.context, ctx
82+
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
83+
ensure
84+
ssl&.close
85+
end
86+
}
87+
end
88+
89+
def test_socket_open_with_local_address_port_context
6090
start_server { |port|
6191
begin
6292
ctx = OpenSSL::SSL::SSLContext.new
63-
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, ctx)
93+
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, "127.0.0.1", 8000, context: ctx)
6494
ssl.sync_close = true
6595
ssl.connect
6696

97+
assert_equal ssl.context, ctx
6798
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
6899
ensure
69100
ssl&.close

0 commit comments

Comments
 (0)