Skip to content

Commit

Permalink
Introduces flat_encode option for multipart adapter. (#1163)
Browse files Browse the repository at this point in the history
Fixes #1157
  • Loading branch information
iMacTia committed Jul 9, 2020
1 parent e02a8c1 commit 5acab36
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
11 changes: 9 additions & 2 deletions lib/faraday/request/multipart.rb
Expand Up @@ -12,6 +12,11 @@ class Multipart < UrlEncoded
DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
end

def initialize(app = nil, options = {})
@app = app
@options = options
end

# Checks for files in the payload, otherwise leaves everything untouched.
#
# @param env [Faraday::Env]
Expand All @@ -30,7 +35,7 @@ def process_request?(env)
type = request_type(env)
env.body.respond_to?(:each_key) && !env.body.empty? && (
(type.empty? && has_multipart?(env.body)) ||
(type == self.class.mime_type)
(type == self.class.mime_type)
)
end

Expand Down Expand Up @@ -79,7 +84,9 @@ def unique_boundary
# @param pieces [Array]
def process_params(params, prefix = nil, pieces = nil, &block)
params.inject(pieces || []) do |all, (key, value)|
key = "#{prefix}[#{key}]" if prefix
if prefix
key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
end

case value
when Array
Expand Down
54 changes: 41 additions & 13 deletions spec/faraday/request/multipart_spec.rb
@@ -1,9 +1,10 @@
# frozen_string_literal: true

RSpec.describe Faraday::Request::Multipart do
let(:options) { {} }
let(:conn) do
Faraday.new do |b|
b.request :multipart
b.request :multipart, options
b.request :url_encoded
b.adapter :test do |stub|
stub.post('/echo') do |env|
Expand Down Expand Up @@ -54,9 +55,10 @@
part_bc, body_bc = result.part('b[c]')
expect(part_bc).to_not be_nil
expect(part_bc.filename).to eq('multipart_spec.rb')
expect(part_bc.headers['content-disposition']).to eq(
'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-disposition'])
.to eq(
'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-type']).to eq('text/x-ruby')
expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
expect(body_bc).to eq(File.read(__FILE__))
Expand Down Expand Up @@ -135,9 +137,10 @@
part_bc, body_bc = result.part('b[][c]')
expect(part_bc).to_not be_nil
expect(part_bc.filename).to eq('multipart_spec.rb')
expect(part_bc.headers['content-disposition']).to eq(
'form-data; name="b[][c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-disposition'])
.to eq(
'form-data; name="b[][c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-type']).to eq('text/x-ruby')
expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
expect(body_bc).to eq(File.read(__FILE__))
Expand Down Expand Up @@ -177,9 +180,10 @@
part_bc, body_bc = result.part('b[c]')
expect(part_bc).to_not be_nil
expect(part_bc.filename).to eq('multipart_spec.rb')
expect(part_bc.headers['content-disposition']).to eq(
'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-disposition'])
.to eq(
'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-type']).to eq('text/x-ruby')
expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
expect(body_bc).to eq(File.read(__FILE__))
Expand Down Expand Up @@ -258,9 +262,10 @@
part_bc, body_bc = result.part('b[][c]')
expect(part_bc).to_not be_nil
expect(part_bc.filename).to eq('multipart_spec.rb')
expect(part_bc.headers['content-disposition']).to eq(
'form-data; name="b[][c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-disposition'])
.to eq(
'form-data; name="b[][c]"; filename="multipart_spec.rb"'
)
expect(part_bc.headers['content-type']).to eq('text/x-ruby')
expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
expect(body_bc).to eq(File.read(__FILE__))
Expand All @@ -271,4 +276,27 @@
expect(body_bd).to eq('2')
end
end

context 'when passing flat_encode=true option' do
let(:options) { { flat_encode: true } }
let(:io) { StringIO.new('io-content') }
let(:payload) do
{
a: 1,
b: [
Faraday::UploadIO.new(io, 'application/pdf'),
Faraday::UploadIO.new(io, 'application/pdf')
]
}
end

it_behaves_like 'a multipart request'

it 'encode params using flat encoder' do
response = conn.post('/echo', payload)

expect(response.body).to include('name="b"')
expect(response.body).not_to include('name="b[]"')
end
end
end

0 comments on commit 5acab36

Please sign in to comment.