Skip to content

Commit

Permalink
Add required boundary between 'pairs' in spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Kelly committed May 28, 2021
1 parent 246a2ca commit 2631b36
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/http/form_data/multipart.rb
Expand Up @@ -54,8 +54,8 @@ def tail
end

def parts(data)
return Param.coerce FormData.ensure_hash data if @params.is_a?(Hash)
Param.coerce_array_of_pairs data
return Param.coerce_array_of_pairs data if data.is_a?(Array)
Param.coerce FormData.ensure_hash data
end
end
end
Expand Down
14 changes: 5 additions & 9 deletions lib/http/form_data/multipart/param.rb
Expand Up @@ -31,14 +31,12 @@ class Param
# @param [FormData::File, FormData::Part, #to_s] value
def initialize(name, value)
@name = name.to_s

@part =
if value.is_a?(FormData::Part)
value
else
FormData::Part.new(value)
end

@io = CompositeIO.new [header, @part, footer]
end

Expand All @@ -52,12 +50,10 @@ def self.coerce(data)
params = []

data.each do |name, values|
binding.pry
Array(values).each do |value|
params << new(name, value)
end
end

params
end

Expand All @@ -70,11 +66,11 @@ def self.coerce(data)
def self.coerce_array_of_pairs(data)
params = []

data.each_slice(2) do |first, second|
binding.pry
params << new(first[0], first[1])
params << new(second[0], second[1])
binding.pry
data.each do |pair|
name, values = pair
Array(values).each do |value|
params << new(name, value)
end
end

params
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/http/form_data/multipart_spec.rb
Expand Up @@ -17,7 +17,6 @@ def disposition(params)

it "properly generates multipart data" do
boundary_value = form_data.boundary

expect(form_data.to_s).to eq([
"--#{boundary_value}#{crlf}",
"#{disposition 'name' => 'foo'}#{crlf}",
Expand Down Expand Up @@ -99,7 +98,7 @@ def disposition(params)
]
end

it "allows duplicate param names and preservesd given order" do
it "allows duplicate param names and preserves given order" do
expect(form_data.to_s).to eq([
%(--#{form_data.boundary}#{crlf}),
%(Content-Disposition: form-data; name="metadata"#{crlf}),
Expand All @@ -108,6 +107,7 @@ def disposition(params)
%(Content-Disposition: form-data; name="file"; filename="abc"#{crlf}),
%(Content-Type: plain/text#{crlf}),
%(#{crlf}uno#{crlf}),
%(--#{form_data.boundary}#{crlf}),
%(Content-Disposition: form-data; name="metadata"#{crlf}#{crlf}),
%(filename=second.txt#{crlf}),
%(--#{form_data.boundary}#{crlf}),
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -17,6 +17,8 @@
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
# This option will remove the default 200 character limit for RSpec diffs
expectations.max_formatted_output_length = nil
end

config.mock_with :rspec do |mocks|
Expand Down

0 comments on commit 2631b36

Please sign in to comment.