Merge pull request #98 from PRX/master

like pull request 97 for multipart check failing when there is an array in the request, but recursive
This commit is contained in:
rick 2011-12-28 09:44:18 -08:00
commit 790d8d3b18
2 changed files with 28 additions and 6 deletions

View File

@ -21,12 +21,11 @@ module Faraday
)
end
def has_multipart?(body)
body.values.each do |val|
if val.respond_to?(:content_type)
return true
elsif val.respond_to?(:values)
return true if has_multipart?(val)
def has_multipart?(obj)
# string is an enum in 1.8, returning list of itself
if obj.respond_to?(:each) && !obj.is_a?(String)
(obj.respond_to?(:values) ? obj.values : obj).each do |val|
return true if (val.respond_to?(:content_type) || has_multipart?(val))
end
end
false

View File

@ -113,4 +113,27 @@ class RequestMiddlewareTest < Faraday::TestCase
end
assert_equal [], regexes
end
def test_multipart_with_arrays
# assume params are out of order
regexes = [
/name\=\"a\"/,
/name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
/name=\"b\[\]\[d\]\"/]
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
response = @conn.post('/echo', payload)
assert_kind_of Faraday::CompositeReadIO, response.body
assert_equal "multipart/form-data;boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
response.headers['Content-Type']
response.body.send(:ios).map{|io| io.read}.each do |io|
if re = regexes.detect { |r| io =~ r }
regexes.delete re
end
end
assert_equal [], regexes
end
end