Permalink
Browse files

update tests to work better with custom server/port (parkplace ftw)

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2676 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information...
1 parent 7be8acf commit dc06765d00e8c6a11b8d692f46e2c48884ed948a technoweenie committed Jan 14, 2007
Showing with 61 additions and 6 deletions.
  1. +39 −2 lib/technoweenie/attachment_fu/backends/s3.rb
  2. +22 −4 test/backends/remote/s3_test.rb
@@ -148,6 +148,32 @@ def self.included(base) #:nodoc:
base.before_update :rename_file
end
+ def self.protocol
+ @protocol ||= s3_config[:use_ssl] ? 'https://' : 'http://'
+ end
+
+ def self.hostname
+ @hostname ||= s3_config[:server] || AWS::S3::DEFAULT_HOST
+ end
+
+ def self.port_string
+ @port_string ||= s3_config[:port] ? ":#{s3_config[:port]}" : ''
+ end
+
+ module ClassMethods
+ def s3_protocol
+ Technoweenie::AttachmentFu::Backends::S3.protocol
+ end
+
+ def s3_hostname
+ Technoweenie::AttachmentFu::Backends::S3.hostname
+ end
+
+ def s3_port_string
+ Technoweenie::AttachmentFu::Backends::S3.port_string
+ end
+ end
+
# Overwrites the base filename writer in order to store the old filename
def filename=(value)
@old_filename = filename unless filename.nil? || @old_filename
@@ -182,8 +208,7 @@ def full_filename(thumbnail = nil)
#
# The optional thumbnail argument will output the thumbnail's filename (if any).
def s3_url(thumbnail = nil)
- protocol, hostname = s3_config[:use_ssl] ? 'https://' : 'http://', s3_config[:server] || DEFAULT_HOST
- protocol + File.join(hostname, bucket_name, full_filename(thumbnail))
+ File.join(s3_protocol + s3_hostname + s3_port_string, bucket_name, full_filename(thumbnail))
end
alias :public_filename :s3_url
@@ -225,6 +250,18 @@ def current_data
S3Object.value full_filename, bucket_name
end
+ def s3_protocol
+ Technoweenie::AttachmentFu::Backends::S3.protocol
+ end
+
+ def s3_hostname
+ Technoweenie::AttachmentFu::Backends::S3.hostname
+ end
+
+ def s3_port_string
+ Technoweenie::AttachmentFu::Backends::S3.port_string
+ end
+
protected
# Called in the after_destroy callback
def destroy_file
@@ -2,7 +2,7 @@
require 'net/http'
class S3Test < Test::Unit::TestCase
- if File.exist?(File.dirname(__FILE__) + '/../../../../../../config/amazon_s3.yml')
+ if File.exist?(RAILS_ROOT + '/config/amazon_s3.yml')
include BaseAttachmentTests
attachment_model S3Attachment
@@ -33,7 +33,7 @@ def test_should_create_custom_path_prefix(klass = S3WithPathPrefixAttachment)
def test_should_create_valid_url(klass = S3Attachment)
attachment_model klass
attachment = upload_file :filename => '/files/rails.png'
- assert_equal "http://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
+ assert_equal "#{s3_protocol}#{s3_hostname}#{s3_port_string}/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
end
test_against_subclass :test_should_create_valid_url, S3Attachment
@@ -67,15 +67,33 @@ def test_should_delete_attachment_from_s3_when_attachment_record_destroyed(klass
urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
attachment.destroy
- urls.each {|url| assert_kind_of Net::HTTPForbidden, http_response_for(url) }
+ urls.each do |url|
+ begin
+ http_response_for(url)
+ rescue Net::HTTPForbidden, Net::HTTPNotFound
+ nil
+ end
+ end
end
test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
protected
def http_response_for(url)
url = URI.parse(url)
- Net::HTTP.start(url.host) {|http| http.request_head(url.path) }
+ Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
+ end
+
+ def s3_protocol
+ Technoweenie::AttachmentFu::Backends::S3.protocol
+ end
+
+ def s3_hostname
+ Technoweenie::AttachmentFu::Backends::S3.hostname
+ end
+
+ def s3_port_string
+ Technoweenie::AttachmentFu::Backends::S3.port_string
end
else
def test_flunk_s3

0 comments on commit dc06765

Please sign in to comment.