Permalink
Browse files

Don't create empty tempfile if no file was selected

  • Loading branch information...
1 parent 7409b0c commit a2a260012d0ab6c52d31f90205469af4b738598b @josh josh committed Jan 21, 2009
Showing with 43 additions and 1 deletion.
  1. +4 −1 lib/rack/utils.rb
  2. +10 −0 test/multipart/empty
  3. +9 −0 test/multipart/none
  4. +20 −0 test/spec_rack_utils.rb
View
@@ -291,7 +291,10 @@ def self.parse_multipart(env)
content_length = -1 if $1 == "--"
end
- if filename
+ if filename == ""
+ # filename is blank which means no file has been selected
+ data = nil
+ elsif filename
body.rewind
# Take the basename of the upload's original filename.
View
@@ -0,0 +1,10 @@
+--AaB03x
+Content-Disposition: form-data; name="submit-name"
+
+Larry
+--AaB03x
+Content-Disposition: form-data; name="files"; filename="file1.txt"
+Content-Type: text/plain
+
+
+--AaB03x--
View
@@ -0,0 +1,9 @@
+--AaB03x
+Content-Disposition: form-data; name="submit-name"
+
+Larry
+--AaB03x
+Content-Disposition: form-data; name="files"; filename=""
+
+
+--AaB03x--
View
@@ -184,6 +184,26 @@ def context env, app=@app; app.call(env); end
params["files"][:tempfile].read.length.should.equal 26473
end
+ specify "should parse multipart upload with empty file" do
+ env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
+ params = Rack::Utils::Multipart.parse_multipart(env)
+ params["submit-name"].should.equal "Larry"
+ params["files"][:type].should.equal "text/plain"
+ params["files"][:filename].should.equal "file1.txt"
+ params["files"][:head].should.equal "Content-Disposition: form-data; " +
+ "name=\"files\"; filename=\"file1.txt\"\r\n" +
+ "Content-Type: text/plain\r\n"
+ params["files"][:name].should.equal "files"
+ params["files"][:tempfile].read.should.equal ""
+ end
+
+ specify "should not create empty an tempfile if no file was selected" do
+ env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
+ params = Rack::Utils::Multipart.parse_multipart(env)
+ params["submit-name"].should.equal "Larry"
+ params["files"].should.equal nil
+ end
+
specify "should parse IE multipart upload and clean up filename" do
env = Rack::MockRequest.env_for("/", multipart_fixture(:ie))
params = Rack::Utils::Multipart.parse_multipart(env)

0 comments on commit a2a2600

Please sign in to comment.