Commit ce604d66 by William Ross Committed by Jon Yurek

accept url strings

parent c893aa0c
......@@ -191,3 +191,4 @@ require 'paperclip/io_adapters/nil_adapter'
require 'paperclip/io_adapters/attachment_adapter'
require 'paperclip/io_adapters/uploaded_file_adapter'
require 'paperclip/io_adapters/uri_adapter'
require 'paperclip/io_adapters/url_adapter'
module Paperclip
class UrlAdapter < UriAdapter
REGEXP = /^https?:\/\//
def initialize(target)
super(URI(target))
end
end
end
Paperclip.io_adapters.register Paperclip::UrlAdapter do |target|
String === target && target =~ Paperclip::UrlAdapter::REGEXP
end
require './test/helper'
class UrlProxyTest < Test::Unit::TestCase
context "a new instance" do
setup do
@open_return = StringIO.new("xxx")
@open_return.stubs(:content_type).returns("image/png")
Paperclip::UrlAdapter.any_instance.stubs(:download_content).returns(@open_return)
@url = "http://thoughtbot.com/images/thoughtbot-logo.png"
@subject = Paperclip.io_adapters.for(@url)
end
should "return a file name" do
assert_equal "thoughtbot-logo.png", @subject.original_filename
end
should 'close open handle after reading' do
assert_equal true, @open_return.closed?
end
should "return a content type" do
assert_equal "image/png", @subject.content_type
end
should "return the size of the data" do
assert_equal @open_return.size, @subject.size
end
should "generate an MD5 hash of the contents" do
assert_equal Digest::MD5.hexdigest("xxx"), @subject.fingerprint
end
should "generate correct fingerprint after read" do
fingerprint = Digest::MD5.hexdigest(@subject.read)
assert_equal fingerprint, @subject.fingerprint
end
should "generate same fingerprint" do
assert_equal @subject.fingerprint, @subject.fingerprint
end
should "return the data contained in the StringIO" do
assert_equal "xxx", @subject.read
end
should 'accept a content_type' do
@subject.content_type = 'image/png'
assert_equal 'image/png', @subject.content_type
end
should 'accept an original_filename' do
@subject.original_filename = 'image.png'
assert_equal 'image.png', @subject.original_filename
end
end
context "a url with query params" do
setup do
Paperclip::UrlAdapter.any_instance.stubs(:download_content).returns(StringIO.new("xxx"))
@url = "https://github.com/thoughtbot/paperclip?file=test"
@subject = Paperclip.io_adapters.for(@url)
end
should "return a file name" do
assert_equal "paperclip", @subject.original_filename
end
end
context "a url with restricted characters in the filename" do
setup do
Paperclip::UrlAdapter.any_instance.stubs(:download_content).returns(StringIO.new("xxx"))
@url = "https://github.com/thoughtbot/paper:clip.jpg"
@subject = Paperclip.io_adapters.for(@url)
end
should "not generate filenames that include restricted characters" do
assert_equal "paper_clip.jpg", @subject.original_filename
end
should "not generate paths that include restricted characters" do
assert_no_match /:/, @subject.path
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment