Skip to content

Commit

Permalink
Restore parsed POST body data if Content-Type is set
Browse files Browse the repository at this point in the history
to address twitter#77
  • Loading branch information
smaeda-ks committed Dec 29, 2018
1 parent 7eb9a13 commit 7f1f7d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ def perform_request_from_options(options, &block)
request.body = multipart_body.join
request.content_type = "multipart/form-data, boundary=\"#{boundary}\""
elsif request.content_type && options.data
request.body = options.data.keys.first
data = ''

if options.data.length == 1 && options.data.values.first == nil
data = options.data.keys.first
else
options.data.each_with_index do |(key, value), i|
data += (i == 0 ? '' : '&') + key.to_s + (value == nil ? '' : '=' + value.to_s)
end
end

request.body = data
elsif options.data
request.set_form_data(options.data)
end
Expand Down
13 changes: 13 additions & 0 deletions test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ def test_content_type_is_not_overridden_if_set_and_data_in_options
client.perform_request_from_options(options)
end

def test_post_body_data_is_not_parsed_if_content_type_is_set

options.request_method = 'post'
options.data = { '{ "data": "do' => nil, 'not' => 'parse', 'me' => '!" }' }
options.headers = { 'Content-Type' => 'application/json' }

mock(client.consumer.http).request(
satisfy { |req| req.is_a?(Net::HTTP::Post) && req.body == '{ "data": "do&not=parse&me=!" }' }
)

client.perform_request_from_options(options)
end

def test_content_type_is_set_to_form_encoded_if_not_set_and_data_in_options
client = Twurl::OAuthClient.test_exemplar

Expand Down

0 comments on commit 7f1f7d7

Please sign in to comment.