Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for curb

  • Loading branch information...
commit 3d6db5f969c8ef017daa547ba5fab36105443aea 1 parent a756ec9
@andrehjr andrehjr authored mojodna committed
View
53 lib/oauth/request_proxy/curb_request.rb
@@ -0,0 +1,53 @@
+require 'oauth/request_proxy/base'
+require 'curb'
+require 'uri'
+require 'cgi'
+
+module OAuth::RequestProxy::Curl
+ class Easy < OAuth::RequestProxy::Base
+ # Proxy for signing Typhoeus::Request requests
+ # Usage example:
+ # oauth_params = {:consumer => oauth_consumer, :token => access_token}
+ # req = Typhoeus::Request.new(uri, options)
+ # oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => uri))
+ # req.headers.merge!({"Authorization" => oauth_helper.header})
+ # hydra = Typhoeus::Hydra.new()
+ # hydra.queue(req)
+ # hydra.run
+ # response = req.response
+ proxies ::Curl::Easy
+
+ def uri
+ options[:uri].to_s
+ end
+
+ def parameters
+ if options[:clobber_request]
+ options[:parameters]
+ else
+ post_parameters.merge(query_parameters).merge(options[:parameters] || {})
+ end
+ end
+
+ private
+
+ def query_parameters
+ query = URI.parse(request.url).query
+ return(query ? CGI.parse(query) : {})
+ end
+
+ def post_parameters
+ post_body = {}
+
+ # Post params are only used if posting form data
+ if (request.headers['Content-Type'] && request.headers['Content-Type'].downcase == 'application/x-www-form-urlencoded')
+
+ request.post_body.split("&").each do |str|
+ param = str.split("=")
+ post_body[param[0]] = param[1]
+ end
+ end
+ post_body
+ end
+ end
+end
View
69 test/test_curb_request_proxy.rb
@@ -0,0 +1,69 @@
+require File.dirname(__FILE__) + '/test_helper.rb'
+require 'oauth/request_proxy/curb_request'
+require 'curb'
+
+class CurbRequestProxyTest < Test::Unit::TestCase
+
+ def test_that_proxy_simple_get_request_works
+ request = Curl::Easy.new('/test?key=value')
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test?key=value'})
+
+ expected_parameters = {'key' => ['value']}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+
+ def test_that_proxy_simple_post_request_works_with_arguments
+ request = Curl::Easy.new('/test')
+ params = {'key' => 'value'}
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test', :parameters => params})
+
+ expected_parameters = {'key' => 'value'}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+
+ def test_that_proxy_simple_post_request_works_with_form_data
+ request = Curl::Easy.new('/test')
+ request.post_body = 'key=value'
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
+
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test'})
+
+ expected_parameters = {'key' => 'value'}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+
+ def test_that_proxy_simple_put_request_works_with_arguments
+ request = Curl::Easy.new('/test')
+ params = {'key' => 'value'}
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test', :parameters => params})
+
+ expected_parameters = {'key' => 'value'}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+
+ def test_that_proxy_simple_put_request_works_with_form_data
+ request = Curl::Easy.new('/test')
+ request.post_body = 'key=value'
+
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test'})
+
+ expected_parameters = {}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+
+ def test_that_proxy_post_request_works_with_mixed_parameter_sources
+ request = Curl::Easy.new('/test?key=value')
+ request.post_body = 'key2=value2'
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test?key=value', :parameters => {'key3' => 'value3'}})
+
+ expected_parameters = {'key' => ['value'], 'key2' => 'value2', 'key3' => 'value3'}
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.