Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multipart to set its header even when other headers are provided #576

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,6 @@ def setup_raw_request
@raw_request = http_method.new(request_uri(uri))
@raw_request.body_stream = options[:body_stream] if options[:body_stream]

if options[:body]
body = Body.new(options[:body], query_string_normalizer: query_string_normalizer)
if body.multipart?
content_type = "multipart/form-data; boundary=#{body.boundary}"
@raw_request.initialize_http_header('Content-Type' => content_type)
end
@raw_request.body = body.call
end

if options[:headers].respond_to?(:to_hash)
headers_hash = options[:headers].to_hash

Expand All @@ -227,6 +218,15 @@ def setup_raw_request
end
end

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

if options[:basic_auth] && send_authorization_header?
@raw_request.basic_auth(username, password)
@credentials_sent = true
Expand Down
21 changes: 21 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,27 @@
expect(CGI.unescape(body)).to eq("foo=bar&foo=baz&page=1")
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')}
@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

context "and header Content-Type is provided" 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
end

describe 'http' do
Expand Down