1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Fix multipart line-breaks and quotes

This commit is contained in:
Nikita Misharin 2018-01-30 23:41:38 +03:00
parent 806278e7f2
commit a3d8528fc3
2 changed files with 22 additions and 22 deletions

View file

@ -30,17 +30,17 @@ module HTTParty
normalized_params = params.flat_map { |key, value| HashConversions.normalize_keys(key, value) }
multipart = normalized_params.inject('') do |memo, (key, value)|
memo += "--#{boundary}\n"
memo += "Content-Disposition: form-data; name='#{key}'"
memo += "; filename='#{File.basename(value)}'" if file?(value)
memo += "\n"
memo += "Content-Type: application/octet-stream\n" if file?(value)
memo += "\n"
memo += "--#{boundary}\r\n"
memo += %(Content-Disposition: form-data; name="#{key}")
memo += %(; filename="#{File.basename(value)}") if file?(value)
memo += "\r\n"
memo += "Content-Type: application/octet-stream\r\n" if file?(value)
memo += "\r\n"
memo += file?(value) ? value.read : value
memo += "\n"
memo += "\r\n"
end
multipart += "--#{boundary}--\n"
multipart += "--#{boundary}--\r\n"
end
def has_file?(hash)

View file

@ -32,20 +32,20 @@ RSpec.describe HTTParty::Request::Body do
}
end
let(:multipart_params) do
"--------------------------c772861a5109d5ef\n" \
"Content-Disposition: form-data; name='user[avatar]'; filename='tiny.gif'\n" \
"Content-Type: application/octet-stream\n" \
"\n" \
"GIF89a\u0001\u0000\u0001\u0000\u0000\xFF\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0000;\n" \
"--------------------------c772861a5109d5ef\n" \
"Content-Disposition: form-data; name='user[first_name]'\n" \
"\n" \
"John\n" \
"--------------------------c772861a5109d5ef\n" \
"Content-Disposition: form-data; name='user[last_name]'\n" \
"\n" \
"Doe\n" \
"--------------------------c772861a5109d5ef--\n"
"--------------------------c772861a5109d5ef\r\n" \
"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"tiny.gif\"\r\n" \
"Content-Type: application/octet-stream\r\n" \
"\r\n" \
"GIF89a\u0001\u0000\u0001\u0000\u0000\xFF\u0000,\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0002\u0000;\r\n" \
"--------------------------c772861a5109d5ef\r\n" \
"Content-Disposition: form-data; name=\"user[first_name]\"\r\n" \
"\r\n" \
"John\r\n" \
"--------------------------c772861a5109d5ef\r\n" \
"Content-Disposition: form-data; name=\"user[last_name]\"\r\n" \
"\r\n" \
"Doe\r\n" \
"--------------------------c772861a5109d5ef--\r\n"
end
it { is_expected.to eq multipart_params }