Skip to content

Commit

Permalink
Add test for not hitting the multipart limit
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnaegle committed Mar 3, 2015
1 parent ad96fd7 commit 3611741
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
31 changes: 31 additions & 0 deletions test/multipart/three_files_three_fields
@@ -0,0 +1,31 @@
--AaB03x
content-disposition: form-data; name="reply"

yes
--AaB03x
content-disposition: form-data; name="to"

people
--AaB03x
content-disposition: form-data; name="from"

others
--AaB03x
content-disposition: form-data; name="fileupload1"; filename="file1.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x
content-disposition: form-data; name="fileupload2"; filename="file2.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x
content-disposition: form-data; name="fileupload3"; filename="file3.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x--
54 changes: 18 additions & 36 deletions test/spec_multipart.rb
Expand Up @@ -476,50 +476,32 @@ def rd.length
end
end

should "not reach a multi-part limit unless files are included" do
should "not reach a multi-part limit" do
begin
previous_limit = Rack::Utils.multipart_part_limit
Rack::Utils.multipart_part_limit = 3

input = <<EOF
--AaB03x\r
content-disposition: form-data; name="reply"\r
\r
yes\r
--AaB03x\r
content-disposition: form-data; name="to"\r
\r
people\r
--AaB03x\r
content-disposition: form-data; name="fileupload"; filename="dj.jpg"\r
Content-Type: image/jpeg\r
Content-Transfer-Encoding: base64\r
\r
/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\r
--AaB03x\r
content-disposition: form-data; name="fileupload1"; filename="dj.jpg"\r
Content-Type: image/jpeg\r
Content-Transfer-Encoding: base64\r
\r
/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\r
--AaB03x--\r
EOF
options = {
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
"CONTENT_LENGTH" => input.size,
:input => StringIO.new(input)
}
Rack::Utils.multipart_part_limit = 4

request = Rack::Request.new Rack::MockRequest.env_for("/", options)
request.POST
request.should.be.form_data
request.content_length.should.equal input.size
env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
params = Rack::Multipart.parse_multipart(env)
params['reply'].should.equal 'yes'
params['to'].should.equal 'people'
params['from'].should.equal 'others'
ensure
Rack::Utils.multipart_part_limit = previous_limit
end

end

should "reach a multipart limit" do
begin
previous_limit = Rack::Utils.multipart_part_limit
Rack::Utils.multipart_part_limit = 3

env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
lambda { Rack::Multipart.parse_multipart(env) }.should.raise(Rack::Multipart::MultipartPartLimitError)
ensure
Rack::Utils.multipart_part_limit = previous_limit
end
end

should "return nil if no UploadedFiles were used" do
data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
Expand Down

0 comments on commit 3611741

Please sign in to comment.