Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to create Test::UploadedFile instances without the file system #149

Merged
merged 3 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,28 @@ class UploadedFile
# The content type of the "uploaded" file
attr_accessor :content_type

def initialize(path, content_type = 'text/plain', binary = false)
raise "#{path} file does not exist" unless ::File.exist?(path)

def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
if content.respond_to?(:read)
initialize_from_io(content, original_filename)
else
initialize_from_file_path(content)
end
@content_type = content_type
@original_filename = ::File.basename(path)

@tempfile = Tempfile.new([@original_filename, ::File.extname(path)])
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
@tempfile.binmode if binary

ObjectSpace.define_finalizer(self, self.class.finalize(@tempfile))

FileUtils.copy_file(path, @tempfile.path)
end

def path
@tempfile.path
tempfile.path
end

alias local_path path

def method_missing(method_name, *args, &block) #:nodoc:
@tempfile.__send__(method_name, *args, &block)
tempfile.public_send(method_name, *args, &block)
end

def respond_to?(method_name, include_private = false) #:nodoc:
@tempfile.respond_to?(method_name, include_private) || super
def respond_to_missing?(method_name, include_private = false) #:nodoc:
tempfile.respond_to?(method_name, include_private) || super
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://ruby-doc.org/core-2.4.1/Object.html#method-i-respond_to_missing-3F says:

DO NOT USE THIS DIRECTLY.

Is there a way to implement this in another way?

end

def self.finalize(file)
Expand All @@ -55,6 +50,26 @@ def self.actually_finalize(file)
file.close
file.unlink
end

private

def initialize_from_io(io, original_filename)
@tempfile = io
@original_filename = original_filename || raise(ArgumentError, 'Missing `original_filename` for IO')
end

def initialize_from_file_path(path)
raise "#{path} file does not exist" unless ::File.exist?(path)

@original_filename = ::File.basename(path)

@tempfile = Tempfile.new([@original_filename, ::File.extname(path)])
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)

ObjectSpace.define_finalizer(self, self.class.finalize(@tempfile))

FileUtils.copy_file(path, @tempfile.path)
end
end
end
end
10 changes: 4 additions & 6 deletions lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,15 @@ def build_primitive_part(parameter_name, value)
module_function :build_primitive_part

def build_file_part(parameter_name, uploaded_file)
::File.open(uploaded_file.path, 'rb') do |physical_file|
physical_file.set_encoding(Encoding::BINARY) if physical_file.respond_to?(:set_encoding)
<<-EOF
uploaded_file.set_encoding(Encoding::BINARY) if uploaded_file.respond_to?(:set_encoding)
<<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"; filename="#{escape(uploaded_file.original_filename)}"\r
Content-Type: #{uploaded_file.content_type}\r
Content-Length: #{::File.stat(uploaded_file.path).size}\r
Content-Length: #{uploaded_file.size}\r
\r
#{physical_file.read}\r
#{uploaded_file.read}\r
EOF
end
end
module_function :build_file_part
end
Expand Down
37 changes: 26 additions & 11 deletions spec/rack/test/uploaded_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ def test_file_path
it 'responds to things that Tempfile responds to' do
uploaded_file = Rack::Test::UploadedFile.new(test_file_path)

expect(uploaded_file).to respond_to(:close)
expect(uploaded_file).to respond_to(:close!)
expect(uploaded_file).to respond_to(:delete)
expect(uploaded_file).to respond_to(:length)
expect(uploaded_file).to respond_to(:open)
expect(uploaded_file).to respond_to(:path)
expect(uploaded_file).to respond_to(:size)
expect(uploaded_file).to respond_to(:unlink)
expect(uploaded_file).to respond_to(:read)
expect(uploaded_file).to respond_to(:original_filename)
expect(uploaded_file).to respond_to(:tempfile) # Allows calls to params[:file].tempfile
Tempfile.public_instance_methods(false).each do |method|
expect(uploaded_file).to respond_to(method)
end
end

it "creates Tempfiles with original file's extension" do
Expand All @@ -51,4 +43,27 @@ def test_file_path
end
end
end

describe '#initialize' do
subject { -> { uploaded_file } }
let(:uploaded_file) { described_class.new(io, original_filename: original_filename) }

context 'with an IO object' do
let(:io) { StringIO.new('I am content') }

context 'with an original filename' do
let(:original_filename) { 'content.txt' }

it 'sets the specified filename' do
subject.call
uploaded_file.original_filename.should == original_filename
end
end

context 'without an original filename' do
let(:original_filename) { nil }
it { should raise_error(ArgumentError) }
end
end
end
end