motionbox-oauth 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/.gemtest +0 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +46 -0
  4. data/HISTORY +160 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +75 -0
  7. data/Rakefile +37 -0
  8. data/TODO +32 -0
  9. data/bin/oauth +5 -0
  10. data/examples/yql.rb +44 -0
  11. data/lib/digest/hmac.rb +104 -0
  12. data/lib/oauth.rb +13 -0
  13. data/lib/oauth/cli.rb +378 -0
  14. data/lib/oauth/client.rb +4 -0
  15. data/lib/oauth/client/action_controller_request.rb +65 -0
  16. data/lib/oauth/client/em_http.rb +124 -0
  17. data/lib/oauth/client/helper.rb +91 -0
  18. data/lib/oauth/client/net_http.rb +120 -0
  19. data/lib/oauth/consumer.rb +382 -0
  20. data/lib/oauth/core_ext.rb +31 -0
  21. data/lib/oauth/errors.rb +3 -0
  22. data/lib/oauth/errors/error.rb +4 -0
  23. data/lib/oauth/errors/problem.rb +14 -0
  24. data/lib/oauth/errors/unauthorized.rb +12 -0
  25. data/lib/oauth/helper.rb +88 -0
  26. data/lib/oauth/oauth.rb +13 -0
  27. data/lib/oauth/oauth_test_helper.rb +25 -0
  28. data/lib/oauth/request_proxy.rb +24 -0
  29. data/lib/oauth/request_proxy/action_controller_request.rb +62 -0
  30. data/lib/oauth/request_proxy/base.rb +174 -0
  31. data/lib/oauth/request_proxy/curb_request.rb +55 -0
  32. data/lib/oauth/request_proxy/em_http_request.rb +74 -0
  33. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  34. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  35. data/lib/oauth/request_proxy/net_http.rb +72 -0
  36. data/lib/oauth/request_proxy/rack_request.rb +44 -0
  37. data/lib/oauth/request_proxy/typhoeus_request.rb +53 -0
  38. data/lib/oauth/server.rb +66 -0
  39. data/lib/oauth/signature.rb +45 -0
  40. data/lib/oauth/signature/base.rb +110 -0
  41. data/lib/oauth/signature/hmac/base.rb +15 -0
  42. data/lib/oauth/signature/hmac/md5.rb +8 -0
  43. data/lib/oauth/signature/hmac/rmd160.rb +8 -0
  44. data/lib/oauth/signature/hmac/sha1.rb +9 -0
  45. data/lib/oauth/signature/hmac/sha2.rb +8 -0
  46. data/lib/oauth/signature/md5.rb +13 -0
  47. data/lib/oauth/signature/plaintext.rb +23 -0
  48. data/lib/oauth/signature/rsa/sha1.rb +46 -0
  49. data/lib/oauth/signature/sha1.rb +13 -0
  50. data/lib/oauth/token.rb +7 -0
  51. data/lib/oauth/tokens/access_token.rb +71 -0
  52. data/lib/oauth/tokens/consumer_token.rb +33 -0
  53. data/lib/oauth/tokens/request_token.rb +32 -0
  54. data/lib/oauth/tokens/server_token.rb +9 -0
  55. data/lib/oauth/tokens/token.rb +17 -0
  56. data/oauth.gemspec +150 -0
  57. data/tasks/deployment.rake +34 -0
  58. data/tasks/environment.rake +7 -0
  59. data/tasks/website.rake +17 -0
  60. data/test/cases/oauth_case.rb +19 -0
  61. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  62. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  63. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  64. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  65. data/test/integration/consumer_test.rb +307 -0
  66. data/test/keys/rsa.cert +11 -0
  67. data/test/keys/rsa.pem +16 -0
  68. data/test/test_access_token.rb +26 -0
  69. data/test/test_action_controller_request_proxy.rb +133 -0
  70. data/test/test_consumer.rb +171 -0
  71. data/test/test_curb_request_proxy.rb +77 -0
  72. data/test/test_em_http_client.rb +80 -0
  73. data/test/test_em_http_request_proxy.rb +115 -0
  74. data/test/test_helper.rb +26 -0
  75. data/test/test_hmac_sha1.rb +20 -0
  76. data/test/test_net_http_client.rb +280 -0
  77. data/test/test_net_http_request_proxy.rb +72 -0
  78. data/test/test_oauth_helper.rb +71 -0
  79. data/test/test_rack_request_proxy.rb +40 -0
  80. data/test/test_request_token.rb +51 -0
  81. data/test/test_rsa_sha1.rb +59 -0
  82. data/test/test_server.rb +40 -0
  83. data/test/test_signature.rb +22 -0
  84. data/test/test_signature_base.rb +32 -0
  85. data/test/test_signature_plain_text.rb +31 -0
  86. data/test/test_token.rb +14 -0
  87. data/test/test_typhoeus_request_proxy.rb +80 -0
  88. metadata +284 -0
@@ -0,0 +1,171 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ require 'mocha'
3
+
4
+ require 'stringio'
5
+
6
+ # This performs testing against Andy Smith's test server http://term.ie/oauth/example/
7
+ # Thanks Andy.
8
+ # This also means you have to be online to be able to run these.
9
+ class ConsumerTest < Test::Unit::TestCase
10
+ def setup
11
+ @consumer=OAuth::Consumer.new(
12
+ 'consumer_key_86cad9', '5888bf0345e5d237',
13
+ {
14
+ :site=>"http://blabla.bla",
15
+ :proxy=>"http://user:password@proxy.bla:8080",
16
+ :request_token_path=>"/oauth/example/request_token.php",
17
+ :access_token_path=>"/oauth/example/access_token.php",
18
+ :authorize_path=>"/oauth/example/authorize.php",
19
+ :scheme=>:header,
20
+ :http_method=>:get
21
+ })
22
+ @token = OAuth::ConsumerToken.new(@consumer,'token_411a7f', '3196ffd991c8ebdb')
23
+ @request_uri = URI.parse('http://example.com/test?key=value')
24
+ @request_parameters = { 'key' => 'value' }
25
+ @nonce = 225579211881198842005988698334675835446
26
+ @timestamp = "1199645624"
27
+ @consumer.http=Net::HTTP.new(@request_uri.host, @request_uri.port)
28
+ end
29
+
30
+ def test_initializer
31
+ assert_equal "consumer_key_86cad9",@consumer.key
32
+ assert_equal "5888bf0345e5d237",@consumer.secret
33
+ assert_equal "http://blabla.bla",@consumer.site
34
+ assert_equal "http://user:password@proxy.bla:8080",@consumer.proxy
35
+ assert_equal "/oauth/example/request_token.php",@consumer.request_token_path
36
+ assert_equal "/oauth/example/access_token.php",@consumer.access_token_path
37
+ assert_equal "http://blabla.bla/oauth/example/request_token.php",@consumer.request_token_url
38
+ assert_equal "http://blabla.bla/oauth/example/access_token.php",@consumer.access_token_url
39
+ assert_equal "http://blabla.bla/oauth/example/authorize.php",@consumer.authorize_url
40
+ assert_equal :header,@consumer.scheme
41
+ assert_equal :get,@consumer.http_method
42
+ end
43
+
44
+ def test_defaults
45
+ @consumer=OAuth::Consumer.new(
46
+ "key",
47
+ "secret",
48
+ {
49
+ :site=>"http://twitter.com"
50
+ })
51
+ assert_equal "key",@consumer.key
52
+ assert_equal "secret",@consumer.secret
53
+ assert_equal "http://twitter.com",@consumer.site
54
+ assert_nil @consumer.proxy
55
+ assert_equal "/oauth/request_token",@consumer.request_token_path
56
+ assert_equal "/oauth/access_token",@consumer.access_token_path
57
+ assert_equal "http://twitter.com/oauth/request_token",@consumer.request_token_url
58
+ assert_equal "http://twitter.com/oauth/access_token",@consumer.access_token_url
59
+ assert_equal "http://twitter.com/oauth/authorize",@consumer.authorize_url
60
+ assert_equal :header,@consumer.scheme
61
+ assert_equal :post,@consumer.http_method
62
+ end
63
+
64
+ def test_override_paths
65
+ @consumer=OAuth::Consumer.new(
66
+ "key",
67
+ "secret",
68
+ {
69
+ :site=>"http://twitter.com",
70
+ :request_token_url=>"http://oauth.twitter.com/request_token",
71
+ :access_token_url=>"http://oauth.twitter.com/access_token",
72
+ :authorize_url=>"http://site.twitter.com/authorize"
73
+ })
74
+ assert_equal "key",@consumer.key
75
+ assert_equal "secret",@consumer.secret
76
+ assert_equal "http://twitter.com",@consumer.site
77
+ assert_equal "/oauth/request_token",@consumer.request_token_path
78
+ assert_equal "/oauth/access_token",@consumer.access_token_path
79
+ assert_equal "http://oauth.twitter.com/request_token",@consumer.request_token_url
80
+ assert_equal "http://oauth.twitter.com/access_token",@consumer.access_token_url
81
+ assert_equal "http://site.twitter.com/authorize",@consumer.authorize_url
82
+ assert_equal :header,@consumer.scheme
83
+ assert_equal :post,@consumer.http_method
84
+ end
85
+
86
+ def test_that_token_response_should_be_uri_parameter_format_as_default
87
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
88
+
89
+ hash = @consumer.token_request(:get, "")
90
+
91
+ assert_equal "token", hash[:oauth_token]
92
+ assert_equal "secret", hash[:oauth_token_secret]
93
+ end
94
+
95
+ def test_can_provided_a_block_to_interpret_token_response
96
+ @consumer.expects(:request).returns(create_stub_http_response)
97
+
98
+ hash = @consumer.token_request(:get, '') {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
99
+
100
+ assert_equal 'token', hash[:oauth_token]
101
+ assert_equal 'secret', hash[:oauth_token_secret]
102
+ end
103
+
104
+ def test_token_request_follows_redirect
105
+ redirect_url = @request_uri.clone
106
+ redirect_url.path = "/oauth/example/request_token_redirect.php"
107
+ stub_request(:get, /.*#{@request_uri.path}/).to_return(:status => 301, :headers => {'Location' => redirect_url.to_s})
108
+ stub_request(:get, /.*#{redirect_url.path}/).to_return(:body => "oauth_token=token&oauth_token_secret=secret")
109
+
110
+ hash = @consumer.token_request(:get, @request_uri.path) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
111
+
112
+ assert_equal 'token', hash[:oauth_token]
113
+ assert_equal 'secret', hash[:oauth_token_secret]
114
+ end
115
+
116
+ def test_that_can_provide_a_block_to_interpret_a_request_token_response
117
+ @consumer.expects(:request).returns(create_stub_http_response)
118
+
119
+ token = @consumer.get_request_token {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
120
+
121
+ assert_equal 'token', token.token
122
+ assert_equal 'secret', token.secret
123
+ end
124
+
125
+ def test_that_block_is_not_mandatory_for_getting_an_access_token
126
+ stub_token = mock
127
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
128
+
129
+ token = @consumer.get_access_token(stub_token)
130
+
131
+ assert_equal 'token', token.token
132
+ assert_equal 'secret', token.secret
133
+ end
134
+
135
+ def test_that_can_provide_a_block_to_interpret_an_access_token_response
136
+ stub_token = mock
137
+ @consumer.expects(:request).returns(create_stub_http_response)
138
+
139
+ token = @consumer.get_access_token(stub_token) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
140
+
141
+ assert_equal 'token', token.token
142
+ assert_equal 'secret', token.secret
143
+ end
144
+
145
+ def test_that_not_setting_ignore_callback_will_include_oauth_callback_in_request_options
146
+ request_options = {}
147
+ @consumer.stubs(:request).returns(create_stub_http_response)
148
+
149
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
150
+
151
+ assert_equal 'oob', request_options[:oauth_callback]
152
+ end
153
+
154
+ def test_that_setting_ignore_callback_will_exclude_oauth_callback_in_request_options
155
+ request_options = { :exclude_callback=> true }
156
+ @consumer.stubs(:request).returns(create_stub_http_response)
157
+
158
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
159
+
160
+ assert_nil request_options[:oauth_callback]
161
+ end
162
+
163
+ private
164
+
165
+ def create_stub_http_response expected_body=nil
166
+ stub_http_response = stub
167
+ stub_http_response.stubs(:code).returns(200)
168
+ stub_http_response.stubs(:body).tap {|expectation| expectation.returns(expected_body) unless expected_body.nil? }
169
+ return stub_http_response
170
+ end
171
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ begin
4
+
5
+ require 'oauth/request_proxy/curb_request'
6
+ require 'curb'
7
+
8
+
9
+ class CurbRequestProxyTest < Test::Unit::TestCase
10
+
11
+ def test_that_proxy_simple_get_request_works
12
+ request = Curl::Easy.new('/test?key=value')
13
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test?key=value'})
14
+
15
+ expected_parameters = {'key' => ['value']}
16
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
17
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
18
+ end
19
+
20
+ def test_that_proxy_simple_post_request_works_with_arguments
21
+ request = Curl::Easy.new('/test')
22
+ params = {'key' => 'value'}
23
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test', :parameters => params})
24
+
25
+ expected_parameters = {'key' => 'value'}
26
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
27
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
28
+ end
29
+
30
+ def test_that_proxy_simple_post_request_works_with_form_data
31
+ request = Curl::Easy.new('/test')
32
+ request.post_body = 'key=value'
33
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
34
+
35
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test'})
36
+
37
+ expected_parameters = {'key' => 'value'}
38
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
39
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
40
+ end
41
+
42
+ def test_that_proxy_simple_put_request_works_with_arguments
43
+ request = Curl::Easy.new('/test')
44
+ params = {'key' => 'value'}
45
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test', :parameters => params})
46
+
47
+ expected_parameters = {'key' => 'value'}
48
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
49
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
50
+ end
51
+
52
+ def test_that_proxy_simple_put_request_works_with_form_data
53
+ request = Curl::Easy.new('/test')
54
+ request.post_body = 'key=value'
55
+
56
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test'})
57
+
58
+ expected_parameters = {}
59
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
60
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
61
+ end
62
+
63
+ def test_that_proxy_post_request_works_with_mixed_parameter_sources
64
+ request = Curl::Easy.new('/test?key=value')
65
+ request.post_body = 'key2=value2'
66
+ request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
67
+ request_proxy = OAuth::RequestProxy.proxy(request, {:uri => 'http://example.com/test?key=value', :parameters => {'key3' => 'value3'}})
68
+
69
+ expected_parameters = {'key' => ['value'], 'key2' => 'value2', 'key3' => 'value3'}
70
+ assert_equal expected_parameters, request_proxy.parameters_for_signature
71
+ assert_equal 'http://example.com/test', request_proxy.normalized_uri
72
+ end
73
+ end
74
+
75
+ rescue LoadError => e
76
+ warn "! problems loading curb, skipping these tests: #{e}"
77
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ begin
3
+
4
+ require 'oauth/client/em_http'
5
+
6
+ class EmHttpClientTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @consumer = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237')
10
+ @token = OAuth::Token.new('token_411a7f', '3196ffd991c8ebdb')
11
+ @request_uri = URI.parse('http://example.com/test?key=value')
12
+ @request_parameters = { 'key' => 'value' }
13
+ @nonce = 225579211881198842005988698334675835446
14
+ @timestamp = "1199645624"
15
+ # This is really unneeded I guess.
16
+ @http = Net::HTTP.new(@request_uri.host, @request_uri.port)
17
+ end
18
+
19
+ def test_that_using_auth_headers_on_get_requests_works
20
+ request = create_client
21
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
22
+
23
+ assert_equal 'GET', request.method
24
+ assert_equal '/test', request.normalize_uri.path
25
+ assert_equal "key=value", request.normalize_uri.query
26
+ assert_equal_authz_headers "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"1oO2izFav1GP4kEH2EskwXkCRFg%3D\", oauth_version=\"1.0\"", authz_header(request)
27
+ end
28
+
29
+ def test_that_using_auth_headers_on_get_requests_works_with_plaintext
30
+ require 'oauth/signature/plaintext'
31
+ c = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237',{
32
+ :signature_method => 'PLAINTEXT'
33
+ })
34
+ request = create_client
35
+ request.oauth!(@http, c, @token, {:nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
36
+
37
+ assert_equal 'GET', request.method
38
+ assert_equal '/test', request.normalize_uri.path
39
+ assert_equal "key=value", request.normalize_uri.query
40
+ assert_equal_authz_headers "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"PLAINTEXT\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"5888bf0345e5d237%263196ffd991c8ebdb\", oauth_version=\"1.0\"", authz_header(request)
41
+ end
42
+
43
+ def test_that_using_auth_headers_on_post_requests_works
44
+ request = create_client(:uri => "http://example.com/test", :method => "POST", :body => @request_parameters, :head => {"Content-Type" => "application/x-www-form-urlencoded"})
45
+ request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
46
+
47
+ assert_equal 'POST', request.method
48
+ assert_equal '/test', request.uri.path
49
+ assert_equal 'key=value', request.normalize_body
50
+ assert_equal_authz_headers "OAuth oauth_nonce=\"225579211881198842005988698334675835446\", oauth_signature_method=\"HMAC-SHA1\", oauth_token=\"token_411a7f\", oauth_timestamp=\"1199645624\", oauth_consumer_key=\"consumer_key_86cad9\", oauth_signature=\"26g7wHTtNO6ZWJaLltcueppHYiI%3D\", oauth_version=\"1.0\"", authz_header(request)
51
+ end
52
+
53
+ protected
54
+
55
+ def create_client(options = {})
56
+ method = options.delete(:method) || "GET"
57
+ uri = options.delete(:uri) || @request_uri.to_s
58
+ client = EventMachine::HttpClient.new("")
59
+ client.uri = URI.parse(uri)
60
+ client.method = method.to_s.upcase
61
+ client.options = options
62
+ client
63
+ end
64
+
65
+ def authz_header(request)
66
+ headers = request.options[:head] || {}
67
+ headers['Authorization'].to_s
68
+ end
69
+
70
+ def assert_equal_authz_headers(expected, actual)
71
+ assert !actual.nil?
72
+ assert_equal expected[0,6], actual[0, 6]
73
+ assert_equal expected[6..1].split(', ').sort, actual[6..1].split(', ').sort
74
+ end
75
+
76
+ end
77
+
78
+ rescue LoadError => e
79
+ warn "! problem loading em-http, skipping these tests: #{e}"
80
+ end
@@ -0,0 +1,115 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ begin
4
+
5
+ require 'em-http'
6
+ require 'oauth/request_proxy/em_http_request'
7
+
8
+
9
+ class EmHttpRequestProxyTest < Test::Unit::TestCase
10
+
11
+ def test_request_proxy_works_with_simple_request
12
+ proxy = create_request_proxy
13
+ assert_equal({}, proxy.parameters)
14
+ end
15
+
16
+ def test_request_proxy_works_with_query_string_params
17
+ assert_equal({"name" => ["Fred"]}, create_request_proxy(:query => "name=Fred").parameters)
18
+ assert_equal({"name" => ["Fred"]}, create_request_proxy(:query => {:name => "Fred"}).parameters)
19
+ proxy = create_request_proxy(:query => {:name => "Fred"}, :uri => "http://example.com/?awesome=true")
20
+ assert_equal({"name" => ["Fred"], "awesome" => ["true"]}, proxy.parameters)
21
+ end
22
+
23
+ def test_request_proxy_works_with_post_body_params_with_correct_content_type
24
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "POST"
25
+ assert_equal({}, proxy.parameters)
26
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "POST", :body => "a=1"
27
+ assert_equal({"a" => ["1"]}, proxy.parameters)
28
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "POST", :body => {"a" => 1}
29
+ assert_equal({"a" => ["1"]}, proxy.parameters)
30
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "PUT"
31
+ assert_equal({}, proxy.parameters)
32
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "PUT", :body => "a=1"
33
+ assert_equal({"a" => ["1"]}, proxy.parameters)
34
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "PUT", :body => {"a" => 1}
35
+ assert_equal({"a" => ["1"]}, proxy.parameters)
36
+ end
37
+
38
+ def test_request_proxy_ignore_post_body_with_invalid_content_type
39
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "POST"
40
+ assert_equal({}, proxy.parameters)
41
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "POST", :body => "a=1"
42
+ assert_equal({}, proxy.parameters)
43
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "POST", :body => {"a" => 1}
44
+ assert_equal({}, proxy.parameters)
45
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "PUT"
46
+ assert_equal({}, proxy.parameters)
47
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "PUT", :body => "a=1"
48
+ assert_equal({}, proxy.parameters)
49
+ proxy = create_request_proxy :head => {'Content-Type' => 'text/plain'}, :method => "PUT", :body => {"a" => 1}
50
+ assert_equal({}, proxy.parameters)
51
+ end
52
+
53
+ def test_request_proxy_ignores_post_body_with_invalid_method
54
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "DELETE"
55
+ assert_equal({}, proxy.parameters)
56
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "DELETE", :body => "a=1"
57
+ assert_equal({}, proxy.parameters)
58
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "DELETE", :body => {"a" => 1}
59
+ assert_equal({}, proxy.parameters)
60
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "GET"
61
+ assert_equal({}, proxy.parameters)
62
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "GET", :body => "a=1"
63
+ assert_equal({}, proxy.parameters)
64
+ proxy = create_request_proxy :head => {'Content-Type' => 'application/x-www-form-urlencoded'}, :method => "GET", :body => {"a" => 1}
65
+ assert_equal({}, proxy.parameters)
66
+ end
67
+
68
+ def test_request_proxy_works_with_argument_params
69
+ assert_equal({"a" => ["1"]}, create_request_proxy(:proxy_options => {:parameters => {"a" => "1"}}).parameters)
70
+ end
71
+
72
+ def test_request_proxy_works_with_mixed_params
73
+ proxy = create_request_proxy(:proxy_options => {:parameters => {"a" => "1"}},:query => {"c" => "1"}, :uri => "http://example.com/test?b=1")
74
+ assert_equal({"a" => ["1"], "b" => ["1"], "c" => ["1"]}, proxy.parameters)
75
+ proxy = create_request_proxy(:proxy_options => {:parameters => {"a" => "1"}}, :body => {"b" => "1"}, :query => {"c" => "1"},
76
+ :uri => "http://example.com/test?d=1", :method => "POST", :head => {"Content-Type" => "application/x-www-form-urlencoded"})
77
+ assert_equal({"a" => ["1"], "b" => ["1"], "c" => ["1"], "d" => ["1"]}, proxy.parameters)
78
+ end
79
+
80
+ def test_request_has_the_correct_uri
81
+ assert_equal "http://example.com/", create_request_proxy.uri
82
+ assert_equal "http://example.com/?a=1", create_request_proxy(:query => "a=1").uri
83
+ assert_equal "http://example.com/?a=1", create_request_proxy(:query => {"a" => "1"}).uri
84
+
85
+ end
86
+
87
+ def test_request_proxy_has_correct_method
88
+ assert_equal "GET", create_request_proxy(:method => "GET").method
89
+ assert_equal "PUT", create_request_proxy(:method => "PUT").method
90
+ assert_equal "POST", create_request_proxy(:method => "POST").method
91
+ assert_equal "DELETE", create_request_proxy(:method => "DELETE").method
92
+ end
93
+
94
+ protected
95
+
96
+ def create_client(options = {})
97
+ method = options.delete(:method) || "GET"
98
+ uri = options.delete(:uri) || "http://example.com/"
99
+ client = EventMachine::HttpClient.new("")
100
+ client.uri = URI.parse(uri)
101
+ client.method = method.to_s.upcase
102
+ client.options = options
103
+ client
104
+ end
105
+
106
+ def create_request_proxy(opts = {})
107
+ arguments = opts.delete(:proxy_options) || {}
108
+ OAuth::RequestProxy.proxy(create_client(opts), arguments)
109
+ end
110
+
111
+ end
112
+
113
+ rescue LoadError => e
114
+ warn "! problem loading em-http, skipping these tests: #{e}"
115
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH << File.dirname(__FILE__) + '/../lib/'
5
+ require 'oauth'
6
+ require 'mocha'
7
+ require 'stringio'
8
+ require 'webmock/test_unit'
9
+
10
+ class Test::Unit::TestCase
11
+ def assert_matching_headers(expected, actual)
12
+ # transform into sorted arrays
13
+ auth_intro, auth_params = actual.split(' ', 2)
14
+ assert_equal auth_intro, 'OAuth'
15
+ expected = expected.split(/(,|\s)/).reject {|v| v == '' || v =~ /^[\,\s]+/}.sort
16
+ auth_params = auth_params.split(/(,|\s)/).reject {|v| v == '' || v =~ /^[\,\s]+/}.sort
17
+ assert_equal expected, auth_params
18
+ end
19
+
20
+ def stub_test_ie
21
+ stub_request(:any, "http://term.ie/oauth/example/request_token.php").to_return(:body => "oauth_token=requestkey&oauth_token_secret=requestsecret")
22
+ stub_request(:post, "http://term.ie/oauth/example/access_token.php").to_return(:body => "oauth_token=accesskey&oauth_token_secret=accesssecret")
23
+ stub_request(:get, %r{http://term\.ie/oauth/example/echo_api\.php\?.+}).to_return(lambda {|request| {:body => request.uri.query}})
24
+ stub_request(:post, "http://term.ie/oauth/example/echo_api.php").to_return(lambda {|request| {:body => request.body}})
25
+ end
26
+ end