From e6a05e33baa9616d74f1d105b5ac4583679d573a Mon Sep 17 00:00:00 2001 From: Paul Boone Date: Tue, 3 Jun 2008 15:55:09 -0700 Subject: [PATCH] added 2 specs for multipart formdata: (1) for checking request with IO, (2) for testing GET with content_type not erroring on multipart/form-data absence; fixed error with multipart/form-data absence. --- lib/merb-core/dispatch/request.rb | 1 + spec/public/request/multipart_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 0 deletions(-) diff --git a/lib/merb-core/dispatch/request.rb b/lib/merb-core/dispatch/request.rb index 6093270..d7a107a 100644 --- a/lib/merb-core/dispatch/request.rb +++ b/lib/merb-core/dispatch/request.rb @@ -499,6 +499,7 @@ module Merb bufsize = 16384 content_length -= boundary_size status = input.read(boundary_size) + return {} if status == nil || status.empty? raise ControllerExceptions::MultiPartParseError, "bad content body:\n'#{status}' should == '#{boundary + EOL}'" unless status == boundary + EOL rx = /(?:#{EOL})?#{Regexp.quote(boundary,'n')}(#{EOL}|--)/ loop { diff --git a/spec/public/request/multipart_spec.rb b/spec/public/request/multipart_spec.rb index 7b2af11..9d98387 100644 --- a/spec/public/request/multipart_spec.rb +++ b/spec/public/request/multipart_spec.rb @@ -12,4 +12,30 @@ describe Merb::Request do request.params[:file][:tempfile].class.should == Tempfile request.params[:file][:content_type].should == 'text/plain' end + + it "should accept env['rack.input'] as IO object (instead of StringIO)" do + file = Struct.new(:read, :filename, :path). + new("This is a text file with some small content in it.", "sample.txt", "sample.txt") + m = Merb::Test::MultipartRequestHelper::Post.new :file => file + body, head = m.to_multipart + + t = Tempfile.new("io") + t.write(body) + t.close + + fd = IO.sysopen(t.path) + io = IO.for_fd(fd,"r") + request = Merb::Test::RequestHelper::FakeRequest.new({:request_method => "POST", :content_type => 'multipart/form-data, boundary=----------0xKhTmLbOuNdArY', :content_length => body.length},io) + + running {request.params}.should_not raise_error + request.params[:file].should_not be_nil + request.params[:file][:tempfile].class.should == Tempfile + request.params[:file][:content_type].should == 'text/plain' + end + + it "should handle GET with a content_type but an empty body (happens in some browsers such as safari after redirect)" do + request = fake_request({:request_method => "GET", :content_type => 'multipart/form-data, boundary=----------0xKhTmLbOuNdArY', :content_length => 0}, :req => '') + running {request.params}.should_not raise_error + end + end \ No newline at end of file -- 1.5.5.3