typhoeus 0.1.14 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/typhoeus/easy.rb +26 -2
- data/spec/servers/app.rb +23 -0
- data/spec/typhoeus/easy_spec.rb +29 -0
- metadata +1 -1
data/lib/typhoeus/easy.rb
CHANGED
@@ -17,11 +17,22 @@ module Typhoeus
|
|
17
17
|
:CURLOPT_NOSIGNAL => 99,
|
18
18
|
:CURLOPT_HTTPHEADER => 10023,
|
19
19
|
:CURLOPT_FOLLOWLOCATION => 52,
|
20
|
-
:CURLOPT_MAXREDIRS => 68
|
20
|
+
:CURLOPT_MAXREDIRS => 68,
|
21
|
+
:CURLOPT_HTTPAUTH => 107,
|
22
|
+
:CURLOPT_USERPWD => 10000 + 5,
|
23
|
+
:CURLOPT_VERBOSE => 41
|
21
24
|
}
|
22
25
|
INFO_VALUES = {
|
23
26
|
:CURLINFO_RESPONSE_CODE => 2097154,
|
24
|
-
:CURLINFO_TOTAL_TIME => 3145731
|
27
|
+
:CURLINFO_TOTAL_TIME => 3145731,
|
28
|
+
:CURLINFO_HTTPAUTH_AVAIL => 0x200000 + 23
|
29
|
+
}
|
30
|
+
AUTH_TYPES = {
|
31
|
+
:CURLAUTH_BASIC => 1,
|
32
|
+
:CURLAUTH_DIGEST => 2,
|
33
|
+
:CURLAUTH_GSSNEGOTIATE => 4,
|
34
|
+
:CURLAUTH_NTLM => 8,
|
35
|
+
:CURLAUTH_DIGEST_IE => 16
|
25
36
|
}
|
26
37
|
|
27
38
|
def initialize
|
@@ -34,6 +45,19 @@ module Typhoeus
|
|
34
45
|
@headers = hash
|
35
46
|
end
|
36
47
|
|
48
|
+
def auth=(authinfo)
|
49
|
+
set_option(OPTION_VALUES[:CURLOPT_USERPWD], "#{authinfo[:username]}:#{authinfo[:password]}")
|
50
|
+
set_option(OPTION_VALUES[:CURLOPT_HTTPAUTH], authinfo[:method]) if authinfo[:method]
|
51
|
+
end
|
52
|
+
|
53
|
+
def auth_methods
|
54
|
+
get_info_long(INFO_VALUES[:CURLINFO_HTTPAUTH_AVAIL])
|
55
|
+
end
|
56
|
+
|
57
|
+
def verbose=(boolean)
|
58
|
+
set_option(OPTION_VALUES[:CURLOPT_VERBOSE], !!boolean ? 1 : 0)
|
59
|
+
end
|
60
|
+
|
37
61
|
def total_time_taken
|
38
62
|
get_info_double(INFO_VALUES[:CURLINFO_TOTAL_TIME])
|
39
63
|
end
|
data/spec/servers/app.rb
CHANGED
@@ -25,6 +25,29 @@ get '/bad_redirect' do
|
|
25
25
|
redirect '/bad_redirect'
|
26
26
|
end
|
27
27
|
|
28
|
+
get '/auth_basic/:username/:password' do
|
29
|
+
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
30
|
+
# Check that we've got a basic auth, and that it's credentials match the ones
|
31
|
+
# provided in the request
|
32
|
+
if @auth.provided? && @auth.basic? && @auth.credentials == [ params[:username], params[:password] ]
|
33
|
+
# auth is valid - confirm it
|
34
|
+
true
|
35
|
+
else
|
36
|
+
# invalid auth - request the authentication
|
37
|
+
response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
|
38
|
+
throw(:halt, [401, "Not authorized\n"])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
get '/auth_ntlm' do
|
43
|
+
# we're just checking for the existence if NTLM auth header here. It's validation
|
44
|
+
# is too troublesome and really doesn't bother is much, it's up to libcurl to make
|
45
|
+
# it valid
|
46
|
+
is_ntlm_auth = /^NTLM/ =~ request.env['HTTP_AUTHORIZATION']
|
47
|
+
true if is_ntlm_auth
|
48
|
+
throw(:halt, [401, "Not authorized\n"]) if !is_ntlm_auth
|
49
|
+
end
|
50
|
+
|
28
51
|
get '/**' do
|
29
52
|
sleep params["delay"].to_i if params.has_key?("delay")
|
30
53
|
request.env.merge!(:body => request.body.read).to_json
|
data/spec/typhoeus/easy_spec.rb
CHANGED
@@ -61,6 +61,35 @@ describe Typhoeus::Easy do
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
+
describe "authentication" do
|
65
|
+
it "should allow to set username and password" do
|
66
|
+
e = Typhoeus::Easy.new
|
67
|
+
username, password = 'foo', 'bar'
|
68
|
+
e.auth = { :username => username, :password => password }
|
69
|
+
e.url = "http://localhost:3001/auth_basic/#{username}/#{password}"
|
70
|
+
e.method = :get
|
71
|
+
e.perform
|
72
|
+
e.response_code.should == 200
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should allow to query auth methods support by the server" do
|
76
|
+
e = Typhoeus::Easy.new
|
77
|
+
e.url = "http://localhost:3001/auth_basic/foo/bar"
|
78
|
+
e.method = :get
|
79
|
+
e.perform
|
80
|
+
e.auth_methods.should == Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_BASIC]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should allow to set authentication method" do
|
84
|
+
e = Typhoeus::Easy.new
|
85
|
+
e.auth = { :username => 'username', :password => 'password', :method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM] }
|
86
|
+
e.url = "http://localhost:3001/auth_ntlm"
|
87
|
+
e.method = :get
|
88
|
+
e.perform
|
89
|
+
e.response_code.should == 200
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
64
93
|
describe "get" do
|
65
94
|
it "should perform a get" do
|
66
95
|
easy = Typhoeus::Easy.new
|