oauth_weshays 0.4.8.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +15 -0
  2. data/.gemtest +0 -0
  3. data/Gemfile +16 -0
  4. data/Gemfile.lock +47 -0
  5. data/HISTORY +173 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +75 -0
  8. data/Rakefile +37 -0
  9. data/TODO +32 -0
  10. data/bin/oauth +5 -0
  11. data/examples/yql.rb +44 -0
  12. data/lib/digest/hmac.rb +104 -0
  13. data/lib/oauth/cli.rb +378 -0
  14. data/lib/oauth/client/action_controller_request.rb +65 -0
  15. data/lib/oauth/client/em_http.rb +120 -0
  16. data/lib/oauth/client/helper.rb +91 -0
  17. data/lib/oauth/client/net_http.rb +120 -0
  18. data/lib/oauth/client.rb +4 -0
  19. data/lib/oauth/consumer.rb +389 -0
  20. data/lib/oauth/core_ext.rb +31 -0
  21. data/lib/oauth/errors/error.rb +4 -0
  22. data/lib/oauth/errors/problem.rb +14 -0
  23. data/lib/oauth/errors/unauthorized.rb +12 -0
  24. data/lib/oauth/errors.rb +3 -0
  25. data/lib/oauth/helper.rb +109 -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/action_controller_request.rb +62 -0
  29. data/lib/oauth/request_proxy/base.rb +174 -0
  30. data/lib/oauth/request_proxy/curb_request.rb +55 -0
  31. data/lib/oauth/request_proxy/em_http_request.rb +66 -0
  32. data/lib/oauth/request_proxy/jabber_request.rb +41 -0
  33. data/lib/oauth/request_proxy/mock_request.rb +44 -0
  34. data/lib/oauth/request_proxy/net_http.rb +73 -0
  35. data/lib/oauth/request_proxy/rack_request.rb +44 -0
  36. data/lib/oauth/request_proxy/typhoeus_request.rb +53 -0
  37. data/lib/oauth/request_proxy.rb +24 -0
  38. data/lib/oauth/server.rb +66 -0
  39. data/lib/oauth/signature/base.rb +110 -0
  40. data/lib/oauth/signature/hmac/base.rb +15 -0
  41. data/lib/oauth/signature/hmac/md5.rb +8 -0
  42. data/lib/oauth/signature/hmac/rmd160.rb +8 -0
  43. data/lib/oauth/signature/hmac/sha1.rb +9 -0
  44. data/lib/oauth/signature/hmac/sha2.rb +8 -0
  45. data/lib/oauth/signature/md5.rb +13 -0
  46. data/lib/oauth/signature/plaintext.rb +23 -0
  47. data/lib/oauth/signature/rsa/sha1.rb +46 -0
  48. data/lib/oauth/signature/sha1.rb +13 -0
  49. data/lib/oauth/signature.rb +45 -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/lib/oauth.rb +13 -0
  57. data/oauth.gemspec +148 -0
  58. data/tasks/deployment.rake +34 -0
  59. data/tasks/environment.rake +7 -0
  60. data/tasks/website.rake +17 -0
  61. data/test/cases/oauth_case.rb +19 -0
  62. data/test/cases/spec/1_0-final/test_construct_request_url.rb +62 -0
  63. data/test/cases/spec/1_0-final/test_normalize_request_parameters.rb +88 -0
  64. data/test/cases/spec/1_0-final/test_parameter_encodings.rb +86 -0
  65. data/test/cases/spec/1_0-final/test_signature_base_strings.rb +77 -0
  66. data/test/integration/consumer_test.rb +307 -0
  67. data/test/keys/rsa.cert +11 -0
  68. data/test/keys/rsa.pem +16 -0
  69. data/test/test_access_token.rb +26 -0
  70. data/test/test_action_controller_request_proxy.rb +133 -0
  71. data/test/test_consumer.rb +220 -0
  72. data/test/test_curb_request_proxy.rb +77 -0
  73. data/test/test_em_http_client.rb +80 -0
  74. data/test/test_em_http_request_proxy.rb +115 -0
  75. data/test/test_helper.rb +28 -0
  76. data/test/test_hmac_sha1.rb +20 -0
  77. data/test/test_net_http_client.rb +292 -0
  78. data/test/test_net_http_request_proxy.rb +72 -0
  79. data/test/test_oauth_helper.rb +94 -0
  80. data/test/test_rack_request_proxy.rb +40 -0
  81. data/test/test_request_token.rb +51 -0
  82. data/test/test_rsa_sha1.rb +59 -0
  83. data/test/test_server.rb +40 -0
  84. data/test/test_signature.rb +22 -0
  85. data/test/test_signature_base.rb +32 -0
  86. data/test/test_signature_plain_text.rb +31 -0
  87. data/test/test_token.rb +14 -0
  88. data/test/test_typhoeus_request_proxy.rb +80 -0
  89. metadata +252 -0
@@ -0,0 +1,220 @@
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_site_without_path
65
+ @consumer=OAuth::Consumer.new(
66
+ "key",
67
+ "secret",
68
+ {
69
+ :site=>"http://twitter.com"
70
+ })
71
+ request = stub(:oauth! => nil)
72
+ http = stub(:request => stub(:to_hash => {}))
73
+ Net::HTTP::Get.expects(:new).with('/people', {}).returns(request)
74
+ @consumer.expects(:create_http).returns(http)
75
+ @consumer.request(:get, '/people', nil, {})
76
+ end
77
+
78
+ def test_site_with_path
79
+ @consumer=OAuth::Consumer.new(
80
+ "key",
81
+ "secret",
82
+ {
83
+ :site=>"http://identi.ca/api"
84
+ })
85
+ request = stub(:oauth! => nil)
86
+ http = stub(:request => stub(:to_hash => {}))
87
+ Net::HTTP::Get.expects(:new).with('/api/people', {}).returns(request)
88
+ @consumer.expects(:create_http).returns(http)
89
+ @consumer.request(:get, '/people', nil, {})
90
+ end
91
+
92
+ def test_post_of_nested_params_maintains_nesting
93
+ @consumer=OAuth::Consumer.new(
94
+ "key",
95
+ "secret",
96
+ {
97
+ :site=>"http://twitter.com"
98
+ })
99
+ request = @consumer.create_signed_request(
100
+ :post,
101
+ '/people',
102
+ nil,
103
+ {},
104
+ {
105
+ :key => {
106
+ :subkey => 'value'
107
+ }
108
+ })
109
+ assert_equal 'key%5Bsubkey%5D=value', request.body
110
+ assert_equal request.content_type, 'application/x-www-form-urlencoded'
111
+ end
112
+
113
+ def test_override_paths
114
+ @consumer=OAuth::Consumer.new(
115
+ "key",
116
+ "secret",
117
+ {
118
+ :site=>"http://twitter.com",
119
+ :request_token_url=>"http://oauth.twitter.com/request_token",
120
+ :access_token_url=>"http://oauth.twitter.com/access_token",
121
+ :authorize_url=>"http://site.twitter.com/authorize"
122
+ })
123
+ assert_equal "key",@consumer.key
124
+ assert_equal "secret",@consumer.secret
125
+ assert_equal "http://twitter.com",@consumer.site
126
+ assert_equal "/oauth/request_token",@consumer.request_token_path
127
+ assert_equal "/oauth/access_token",@consumer.access_token_path
128
+ assert_equal "http://oauth.twitter.com/request_token",@consumer.request_token_url
129
+ assert_equal "http://oauth.twitter.com/access_token",@consumer.access_token_url
130
+ assert_equal "http://site.twitter.com/authorize",@consumer.authorize_url
131
+ assert_equal :header,@consumer.scheme
132
+ assert_equal :post,@consumer.http_method
133
+ end
134
+
135
+ def test_that_token_response_should_be_uri_parameter_format_as_default
136
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
137
+
138
+ hash = @consumer.token_request(:get, "")
139
+
140
+ assert_equal "token", hash[:oauth_token]
141
+ assert_equal "secret", hash[:oauth_token_secret]
142
+ end
143
+
144
+ def test_can_provided_a_block_to_interpret_token_response
145
+ @consumer.expects(:request).returns(create_stub_http_response)
146
+
147
+ hash = @consumer.token_request(:get, '') {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
148
+
149
+ assert_equal 'token', hash[:oauth_token]
150
+ assert_equal 'secret', hash[:oauth_token_secret]
151
+ end
152
+
153
+ def test_token_request_follows_redirect
154
+ redirect_url = @request_uri.clone
155
+ redirect_url.path = "/oauth/example/request_token_redirect.php"
156
+ stub_request(:get, /.*#{@request_uri.path}/).to_return(:status => 301, :headers => {'Location' => redirect_url.to_s})
157
+ stub_request(:get, /.*#{redirect_url.path}/).to_return(:body => "oauth_token=token&oauth_token_secret=secret")
158
+
159
+ hash = @consumer.token_request(:get, @request_uri.path) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
160
+
161
+ assert_equal 'token', hash[:oauth_token]
162
+ assert_equal 'secret', hash[:oauth_token_secret]
163
+ end
164
+
165
+ def test_that_can_provide_a_block_to_interpret_a_request_token_response
166
+ @consumer.expects(:request).returns(create_stub_http_response)
167
+
168
+ token = @consumer.get_request_token {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
169
+
170
+ assert_equal 'token', token.token
171
+ assert_equal 'secret', token.secret
172
+ end
173
+
174
+ def test_that_block_is_not_mandatory_for_getting_an_access_token
175
+ stub_token = mock
176
+ @consumer.expects(:request).returns(create_stub_http_response("oauth_token=token&oauth_token_secret=secret"))
177
+
178
+ token = @consumer.get_access_token(stub_token)
179
+
180
+ assert_equal 'token', token.token
181
+ assert_equal 'secret', token.secret
182
+ end
183
+
184
+ def test_that_can_provide_a_block_to_interpret_an_access_token_response
185
+ stub_token = mock
186
+ @consumer.expects(:request).returns(create_stub_http_response)
187
+
188
+ token = @consumer.get_access_token(stub_token) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
189
+
190
+ assert_equal 'token', token.token
191
+ assert_equal 'secret', token.secret
192
+ end
193
+
194
+ def test_that_not_setting_ignore_callback_will_include_oauth_callback_in_request_options
195
+ request_options = {}
196
+ @consumer.stubs(:request).returns(create_stub_http_response)
197
+
198
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
199
+
200
+ assert_equal 'oob', request_options[:oauth_callback]
201
+ end
202
+
203
+ def test_that_setting_ignore_callback_will_exclude_oauth_callback_in_request_options
204
+ request_options = { :exclude_callback=> true }
205
+ @consumer.stubs(:request).returns(create_stub_http_response)
206
+
207
+ @consumer.get_request_token(request_options) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
208
+
209
+ assert_nil request_options[:oauth_callback]
210
+ end
211
+
212
+ private
213
+
214
+ def create_stub_http_response expected_body=nil
215
+ stub_http_response = stub
216
+ stub_http_response.stubs(:code).returns(200)
217
+ stub_http_response.stubs(:body).tap {|expectation| expectation.returns(expected_body) unless expected_body.nil? }
218
+ return stub_http_response
219
+ end
220
+ 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,28 @@
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'
9
+
10
+ class Test::Unit::TestCase
11
+ include WebMock::API
12
+
13
+ def assert_matching_headers(expected, actual)
14
+ # transform into sorted arrays
15
+ auth_intro, auth_params = actual.split(' ', 2)
16
+ assert_equal auth_intro, 'OAuth'
17
+ expected = expected.split(/(,|\s)/).reject {|v| v == '' || v =~ /^[\,\s]+/}.sort
18
+ auth_params = auth_params.split(/(,|\s)/).reject {|v| v == '' || v =~ /^[\,\s]+/}.sort
19
+ assert_equal expected, auth_params
20
+ end
21
+
22
+ def stub_test_ie
23
+ stub_request(:any, "http://term.ie/oauth/example/request_token.php").to_return(:body => "oauth_token=requestkey&oauth_token_secret=requestsecret")
24
+ stub_request(:post, "http://term.ie/oauth/example/access_token.php").to_return(:body => "oauth_token=accesskey&oauth_token_secret=accesssecret")
25
+ stub_request(:get, %r{http://term\.ie/oauth/example/echo_api\.php\?.+}).to_return(lambda {|request| {:body => request.uri.query}})
26
+ stub_request(:post, "http://term.ie/oauth/example/echo_api.php").to_return(lambda {|request| {:body => request.body}})
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class TestSignatureHmacSha1 < Test::Unit::TestCase
4
+ def test_that_hmac_sha1_implements_hmac_sha1
5
+ assert OAuth::Signature.available_methods.include?('hmac-sha1')
6
+ end
7
+
8
+ def test_that_get_request_from_oauth_test_cases_produces_matching_signature
9
+ request = Net::HTTP::Get.new('/photos?file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1')
10
+
11
+ consumer = OAuth::Consumer.new('dpf43f3p2l4k3l03', 'kd94hf93k423kf44')
12
+ token = OAuth::Token.new('nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00')
13
+
14
+ signature = OAuth::Signature.sign(request, { :consumer => consumer,
15
+ :token => token,
16
+ :uri => 'http://photos.example.net/photos' } )
17
+
18
+ assert_equal 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=', signature
19
+ end
20
+ end