1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add Net::FTP#features and Net::FTP#option

Patch by darkphnx (Dan Wentworth) .  Thanks!
[Feature #15964]
This commit is contained in:
Shugo Maeda 2019-09-02 14:43:35 +09:00
parent e9b271d1e2
commit 633ae3278e
No known key found for this signature in database
GPG key ID: 568AD8CEAD744A13
3 changed files with 135 additions and 0 deletions

4
NEWS
View file

@ -254,6 +254,10 @@ IRB::
* Enable auto indent and save/load history by default.
Net::FTP::
* Add features and options. [Feature #15964]
Net::IMAP::
* Add Server Name Indication (SNI) support. [Feature #15594]

View file

@ -1297,6 +1297,41 @@ module Net
voidcmd(cmd)
end
#
# Issues a FEAT command
#
# Returns an array of supported optional features
#
def features
resp = sendcmd("FEAT")
if !resp.start_with?("211")
raise FTPReplyError, resp
end
feats = []
resp.split("\n").each do |line|
next if !line.start_with?(' ') # skip status lines
feats << line.strip
end
return feats
end
#
# Issues an OPTS command
# - name Should be the name of the option to set
# - params is any optional parameters to supply with the option
#
# example: option('UTF8', 'ON') => 'OPTS UTF8 ON'
#
def option(name, params = nil)
cmd = "OPTS #{name}"
cmd += " #{params}" if params
voidcmd(cmd)
end
#
# Closes the connection. Further operations are impossible until you open
# a new connection with #connect.

View file

@ -1530,6 +1530,102 @@ EOF
end
end
def test_features
commands = []
server = create_ftp_server { |sock|
sock.print("220 (test_ftp).\r\n")
commands.push(sock.gets)
sock.print("211-Features\r\n")
sock.print(" LANG EN*\r\n")
sock.print(" UTF8\r\n")
sock.print("211 End\r\n")
}
begin
begin
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
assert_equal(['LANG EN*', 'UTF8'], ftp.features)
assert_equal("FEAT\r\n", commands.shift)
assert_equal(nil, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
def test_features_not_implemented
commands = []
server = create_ftp_server { |sock|
sock.print("220 (test_ftp).\r\n")
commands.push(sock.gets)
sock.print("502 Not Implemented\r\n")
}
begin
begin
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
assert_raise(Net::FTPPermError) do
ftp.features
end
assert_equal("FEAT\r\n", commands.shift)
assert_equal(nil, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
def test_option
commands = []
server = create_ftp_server { |sock|
sock.print("220 (test_ftp)\r\n")
commands.push(sock.gets)
sock.print("200 OPTS UTF8 command successful\r\n")
}
begin
begin
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
ftp.option("UTF8", "ON")
assert_equal("OPTS UTF8 ON\r\n", commands.shift)
assert_equal(nil, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
def test_option_not_implemented
commands = []
server = create_ftp_server { |sock|
sock.print("220 (test_ftp)\r\n")
commands.push(sock.gets)
sock.print("502 Not implemented\r\n")
}
begin
begin
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
assert_raise(Net::FTPPermError) do
ftp.option("UTF8", "ON")
end
assert_equal("OPTS UTF8 ON\r\n", commands.shift)
assert_equal(nil, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
def test_mlst
commands = []
server = create_ftp_server { |sock|