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

Merge pull request #615 from jnunemaker/add_ability_to_force_multipart

Add ability to force multipart
This commit is contained in:
Nikita Misharin 2018-10-20 19:15:40 +03:00 committed by GitHub
commit c92336bb7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 20 deletions

View file

@ -36,8 +36,8 @@ class MicrosoftGraph
def bearer_token
response = HTTParty.post(
"#{MS_BASE_URL}/#{@tenant_id}/#{TOKEN_REQUEST_PATH}",
body: {
multipart: true,
body: {
client_id: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_id],
client_secret: Rails.application.credentials[Rails.env.to_sym][:microsoft_client_secret],
scope: 'https://graph.microsoft.com/.default',

View file

@ -221,7 +221,12 @@ module HTTParty
end
if options[:body]
body = Body.new(options[:body], query_string_normalizer: query_string_normalizer)
body = Body.new(
options[:body],
query_string_normalizer: query_string_normalizer,
force_multipart: options[:multipart]
)
if body.multipart?
content_type = "multipart/form-data; boundary=#{body.boundary}"
@raw_request['Content-Type'] = content_type

View file

@ -3,9 +3,10 @@ require_relative 'multipart_boundary'
module HTTParty
class Request
class Body
def initialize(params, query_string_normalizer: nil)
def initialize(params, query_string_normalizer: nil, force_multipart: false)
@params = params
@query_string_normalizer = query_string_normalizer
@force_multipart = force_multipart
end
def call
@ -21,7 +22,7 @@ module HTTParty
end
def multipart?
params.respond_to?(:to_hash) && has_file?(params.to_hash)
params.respond_to?(:to_hash) && (force_multipart || has_file?(params.to_hash))
end
private
@ -77,7 +78,7 @@ module HTTParty
object.respond_to?(:original_filename) ? object.original_filename : File.basename(object.path)
end
attr_reader :params, :query_string_normalizer
attr_reader :params, :query_string_normalizer, :force_multipart
end
end
end

View file

@ -3,7 +3,9 @@ require 'tempfile'
RSpec.describe HTTParty::Request::Body do
describe '#call' do
subject { described_class.new(params).call }
let(:options) { {} }
subject { described_class.new(params, options).call }
context 'when params is string' do
let(:params) { 'name=Bob%20Jones' }
@ -60,6 +62,37 @@ RSpec.describe HTTParty::Request::Body do
it { is_expected.to eq multipart_params }
context 'when passing multipart as an option' do
let(:options) { { force_multipart: true } }
let(:params) do
{
user: {
first_name: 'John',
last_name: 'Doe',
enabled: true
}
}
end
let(:multipart_params) do
"--------------------------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" \
"Content-Disposition: form-data; name=\"user[enabled]\"\r\n" \
"\r\n" \
"true\r\n" \
"--------------------------c772861a5109d5ef--\r\n"
end
it { is_expected.to eq multipart_params }
end
context 'file object responds to original_filename' do
let(:some_temp_file) { Tempfile.new('some_temp_file.gif') }
let(:expected_file_name) { "some_temp_file.gif" }

View file

@ -343,12 +343,17 @@ RSpec.describe HTTParty::Request do
end
end
context "when body is multipart" do
it "sets header Content-Type: multipart/form-data; boundary=" do
@request.options[:body] = {file: File.open(File::NULL, 'r')}
context "when mulipart" do
subject(:headers) do
@request.send(:setup_raw_request)
headers = @request.instance_variable_get(:@raw_request).each_header.to_a
headers = Hash[*headers.flatten] # Ruby 2.0 doesn't have Array#to_h
Hash[*headers.flatten] # Ruby 2.0 doesn't have Array#to_h
end
context "when body contains file" do
it "sets header Content-Type: multipart/form-data; boundary=" do
@request.options[:body] = {file: File.open(File::NULL, 'r')}
expect(headers['content-type']).to match(%r{^multipart/form-data; boundary=---})
end
@ -356,9 +361,17 @@ RSpec.describe HTTParty::Request do
it "overwrites the header to: multipart/form-data; boundary=" do
@request.options[:body] = {file: File.open(File::NULL, 'r')}
@request.options[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
@request.send(:setup_raw_request)
headers = @request.instance_variable_get(:@raw_request).each_header.to_a
headers = Hash[*headers.flatten] # Ruby 2.0 doesn't have Array#to_h
expect(headers['content-type']).to match(%r{^multipart/form-data; boundary=---})
end
end
end
context 'when mulipart option is provided' do
it "sets header Content-Type: multipart/form-data; boundary=" do
@request.options[:body] = { text: 'something' }
@request.options[:multipart] = true
expect(headers['content-type']).to match(%r{^multipart/form-data; boundary=---})
end
end