Add recursive search through request to see if it is multi-part, with test.

This commit is contained in:
kookster 2011-12-15 18:19:05 -05:00
parent 86767d2dbd
commit 0e4a05242d
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