2 # Copyright Michael Orlitzky
4 # http://michael.orlitzky.com/
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # http://www.fsf.org/licensing/licenses/gpl.html
20 require 'src/uri_utilities.rb'
22 class UriUtilitiesTest
< Test
::Unit::TestCase
25 uu
= UriUtilities
.new()
26 example_uri
= 'http://www.example.com/whatever.avi'
27 uri
= URI
.parse(example_uri
)
28 assert_equal('whatever.avi', uu
.get_filename(uri
))
32 def test_no_filename_results_in_nil
33 uu
= UriUtilities
.new()
34 example_uri
= 'http://www.example.com'
35 uri
= URI
.parse(example_uri
)
36 assert(uu
.get_filename(uri
).nil?)
40 def test_no_filename_with_trailing_slash_results_in_nil
41 uu
= UriUtilities
.new()
42 example_uri
= 'http://www.example.com/'
43 uri
= URI
.parse(example_uri
)
44 assert(uu
.get_filename(uri
).nil?)
48 def test_system_call_exception_on_connection_refused
49 uu
= UriUtilities
.new()
51 # *Hopefully* this connection is refused.
52 example_uri
= 'http://localhost:1/'
53 uri
= URI
.parse(example_uri
)
55 assert_raise Errno
::ECONNREFUSED do
56 uu
.download_with_progress_bar(uri
, 'dummy.tmp')
59 # This should be gone since there was an error.
60 assert(!File
.exists
?('dummy.tmp'))
64 def test_access_denied_on_unwritable_outfile
65 # Get rid of the dummy file if it already exists.
66 File
.delete('dummy.tmp') if File
.exists
?('dummy.tmp')
68 File
.new('dummy.tmp', 'w')
70 # Make the new file unwritable
71 File
.chmod(0400, 'dummy.tmp')
73 uu
= UriUtilities
.new()
75 # *Hopefully* this connection is refused.
76 example_uri
= 'http://localhost:1/'
77 uri
= URI
.parse(example_uri
)
79 assert_raise Errno
::EACCES do
80 uu
.download_with_progress_bar(uri
, 'dummy.tmp')
83 # This should *not* be deleted, since it was unwritable,
84 # and we got an IO error instead of an HTTP or connection
86 assert(File
.exists
?('dummy.tmp'))
88 # Ok, now we delete it anyway since it will mess up the other tests.
89 File
.delete('dummy.tmp')