]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - test/uri_utilities_test.rb
ad4d4f3cbc641a099c830b7f035d26647ad1aee3
[dead/whatever-dl.git] / test / uri_utilities_test.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
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.
10 #
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.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 require 'test/unit'
20 require 'src/uri_utilities.rb'
21
22 class UriUtilitiesTest < Test::Unit::TestCase
23
24 def test_filename
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))
29 end
30
31
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?)
37 end
38
39
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?)
45 end
46
47
48 def test_system_call_exception_on_connection_refused
49 uu = UriUtilities.new()
50
51 # *Hopefully* this connection is refused.
52 example_uri = 'http://localhost:1/'
53 uri = URI.parse(example_uri)
54
55 assert_raise Errno::ECONNREFUSED do
56 uu.download_with_progress_bar(uri, 'dummy.tmp')
57 end
58
59 # This should be gone since there was an error.
60 assert(!File.exists?('dummy.tmp'))
61 end
62
63
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')
67
68 File.new('dummy.tmp', 'w')
69
70 # Make the new file unwritable
71 File.chmod(0400, 'dummy.tmp')
72
73 uu = UriUtilities.new()
74
75 # *Hopefully* this connection is refused.
76 example_uri = 'http://localhost:1/'
77 uri = URI.parse(example_uri)
78
79 assert_raise Errno::EACCES do
80 uu.download_with_progress_bar(uri, 'dummy.tmp')
81 end
82
83 # This should *not* be deleted, since it was unwritable,
84 # and we got an IO error instead of an HTTP or connection
85 # error.
86 assert(File.exists?('dummy.tmp'))
87
88 # Ok, now we delete it anyway since it will mess up the other tests.
89 File.delete('dummy.tmp')
90 end
91 end