1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
# frozen_string_literal: true
RSpec.describe HTTP::FormData::Multipart do
subject(:form_data) { HTTP::FormData::Multipart.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}/ }
describe "#to_s" do
def disposition(params)
params = params.map { |k, v| "#{k}=#{v.inspect}" }.join("; ")
"Content-Disposition: form-data; #{params}"
end
let(:crlf) { "\r\n" }
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}",
"#{crlf}bar#{crlf}",
"--#{boundary_value}#{crlf}",
"#{disposition 'name' => 'baz', 'filename' => file.filename}#{crlf}",
"Content-Type: #{file.content_type}#{crlf}",
"#{crlf}#{file}#{crlf}",
"--#{boundary_value}--#{crlf}"
].join("")
end
it "rewinds content" do
content = form_data.read
expect(form_data.to_s).to eq content
expect(form_data.read).to eq content
end
context "with user-defined boundary" do
subject(:form_data) do
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
end
it "uses the given boundary" do
expect(form_data.to_s).to eq [
"--my-boundary#{crlf}",
"#{disposition 'name' => 'foo'}#{crlf}",
"#{crlf}bar#{crlf}",
"--my-boundary#{crlf}",
"#{disposition 'name' => 'baz', 'filename' => file.filename}#{crlf}",
"Content-Type: #{file.content_type}#{crlf}",
"#{crlf}#{file}#{crlf}",
"--my-boundary--#{crlf}"
].join("")
end
end
context "with filename set to nil" do
let(:part) { HTTP::FormData::Part.new("s", :content_type => "mime/type") }
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
it "doesn't include a filename" do
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
expect(form_data.to_s).to eq [
"--#{boundary_value}#{crlf}",
"#{disposition 'name' => 'foo'}#{crlf}",
"Content-Type: #{part.content_type}#{crlf}",
"#{crlf}s#{crlf}",
"--#{boundary_value}--#{crlf}"
].join("")
end
end
context "with content type set to nil" do
let(:part) { HTTP::FormData::Part.new("s") }
let(:form_data) { HTTP::FormData::Multipart.new({ :foo => part }) }
it "doesn't include a filename" do
boundary_value = form_data.content_type[/(#{boundary})$/, 1]
expect(form_data.to_s).to eq [
"--#{boundary_value}#{crlf}",
"#{disposition 'name' => 'foo'}#{crlf}",
"#{crlf}s#{crlf}",
"--#{boundary_value}--#{crlf}"
].join("")
end
end
end
describe "#size" do
it "returns bytesize of multipart data" do
expect(form_data.size).to eq form_data.to_s.bytesize
end
end
describe "#read" do
it "returns multipart data" do
expect(form_data.read).to eq form_data.to_s
end
end
describe "#rewind" do
it "rewinds the multipart data IO" do
form_data.read
form_data.rewind
expect(form_data.read).to eq form_data.to_s
end
end
describe "#content_type" do
subject { form_data.content_type }
let(:content_type) { %r{^multipart\/form-data; boundary=#{boundary}$} }
it { is_expected.to match(content_type) }
context "with user-defined boundary" do
let(:form_data) do
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
end
it "includes the given boundary" do
expect(form_data.content_type)
.to eq "multipart/form-data; boundary=my-boundary"
end
end
end
describe "#content_length" do
subject { form_data.content_length }
it { is_expected.to eq form_data.to_s.bytesize }
end
describe "#boundary" do
it "returns a new boundary" do
expect(form_data.boundary).to match(boundary)
end
context "with user-defined boundary" do
let(:form_data) do
HTTP::FormData::Multipart.new params, :boundary => "my-boundary"
end
it "returns the given boundary" do
expect(form_data.boundary).to eq "my-boundary"
end
end
end
describe ".generate_boundary" do
it "returns a string suitable as a multipart boundary" do
expect(form_data.class.generate_boundary).to match(boundary)
end
end
end
|