Skip to content

Commit

Permalink
Merge pull request #60 from brandonblack/post_body_fix
Browse files Browse the repository at this point in the history
[fix] Allowing raw request body when Content-Type is already specified
  • Loading branch information
sferik committed Jun 8, 2015
2 parents c88deb7 + bac93af commit 9faa220
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def perform_request_from_options(options, &block)
multipart_body << "\r\n--#{boundary}--\r\n"

request.body = multipart_body.join
request['Content-Type'] = "multipart/form-data, boundary=\"#{boundary}\""
request.content_type = "multipart/form-data, boundary=\"#{boundary}\""
elsif request.content_type && options.data
request.body = options.data.keys.first
elsif options.data
request.set_form_data(options.data)
end
Expand Down
34 changes: 31 additions & 3 deletions test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,37 @@ def test_oauth_options_are_passed_through
class Twurl::OAuthClient::PerformingRequestsFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_request_is_made_using_request_method_and_path_and_data_in_options
client = Twurl::OAuthClient.test_exemplar
mock(client.consumer.http).request(satisfy { |req|
req.is_a?(Net::HTTP::Get) && (req.path == options.path)
})

mock(client.consumer.http).request(
satisfy { |req| req.is_a?(Net::HTTP::Get) && (req.path == options.path) }
)

client.perform_request_from_options(options)
end

def test_content_type_is_not_overridden_if_set_and_data_in_options
client = Twurl::OAuthClient.test_exemplar

options.request_method = 'post'
options.data = { '{ "foo": "bar" }' => nil }
options.headers = { 'Content-Type' => 'application/json' }

mock(client.consumer.http).request(
satisfy { |req| req.is_a?(Net::HTTP::Post) && req.content_type == 'application/json' }
)

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

options.request_method = 'post'
options.data = { '{ "foo": "bar" }' => nil }

mock(client.consumer.http).request(
satisfy { |req| req.is_a?(Net::HTTP::Post) && req.content_type == 'application/x-www-form-urlencoded' }
)

client.perform_request_from_options(options)
end
Expand Down

0 comments on commit 9faa220

Please sign in to comment.