Skip to content

Commit 172b2a3

Browse files
authored
🔀 Merge pull request #176 from oauth-xx/depfu/update/em-http-request-1.1.7
Upgrade em-http-request to version 1.1.7
2 parents 56a189b + 276733a commit 172b2a3

File tree

5 files changed

+134
-137
lines changed

5 files changed

+134
-137
lines changed

‎lib/oauth/client/em_http.rb

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -4,116 +4,116 @@
44

55
# Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient
66
# instance. This is purely syntactic sugar.
7-
class EventMachine::HttpClient
7+
module EventMachine
8+
class HttpClient
9+
attr_reader :oauth_helper
810

9-
attr_reader :oauth_helper
11+
# Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting
12+
# this may add a header, additional query string parameters, or additional POST body parameters.
13+
# The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
14+
# header.
15+
#
16+
# * http - Configured Net::HTTP instance, ignored in this scenario except for getting host.
17+
# * consumer - OAuth::Consumer instance
18+
# * token - OAuth::Token instance
19+
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
20+
# +signature_method+, +nonce+, +timestamp+)
21+
#
22+
# This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version.
23+
#
24+
# See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1]
25+
def oauth!(http, consumer = nil, token = nil, options = {})
26+
options = { :request_uri => normalized_oauth_uri(http),
27+
:consumer => consumer,
28+
:token => token,
29+
:scheme => 'header',
30+
:signature_method => nil,
31+
:nonce => nil,
32+
:timestamp => nil }.merge(options)
1033

11-
# Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting
12-
# this may add a header, additional query string parameters, or additional POST body parameters.
13-
# The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
14-
# header.
15-
#
16-
# * http - Configured Net::HTTP instance, ignored in this scenario except for getting host.
17-
# * consumer - OAuth::Consumer instance
18-
# * token - OAuth::Token instance
19-
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
20-
# +signature_method+, +nonce+, +timestamp+)
21-
#
22-
# This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version.
23-
#
24-
# See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1]
25-
def oauth!(http, consumer = nil, token = nil, options = {})
26-
options = { :request_uri => normalized_oauth_uri(http),
27-
:consumer => consumer,
28-
:token => token,
29-
:scheme => 'header',
30-
:signature_method => nil,
31-
:nonce => nil,
32-
:timestamp => nil }.merge(options)
33-
34-
@oauth_helper = OAuth::Client::Helper.new(self, options)
35-
self.__send__(:"set_oauth_#{options[:scheme]}")
36-
end
34+
@oauth_helper = OAuth::Client::Helper.new(self, options)
35+
self.__send__(:"set_oauth_#{options[:scheme]}")
36+
end
3737

38-
# Create a string suitable for signing for an HTTP request. This process involves parameter
39-
# normalization as specified in the OAuth specification. The exact normalization also depends
40-
# on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request
41-
# itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
42-
# header.
43-
#
44-
# * http - Configured Net::HTTP instance
45-
# * consumer - OAuth::Consumer instance
46-
# * token - OAuth::Token instance
47-
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
48-
# +signature_method+, +nonce+, +timestamp+)
49-
#
50-
# See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
51-
def signature_base_string(http, consumer = nil, token = nil, options = {})
52-
options = { :request_uri => normalized_oauth_uri(http),
53-
:consumer => consumer,
54-
:token => token,
55-
:scheme => 'header',
56-
:signature_method => nil,
57-
:nonce => nil,
58-
:timestamp => nil }.merge(options)
38+
# Create a string suitable for signing for an HTTP request. This process involves parameter
39+
# normalization as specified in the OAuth specification. The exact normalization also depends
40+
# on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request
41+
# itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
42+
# header.
43+
#
44+
# * http - Configured Net::HTTP instance
45+
# * consumer - OAuth::Consumer instance
46+
# * token - OAuth::Token instance
47+
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
48+
# +signature_method+, +nonce+, +timestamp+)
49+
#
50+
# See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
51+
def signature_base_string(http, consumer = nil, token = nil, options = {})
52+
options = { :request_uri => normalized_oauth_uri(http),
53+
:consumer => consumer,
54+
:token => token,
55+
:scheme => 'header',
56+
:signature_method => nil,
57+
:nonce => nil,
58+
:timestamp => nil }.merge(options)
5959

60-
OAuth::Client::Helper.new(self, options).signature_base_string
61-
end
60+
OAuth::Client::Helper.new(self, options).signature_base_string
61+
end
6262

63-
# This code was lifted from the em-http-request because it was removed from
64-
# the gem June 19, 2010
65-
# see: http://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b
66-
def normalize_uri
67-
@normalized_uri ||= begin
68-
uri = @uri.dup
69-
encoded_query = encode_query(@uri, @options[:query])
70-
path, query = encoded_query.split("?", 2)
71-
uri.query = query unless encoded_query.empty?
72-
uri.path = path
73-
uri
63+
# This code was lifted from the em-http-request because it was removed from
64+
# the gem June 19, 2010
65+
# see: http://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b
66+
def normalize_uri
67+
@normalized_uri ||= begin
68+
uri = @conn.dup
69+
encoded_query = encode_query(@conn, @req[:query])
70+
path, query = encoded_query.split("?", 2)
71+
uri.query = query unless encoded_query.empty?
72+
uri.path = path
73+
uri
74+
end
7475
end
75-
end
7676

77-
protected
77+
protected
7878

79-
def combine_query(path, query, uri_query)
80-
combined_query = if query.kind_of?(Hash)
81-
query.map { |k, v| encode_param(k, v) }.join('&')
82-
else
83-
query.to_s
79+
def combine_query(path, query, uri_query)
80+
combined_query = if query.kind_of?(Hash)
81+
query.map { |k, v| encode_param(k, v) }.join('&')
82+
else
83+
query.to_s
84+
end
85+
if !uri_query.to_s.empty?
86+
combined_query = [combined_query, uri_query].reject {|part| part.empty?}.join("&")
87+
end
88+
combined_query.to_s.empty? ? path : "#{path}?#{combined_query}"
8489
end
85-
if !uri_query.to_s.empty?
86-
combined_query = [combined_query, uri_query].reject {|part| part.empty?}.join("&")
87-
end
88-
combined_query.to_s.empty? ? path : "#{path}?#{combined_query}"
89-
end
9090

91-
# Since we expect to get the host etc details from the http instance (...),
92-
# we create a fake url here. Surely this is a horrible, horrible idea?
93-
def normalized_oauth_uri(http)
94-
uri = URI.parse(normalize_uri.path)
95-
uri.host = http.address
96-
uri.port = http.port
91+
# Since we expect to get the host etc details from the http instance (...),
92+
# we create a fake url here. Surely this is a horrible, horrible idea?
93+
def normalized_oauth_uri(http)
94+
uri = URI.parse(normalize_uri.path)
95+
uri.host = http.address
96+
uri.port = http.port
9797

98-
if http.respond_to?(:use_ssl?) && http.use_ssl?
99-
uri.scheme = "https"
100-
else
101-
uri.scheme = "http"
98+
if http.respond_to?(:use_ssl?) && http.use_ssl?
99+
uri.scheme = "https"
100+
else
101+
uri.scheme = "http"
102+
end
103+
uri.to_s
102104
end
103-
uri.to_s
104-
end
105105

106-
def set_oauth_header
107-
headers = (self.options[:head] ||= {})
108-
headers['Authorization'] = @oauth_helper.header
109-
end
106+
def set_oauth_header
107+
self.req[:head] ||= {}
108+
self.req[:head].merge!('Authorization' => @oauth_helper.header)
109+
end
110110

111-
def set_oauth_body
112-
raise NotImplementedError, 'please use the set_oauth_header method instead'
113-
end
111+
def set_oauth_body
112+
raise NotImplementedError, 'please use the set_oauth_header method instead'
113+
end
114114

115-
def set_oauth_query_string
116-
raise NotImplementedError, 'please use the set_oauth_header method instead'
115+
def set_oauth_query_string
116+
raise NotImplementedError, 'please use the set_oauth_header method instead'
117+
end
117118
end
118-
119119
end

‎lib/oauth/request_proxy/em_http_request.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class HttpRequest < OAuth::RequestProxy::Base
1414
# Request in this con
1515

1616
def method
17-
request.method
17+
request.req[:method]
1818
end
1919

2020
def uri
21-
request.normalize_uri.to_s
21+
request.conn.normalize.to_s
2222
end
2323

2424
def parameters
@@ -36,14 +36,20 @@ def all_parameters
3636
end
3737

3838
def query_parameters
39-
CGI.parse(request.normalize_uri.query.to_s)
39+
quer = request.req[:query]
40+
hash_quer = if quer.respond_to?(:merge)
41+
quer
42+
else
43+
CGI.parse(quer.to_s)
44+
end
45+
CGI.parse(request.conn.query.to_s).merge(hash_quer)
4046
end
4147

4248
def post_parameters
43-
headers = request.options[:head] || {}
49+
headers = request.req[:head] || {}
4450
form_encoded = headers['Content-Type'].to_s.downcase.start_with?("application/x-www-form-urlencoded")
4551
if ['POST', 'PUT'].include?(method) && form_encoded
46-
CGI.parse(request.normalize_body.to_s)
52+
CGI.parse(request.normalize_body(request.req[:body]).to_s)
4753
else
4854
{}
4955
end
@@ -53,9 +59,9 @@ def merged_parameters(params, *extra_params)
5359
extra_params.compact.each do |params_pairs|
5460
params_pairs.each_pair do |key, value|
5561
if params.has_key?(key)
56-
params[key] += value
62+
params[key.to_s] += value
5763
else
58-
params[key] = [value].flatten
64+
params[key.to_s] = [value].flatten
5965
end
6066
end
6167
end

‎oauth.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Gem::Specification.new do |spec|
4242
spec.add_development_dependency("rack-test")
4343
spec.add_development_dependency("mocha", ">= 0.9.12", "<=1.1.0")
4444
spec.add_development_dependency("typhoeus", ">= 0.1.13")
45-
spec.add_development_dependency("em-http-request", "0.2.11")
45+
spec.add_development_dependency("em-http-request", "~> 1.1.7")
4646
spec.add_development_dependency("curb")
4747
spec.add_development_dependency("webmock", "< 2.0")
4848
spec.add_development_dependency("codeclimate-test-reporter")

‎test/units/test_em_http_client.rb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ def test_that_using_auth_headers_on_get_requests_works
2020
request = create_client
2121
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
2222

23-
assert_equal 'GET', request.method
23+
assert_equal 'GET', request.req[:method]
2424
assert_equal '/test', request.normalize_uri.path
2525
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)
26+
correct_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\""
27+
generated_headers = authz_header(request)
28+
assert_equal_authz_headers correct_headers, generated_headers
2729
end
2830

2931
def test_that_using_auth_headers_on_get_requests_works_with_plaintext
@@ -34,37 +36,32 @@ def test_that_using_auth_headers_on_get_requests_works_with_plaintext
3436
request = create_client
3537
request.oauth!(@http, c, @token, {:nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
3638

37-
assert_equal 'GET', request.method
38-
assert_equal '/test', request.normalize_uri.path
39-
assert_equal "key=value", request.normalize_uri.query
39+
assert_equal 'GET', request.req[:method]
40+
assert_equal '/test', request.conn.path
41+
assert_equal "key=value", request.conn.query
4042
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)
4143
end
4244

4345
def test_that_using_auth_headers_on_post_requests_works
4446
request = create_client(:uri => "http://example.com/test", :method => "POST", :body => @request_parameters, :head => {"Content-Type" => "application/x-www-form-urlencoded"})
4547
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
4648

47-
assert_equal 'POST', request.method
48-
assert_equal '/test', request.uri.path
49-
assert_equal 'key=value', request.normalize_body
49+
assert_equal 'POST', request.req[:method]
50+
assert_equal '/test', request.conn.path
5051
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)
52+
assert_equal 'key=value', request.normalize_body(request.req[:body])
5153
end
5254

5355
protected
5456

5557
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
58+
options[:method] = options.key?(:method) ? options[:method].upcase : "GET"
59+
uri = options.delete(:uri) || @request_uri.to_s
60+
EventMachine::HttpClient.new(URI.parse(uri), options)
6361
end
6462

6563
def authz_header(request)
66-
headers = request.options[:head] || {}
67-
headers['Authorization'].to_s
64+
request.req[:head]['Authorization']
6865
end
6966

7067
def assert_equal_authz_headers(expected, actual)

‎test/units/test_em_http_request_proxy.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
require 'em-http'
66
require 'oauth/request_proxy/em_http_request'
77

8-
98
class EmHttpRequestProxyTest < Minitest::Test
109

1110
def test_request_proxy_works_with_simple_request
@@ -70,7 +69,7 @@ def test_request_proxy_works_with_argument_params
7069
end
7170

7271
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")
72+
proxy = create_request_proxy(:proxy_options => {:parameters => {"a" => "1"}}, :query => {"c" => "1"}, :uri => "http://example.com/test?b=1")
7473
assert_equal({"a" => ["1"], "b" => ["1"], "c" => ["1"]}, proxy.parameters)
7574
proxy = create_request_proxy(:proxy_options => {:parameters => {"a" => "1"}}, :body => {"b" => "1"}, :query => {"c" => "1"},
7675
:uri => "http://example.com/test?d=1", :method => "POST", :head => {"Content-Type" => "application/x-www-form-urlencoded"})
@@ -79,28 +78,23 @@ def test_request_proxy_works_with_mixed_params
7978

8079
def test_request_has_the_correct_uri
8180
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-
81+
assert_equal "http://example.com/?a=1", create_request_proxy(:query => "a=1").request.normalize_uri.to_s
82+
assert_equal "http://example.com/?a=1", create_request_proxy(:query => {"a" => "1"}).request.normalize_uri.to_s
8583
end
8684

8785
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
86+
assert_equal "GET", create_request_proxy(:method => "GET").request.req[:method]
87+
assert_equal "PUT", create_request_proxy(:method => "PUT").request.req[:method]
88+
assert_equal "POST", create_request_proxy(:method => "POST").request.req[:method]
89+
assert_equal "DELETE", create_request_proxy(:method => "DELETE").request.req[:method]
9290
end
9391

9492
protected
9593

9694
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
95+
options[:method] = options.key?(:method) ? options[:method].upcase : "GET"
96+
uri = options.delete(:uri) || "http://example.com/"
97+
EventMachine::HttpClient.new(URI.parse(uri), options)
10498
end
10599

106100
def create_request_proxy(opts = {})

0 commit comments

Comments
 (0)