Skip to content

Commit

Permalink
[broken] update specs to test any enumerable case
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed May 29, 2021
1 parent 705ab84 commit 52c66d9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
71 changes: 38 additions & 33 deletions spec/lib/http/form_data/multipart_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
# frozen_string_literal: true

RSpec.describe HTTP::FormData::Multipart do
subject(:form_data) { HTTP::FormData::Multipart.new params }
subject(:form_data) { described_class.new params }

let(:file) { HTTP::FormData::File.new fixture "the-http-gem.info" }
let(:params) { { :foo => :bar, :baz => file } }
let(:boundary) { /-{21}[a-f0-9]{42}/ }

# https://github.com/httprb/http/issues/663
it "supports any Enumerables of pairs" do
form_data = described_class.new(
Enumerator.new do |y|
y << ["metadata", %(filename="first.txt")]
y << ["file", HTTP::FormData::File.new(StringIO.new("uno"), :content_type => "plain/text", :filename => "abc")]
y << ["metadata", %(filename="second.txt")]
y << ["file", HTTP::FormData::File.new(StringIO.new("dos"), :content_type => "plain/text", :filename => "xyz")]
y << ["metadata", %w[why not]]
end
)

expect(form_data.to_s).to eq([
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="metadata"\r\n),
%(\r\nfilename="first.txt"\r\n),
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="file"; filename="abc"\r\n),
%(Content-Type: plain/text\r\n),
%(\r\nuno\r\n),
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="metadata"\r\n),
%(\r\nfilename="second.txt"\r\n),
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="file"; filename="xyz"\r\n),
%(Content-Type: plain/text\r\n),
%(\r\ndos\r\n),
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="metadata"\r\n),
%(\r\nwhy\r\n),
%(--#{form_data.boundary}\r\n),
%(Content-Disposition: form-data; name="metadata"\r\n),
%(\r\nnot\r\n),
%(--#{form_data.boundary}--\r\n)
].join)
end

describe "#to_s" do
def disposition(params)
params = params.map { |k, v| "#{k}=#{v.inspect}" }.join("; ")
Expand Down Expand Up @@ -86,38 +123,6 @@ def disposition(params)
].join)
end
end

# https://github.com/httprb/http/issues/663
context "when params is a list of pairs" do
let(:params) do
[
["metadata", %(filename=first.txt)],
["file", HTTP::FormData::File.new(StringIO.new("uno"), :content_type => "plain/text", :filename => "abc")],
["metadata", %(filename=second.txt)],
["file", HTTP::FormData::File.new(StringIO.new("dos"), :content_type => "plain/text", :filename => "xyz")]
]
end

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}),
%(#{crlf}filename=first.txt#{crlf}),
%(--#{form_data.boundary}#{crlf}),
%(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}),
%(Content-Disposition: form-data; name="file"; filename="xyz"#{crlf}),
%(Content-Type: plain/text#{crlf}),
%(#{crlf}dos#{crlf}),
%(--#{form_data.boundary}--#{crlf})
].join)
end
end
end

describe "#size" do
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/http/form_data/urlencoded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
let(:data) { { "foo[bar]" => "test" } }
subject(:form_data) { HTTP::FormData::Urlencoded.new data }

it "supports any Enumerables of pairs" do
form_data = described_class.new(
Enumerator.new do |y|
y << %w[foo abc]
y << ["foo", %w[def xyz]]
end
)

expect(form_data.to_s).to eq("foo=abc&foo=def&foo=xyz")
end

describe "#content_type" do
subject { form_data.content_type }
it { is_expected.to eq "application/x-www-form-urlencoded" }
Expand Down

0 comments on commit 52c66d9

Please sign in to comment.