Skip to content

Commit

Permalink
Fix: Encoding params values in multipart bodies
Browse files Browse the repository at this point in the history
This fixes an error that happens when testing a file upload with the new
Rails 3 snowman character.
  • Loading branch information
alan committed Jul 30, 2010
1 parent 2caed94 commit c5a4555
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def build_primitive_part(parameter_name, value)
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"\r
\r
#{value}\r
#{escape(value)}\r
EOF
end

Expand Down
2 changes: 1 addition & 1 deletion spec/rack/test/multipart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def second_uploaded_file

it "sends params with encoding sensitive values" do
post "/", "photo" => uploaded_file, "foo" => "bar? baz"
last_request.POST["foo"].should == "bar? baz"
last_request.POST["foo"].should == "bar%3F+baz"
end

it "sends params with parens in names" do
Expand Down
20 changes: 20 additions & 0 deletions spec/rack/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@
params["files"][1][:tempfile].read.should == "baz\n"
end

it "encodes special parameter values in multipart bodies" do
files = [Rack::Test::UploadedFile.new(multipart_file("foo.txt")), Rack::Test::UploadedFile.new(multipart_file("bar.txt"))]
data = build_multipart("_question" => "?", "files" => files)

options = {
"CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
"CONTENT_LENGTH" => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for("/", options)
params = Rack::Utils::Multipart.parse_multipart(env)
check params["_question"].should == "%3F"

check params["files"][0][:filename].should == "foo.txt"
params["files"][0][:tempfile].read.should == "bar\n"

check params["files"][1][:filename].should == "bar.txt"
params["files"][1][:tempfile].read.should == "baz\n"
end

it "builds nested multipart bodies" do
files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
data = build_multipart("people" => [{"submit-name" => "Larry", "files" => files}])
Expand Down

0 comments on commit c5a4555

Please sign in to comment.