]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blobdiff - test/uri_utilities_test.rb
Added independent (non-wget) file downloads via open-uri.
[dead/whatever-dl.git] / test / uri_utilities_test.rb
diff --git a/test/uri_utilities_test.rb b/test/uri_utilities_test.rb
new file mode 100644 (file)
index 0000000..ad4d4f3
--- /dev/null
@@ -0,0 +1,91 @@
+#
+# Copyright Michael Orlitzky
+#
+# http://michael.orlitzky.com/
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# http://www.fsf.org/licensing/licenses/gpl.html
+#
+
+require 'test/unit'
+require 'src/uri_utilities.rb'
+
+class UriUtilitiesTest < Test::Unit::TestCase
+
+  def test_filename
+    uu = UriUtilities.new()
+    example_uri = 'http://www.example.com/whatever.avi'
+    uri = URI.parse(example_uri)
+    assert_equal('whatever.avi', uu.get_filename(uri))
+  end
+
+  
+  def test_no_filename_results_in_nil
+    uu = UriUtilities.new()
+    example_uri = 'http://www.example.com'
+    uri = URI.parse(example_uri)
+    assert(uu.get_filename(uri).nil?)    
+  end
+
+  
+  def test_no_filename_with_trailing_slash_results_in_nil
+    uu = UriUtilities.new()
+    example_uri = 'http://www.example.com/'
+    uri = URI.parse(example_uri)
+    assert(uu.get_filename(uri).nil?)    
+  end
+
+
+  def test_system_call_exception_on_connection_refused
+    uu = UriUtilities.new()
+
+    # *Hopefully* this connection is refused.
+    example_uri = 'http://localhost:1/'
+    uri = URI.parse(example_uri)
+    
+    assert_raise Errno::ECONNREFUSED do
+      uu.download_with_progress_bar(uri, 'dummy.tmp')
+    end
+
+    # This should be gone since there was an error.
+    assert(!File.exists?('dummy.tmp'))
+  end
+
+  
+  def test_access_denied_on_unwritable_outfile
+    # Get rid of the dummy file if it already exists.
+    File.delete('dummy.tmp') if File.exists?('dummy.tmp')
+    
+    File.new('dummy.tmp', 'w')
+    
+    # Make the new file unwritable
+    File.chmod(0400, 'dummy.tmp')
+    
+    uu = UriUtilities.new()
+
+    # *Hopefully* this connection is refused.
+    example_uri = 'http://localhost:1/'
+    uri = URI.parse(example_uri)
+    
+    assert_raise Errno::EACCES do
+      uu.download_with_progress_bar(uri, 'dummy.tmp')
+    end
+
+    # This should *not* be deleted, since it was unwritable,
+    # and we got an IO error instead of an HTTP or connection
+    # error.
+    assert(File.exists?('dummy.tmp'))
+
+    # Ok, now we delete it anyway since it will mess up the other tests.
+    File.delete('dummy.tmp')
+  end
+end