1require 'test/unit' 2require 'uri/ftp' 3 4module URI 5 6 7class TestFTP < Test::Unit::TestCase 8 def setup 9 end 10 11 def test_parse 12 url = URI.parse('ftp://user:pass@host.com/abc/def') 13 assert_kind_of(URI::FTP, url) 14 15 exp = [ 16 'ftp', 17 'user:pass', 'host.com', URI::FTP.default_port, 18 'abc/def', nil, 19 ] 20 ary = [ 21 url.scheme, url.userinfo, url.host, url.port, 22 url.path, url.opaque 23 ] 24 assert_equal(exp, ary) 25 26 assert_equal('user', url.user) 27 assert_equal('pass', url.password) 28 end 29 30 def test_parse_invalid 31 assert_raise(InvalidURIError){URI.parse('ftp:example')} 32 end 33 34 def test_paths 35 # If you think what's below is wrong, please read RubyForge bug 2055, 36 # RFC 1738 section 3.2.2, and RFC 2396. 37 u = URI.parse('ftp://ftp.example.com/foo/bar/file.ext') 38 assert(u.path == 'foo/bar/file.ext') 39 u = URI.parse('ftp://ftp.example.com//foo/bar/file.ext') 40 assert(u.path == '/foo/bar/file.ext') 41 u = URI.parse('ftp://ftp.example.com/%2Ffoo/bar/file.ext') 42 assert(u.path == '/foo/bar/file.ext') 43 end 44 45 def test_assemble 46 # uri/ftp is conservative and uses the older RFC 1738 rules, rather than 47 # assuming everyone else has implemented RFC 2396. 48 uri = URI::FTP.build(['user:password', 'ftp.example.com', nil, 49 '/path/file.zip', 'i']) 50 assert(uri.to_s == 51 'ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i') 52 end 53 54 def test_select 55 assert_equal(['ftp', 'a.b.c', 21], URI.parse('ftp://a.b.c/').select(:scheme, :host, :port)) 56 u = URI.parse('ftp://a.b.c/') 57 ary = u.component.collect {|c| u.send(c)} 58 assert_equal(ary, u.select(*u.component)) 59 assert_raise(ArgumentError) do 60 u.select(:scheme, :host, :not_exist, :port) 61 end 62 end 63end 64 65 66end 67