Skip to content

Commit 11bb446

Browse files
author
Matt Kelly
committed
Introduce multi-part pair spec
1 parent 48ada9a commit 11bb446

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/http/form_data/multipart.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Multipart
1616

1717
# @param [#to_h, Hash] data form data key-value Hash
1818
def initialize(data, boundary: self.class.generate_boundary)
19-
parts = Param.coerce FormData.ensure_hash data
19+
parts = Param.coerce FormData.ensure_hash data unless @params.is_a?(Array)
2020

2121
@boundary = boundary.to_s.freeze
2222
@io = CompositeIO.new [*parts.flat_map { |part| [glue, part] }, tail]
@@ -54,6 +54,14 @@ def glue
5454
def tail
5555
@tail ||= "--#{@boundary}--#{CRLF}"
5656
end
57+
58+
def join_multipart_pairs(pairs)
59+
pairs.each_cons(2).each do |pair|
60+
multipart = pair.first
61+
file = pair.last
62+
63+
end
64+
end
5765
end
5866
end
5967
end

spec/lib/http/form_data/multipart_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ def disposition(params)
8787
].join)
8888
end
8989
end
90+
91+
# https://github.com/httprb/http/issues/663
92+
context "when params is a list of pairs" do
93+
let(:params) do
94+
[
95+
["metadata", %(filename=first.txt)],
96+
["file", HTTP::FormData::File.new(StringIO.new("uno"), :content_type => 'plain/text', :filename => "stream-123")],
97+
["metadata", %(filename=second.txt)],
98+
["file", HTTP::FormData::File.new(StringIO.new("dos"), :content_type => 'plain/text', :filename => "stream-456")]
99+
]
100+
end
101+
102+
it "allows duplicate param names and preservesd given order" do
103+
# form_data.boundary
104+
105+
expect(form_data.to_s).to eq([
106+
%(--#{form_data.boundary}#{crlf}),
107+
%(Content-Disposition: form-data; name="metadata"#{crlf}#{crlf}),
108+
%(filename=first.txt#{crlf}),
109+
%(--#{form_data.boundary}#{crlf}),
110+
%(--#{boundary_value}--#{crlf})
111+
].join)
112+
end
113+
end
90114
end
91115

92116
describe "#size" do

0 commit comments

Comments
 (0)