diff --git a/lib/uri/ftp.rb b/lib/uri/ftp.rb index 1c75e24..0270af5 100644 --- a/lib/uri/ftp.rb +++ b/lib/uri/ftp.rb @@ -249,14 +249,11 @@ def set_path(v) # Returns a String representation of the URI::FTP. def to_s - save_path = nil - if @typecode - save_path = @path - @path = @path + TYPECODE_PREFIX + @typecode - end str = super if @typecode - @path = save_path + suffix_length = (@query ? @query.length + 1 : 0) + + (@fragment ? @fragment.length + 1 : 0) + str.insert(-suffix_length - 1, TYPECODE_PREFIX + @typecode) end return str diff --git a/test/uri/test_ftp.rb b/test/uri/test_ftp.rb index 3ad7864..d35f209 100644 --- a/test/uri/test_ftp.rb +++ b/test/uri/test_ftp.rb @@ -6,6 +6,15 @@ class URI::TestFTP < Test::Unit::TestCase def setup end + + def test_to_s_does_not_mutate_frozen_ftp_uri + uri = URI.parse("ftp://example.test/file;type=i?download=1#section").freeze + + 2.times do + assert_equal("ftp://example.test/file;type=i?download=1#section", uri.to_s) + end + end + def test_parse url = URI.parse('ftp://user:pass@host.com/abc/def') assert_kind_of(URI::FTP, url)