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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
# frozen-string-literal: true
require_relative '../../spec_helper'
describe "Rack::Test::Utils#build_nested_query" do
include Rack::Test::Utils
it 'converts empty strings to =' do
build_nested_query('').must_equal '='
end
it 'converts nil to an empty string' do
build_nested_query(nil).must_equal ''
end
it 'converts hashes with nil values' do
build_nested_query(a: nil).must_equal 'a'
end
it 'converts hashes' do
build_nested_query(a: 1).must_equal 'a=1'
end
it 'converts hashes with multiple keys' do
hash = { a: 1, b: 2 }
build_nested_query(hash).must_equal 'a=1&b=2'
end
it 'converts empty arrays' do
build_nested_query(a: []).must_equal 'a[]='
end
it 'converts arrays with one element' do
build_nested_query(a: [1]).must_equal 'a[]=1'
end
it 'converts arrays with multiple elements' do
build_nested_query(a: [1, 2]).must_equal 'a[]=1&a[]=2'
end
it "converts arrays with brackets '[]' in the name" do
build_nested_query('a[]' => [1, 2]).must_equal 'a%5B%5D=1&a%5B%5D=2'
end
it 'converts nested hashes' do
build_nested_query(a: { b: 1 }).must_equal 'a[b]=1'
end
it 'converts arrays nested in a hash' do
build_nested_query(a: { b: [1, 2] }).must_equal 'a[b][]=1&a[b][]=2'
end
it 'converts arrays of hashes' do
build_nested_query(a: [{ b: 2 }, { c: 3 }]).must_equal 'a[][b]=2&a[][c]=3'
end
it 'supports hash keys with empty arrays' do
input = { collection: [] }
build_nested_query(input).must_equal 'collection[]='
end
end
describe 'Rack::Test::Utils.build_multipart' do
include Rack::Test::Utils
it 'builds multipart bodies' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => files)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['submit-name'].must_equal 'Larry'
params['files'][:filename].must_equal 'foo.txt'
files.pos.must_equal 0
params['files'][:tempfile].read.must_equal files.read
end
it 'handles uploaded files not responding to set_encoding as empty' do
# Capybara::RackTest::Form::NilUploadedFile
c = Class.new(Rack::Test::UploadedFile) do
def initialize
@empty_file = Tempfile.new('nil_uploaded_file')
@empty_file.close
end
def original_filename; ''; end
def content_type; 'application/octet-stream'; end
def path; @empty_file.path; end
def size; 0; end
def read; ''; end
def respond_to?(m, *a)
return false if m == :set_encoding
super(m, *a)
end
end
data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => c.new)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['submit-name'].must_equal 'Larry'
params['files'].must_be_nil
data.must_include 'content-disposition: form-data; name="files"; filename=""'
data.must_include 'content-length: 0'
end
it 'builds multipart bodies from array of files' do
files = [Rack::Test::UploadedFile.new(multipart_file('foo.txt')), Rack::Test::UploadedFile.new(multipart_file('bar.txt'))]
data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => files)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['submit-name'].must_equal 'Larry'
params['files'][0][:filename].must_equal 'foo.txt'
params['files'][0][:tempfile].read.must_equal "bar\n"
params['files'][1][:filename].must_equal 'bar.txt'
params['files'][1][:tempfile].read.must_equal "baz\n"
end
it 'builds multipart bodies from mixed array of a file and a primitive' do
files = [Rack::Test::UploadedFile.new(multipart_file('foo.txt')), 'baz']
data = Rack::Test::Utils.build_multipart('files' => files)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['files'][0][:filename].must_equal 'foo.txt'
params['files'][0][:tempfile].read.must_equal "bar\n"
params['files'][1].must_equal 'baz'
end
it 'builds nested multipart bodies' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => 'Larry', 'files' => files }], 'foo' => %w[1 2])
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['people'][0]['submit-name'].must_equal 'Larry'
params['people'][0]['files'][:filename].must_equal 'foo.txt'
params['people'][0]['files'][:tempfile].read.must_equal "bar\n"
params['foo'].must_equal %w[1 2]
end
it 'builds nested multipart bodies with UTF-8 data' do
files = Rack::Test::UploadedFile.new(multipart_file('mb.txt'))
data = Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => "\u1234", 'files' => files }], 'foo' => %w[1 2])
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['people'][0]['submit-name'].b.must_equal "\u1234".b
params['people'][0]['files'][:filename].must_equal 'mb.txt'
params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b
params['foo'].must_equal %w[1 2]
files = Rack::Test::UploadedFile.new(multipart_file('mb.txt'))
data = Rack::Test::Utils.build_multipart('people' => [{ 'files' => files, 'submit-name' => "\u1234" }], 'foo' => %w[1 2])
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['people'][0]['submit-name'].b.must_equal "\u1234".b
params['people'][0]['files'][:filename].must_equal 'mb.txt'
params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b
params['foo'].must_equal %w[1 2]
end
it 'builds nested multipart bodies with an array of hashes' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('files' => files, 'foo' => [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve' }])
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['files'][:filename].must_equal 'foo.txt'
params['files'][:tempfile].read.must_equal "bar\n"
params['foo'].must_equal [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve' }]
end
it 'builds nested multipart bodies with arbitrarily nested array of hashes' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('files' => files, 'foo' => { 'bar' => [{ 'id' => '1', 'name' => 'Dave' },
{ 'id' => '2', 'name' => 'Steve', 'qux' => [{ 'id' => '3', 'name' => 'mike' },
{ 'id' => '4', 'name' => 'Joan' }] }] })
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['files'][:filename].must_equal 'foo.txt'
params['files'][:tempfile].read.must_equal "bar\n"
params['foo'].must_equal 'bar' => [{ 'id' => '1', 'name' => 'Dave' },
{ 'id' => '2', 'name' => 'Steve', 'qux' => [{ 'id' => '3', 'name' => 'mike' },
{ 'id' => '4', 'name' => 'Joan' }] }]
end
it 'does not break with params that look nested, but are not' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('foo[]' => '1', 'bar[]' => { 'qux' => '2' }, 'files[]' => files)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['files'][0][:filename].must_equal 'foo.txt'
params['files'][0][:tempfile].read.must_equal "bar\n"
params['foo'][0].must_equal '1'
params['bar'][0].must_equal 'qux' => '2'
end
it 'allows for nested files' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('foo' => [{ 'id' => '1', 'data' => files },
{ 'id' => '2', 'data' => %w[3 4] }])
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['foo'][0]['id'].must_equal '1'
params['foo'][0]['data'][:filename].must_equal 'foo.txt'
params['foo'][0]['data'][:tempfile].read.must_equal "bar\n"
params['foo'][1].must_equal 'id' => '2', 'data' => %w[3 4]
end
it 'returns nil if no UploadedFiles were used' do
Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => 'Larry', 'files' => 'contents' }]).must_be_nil
end
it 'allows for forcing multipart uploads even without a file' do
data = Rack::Test::Utils.build_multipart({'foo' => [{ 'id' => '2', 'data' => %w[3 4] }]}, true, true)
options = {
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
'CONTENT_LENGTH' => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for('/', options)
params = Rack::Multipart.parse_multipart(env)
params['foo'][0].must_equal 'id' => '2', 'data' => %w[3 4]
end
it 'raises ArgumentErrors if params is not a Hash' do
proc do
Rack::Test::Utils.build_multipart('foo=bar')
end.must_raise(ArgumentError, 'value must be a Hash')
end
def multipart_file(name)
File.join(File.dirname(__FILE__), '..', '..', 'fixtures', name.to_s)
end
end
|