-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
test_em_http_client.rb
79 lines (64 loc) · 3.59 KB
/
test_em_http_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require File.expand_path('../../test_helper_units', __FILE__)
begin
require 'oauth/client/em_http'
class EmHttpClientTest < Minitest::Test
def setup
@consumer = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237')
@token = OAuth::Token.new('token_411a7f', '3196ffd991c8ebdb')
@request_uri = URI.parse('http://example.com/test?key=value')
@request_parameters = { 'key' => 'value' }
@nonce = 225579211881198842005988698334675835446
@timestamp = "1199645624"
# This is really unneeded I guess.
@http = Net::HTTP.new(@request_uri.host, @request_uri.port)
end
def test_that_using_auth_headers_on_get_requests_works
request = create_client
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
assert_equal 'GET', request.method
assert_equal '/test', request.normalize_uri.path
assert_equal "key=value", request.normalize_uri.query
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)
end
def test_that_using_auth_headers_on_get_requests_works_with_plaintext
c = OAuth::Consumer.new('consumer_key_86cad9', '5888bf0345e5d237',{
:signature_method => 'PLAINTEXT'
})
request = create_client
request.oauth!(@http, c, @token, {:nonce => @nonce, :timestamp => @timestamp, :signature_method => 'PLAINTEXT'})
assert_equal 'GET', request.method
assert_equal '/test', request.normalize_uri.path
assert_equal "key=value", request.normalize_uri.query
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)
end
def test_that_using_auth_headers_on_post_requests_works
request = create_client(:uri => "http://example.com/test", :method => "POST", :body => @request_parameters, :head => {"Content-Type" => "application/x-www-form-urlencoded"})
request.oauth!(@http, @consumer, @token, {:nonce => @nonce, :timestamp => @timestamp})
assert_equal 'POST', request.method
assert_equal '/test', request.uri.path
assert_equal 'key=value', request.normalize_body
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)
end
protected
def create_client(options = {})
method = options.delete(:method) || "GET"
uri = options.delete(:uri) || @request_uri.to_s
client = EventMachine::HttpClient.new("")
client.uri = URI.parse(uri)
client.method = method.to_s.upcase
client.options = options
client
end
def authz_header(request)
headers = request.options[:head] || {}
headers['Authorization'].to_s
end
def assert_equal_authz_headers(expected, actual)
assert !actual.nil?
assert_equal expected[0,6], actual[0, 6]
assert_equal expected[6..1].split(', ').sort, actual[6..1].split(', ').sort
end
end
rescue LoadError => e
warn "! problem loading em-http, skipping these tests: #{e}"
end