xenda-typhoeus 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.markdown +70 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +38 -0
- data/LICENSE +20 -0
- data/README.textile +397 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/benchmarks/profile.rb +25 -0
- data/benchmarks/vs_nethttp.rb +35 -0
- data/examples/file.rb +12 -0
- data/examples/times.rb +40 -0
- data/examples/twitter.rb +21 -0
- data/ext/typhoeus/.gitignore +7 -0
- data/ext/typhoeus/extconf.rb +65 -0
- data/ext/typhoeus/native.c +12 -0
- data/ext/typhoeus/native.h +22 -0
- data/ext/typhoeus/typhoeus_easy.c +232 -0
- data/ext/typhoeus/typhoeus_easy.h +20 -0
- data/ext/typhoeus/typhoeus_form.c +59 -0
- data/ext/typhoeus/typhoeus_form.h +13 -0
- data/ext/typhoeus/typhoeus_multi.c +217 -0
- data/ext/typhoeus/typhoeus_multi.h +16 -0
- data/lib/typhoeus.rb +58 -0
- data/lib/typhoeus/.gitignore +1 -0
- data/lib/typhoeus/easy.rb +379 -0
- data/lib/typhoeus/filter.rb +28 -0
- data/lib/typhoeus/form.rb +32 -0
- data/lib/typhoeus/hydra.rb +247 -0
- data/lib/typhoeus/hydra/callbacks.rb +24 -0
- data/lib/typhoeus/hydra/connect_options.rb +61 -0
- data/lib/typhoeus/hydra/stubbing.rb +52 -0
- data/lib/typhoeus/hydra_mock.rb +131 -0
- data/lib/typhoeus/multi.rb +37 -0
- data/lib/typhoeus/normalized_header_hash.rb +58 -0
- data/lib/typhoeus/remote.rb +306 -0
- data/lib/typhoeus/remote_method.rb +108 -0
- data/lib/typhoeus/remote_proxy_object.rb +50 -0
- data/lib/typhoeus/request.rb +203 -0
- data/lib/typhoeus/response.rb +91 -0
- data/lib/typhoeus/service.rb +20 -0
- data/lib/typhoeus/utils.rb +63 -0
- data/profilers/valgrind.rb +24 -0
- data/spec/fixtures/placeholder.gif +0 -0
- data/spec/fixtures/placeholder.txt +1 -0
- data/spec/fixtures/placeholder.ukn +0 -0
- data/spec/fixtures/result_set.xml +60 -0
- data/spec/servers/app.rb +94 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/typhoeus/easy_spec.rb +343 -0
- data/spec/typhoeus/filter_spec.rb +35 -0
- data/spec/typhoeus/form_spec.rb +117 -0
- data/spec/typhoeus/hydra_mock_spec.rb +300 -0
- data/spec/typhoeus/hydra_spec.rb +574 -0
- data/spec/typhoeus/multi_spec.rb +74 -0
- data/spec/typhoeus/normalized_header_hash_spec.rb +41 -0
- data/spec/typhoeus/remote_method_spec.rb +141 -0
- data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
- data/spec/typhoeus/remote_spec.rb +695 -0
- data/spec/typhoeus/request_spec.rb +300 -0
- data/spec/typhoeus/response_spec.rb +151 -0
- data/spec/typhoeus/utils_spec.rb +22 -0
- data/xenda-typhoeus.gemspec +139 -0
- metadata +203 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Service
|
3
|
+
def initialize(host, port)
|
4
|
+
@host = host
|
5
|
+
@port = port
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(resource, params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def put(resource, params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def post(resource, params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete(resource, params)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Typhoeus
|
4
|
+
module Utils
|
5
|
+
# Taken from Rack::Utils, 1.2.1 to remove Rack dependency.
|
6
|
+
def escape(s)
|
7
|
+
s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/u) {
|
8
|
+
'%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
|
9
|
+
}.tr(' ', '+')
|
10
|
+
end
|
11
|
+
module_function :escape
|
12
|
+
|
13
|
+
# Params are NOT escaped.
|
14
|
+
def traverse_params_hash(hash, result = nil, current_key = nil)
|
15
|
+
result ||= { :files => [], :params => [] }
|
16
|
+
|
17
|
+
hash.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
|
18
|
+
new_key = (current_key ? "#{current_key}[#{key}]" : key).to_s
|
19
|
+
case hash[key]
|
20
|
+
when Hash
|
21
|
+
traverse_params_hash(hash[key], result, new_key)
|
22
|
+
when Array
|
23
|
+
hash[key].each do |v|
|
24
|
+
result[:params] << [new_key, v.to_s]
|
25
|
+
end
|
26
|
+
when File, Tempfile
|
27
|
+
filename = File.basename(hash[key].path)
|
28
|
+
types = MIME::Types.type_for(filename)
|
29
|
+
result[:files] << [
|
30
|
+
new_key,
|
31
|
+
filename,
|
32
|
+
types.empty? ? 'application/octet-stream' : types[0].to_s,
|
33
|
+
File.expand_path(hash[key].path)
|
34
|
+
]
|
35
|
+
else
|
36
|
+
result[:params] << [new_key, hash[key].to_s]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
module_function :traverse_params_hash
|
42
|
+
|
43
|
+
def traversal_to_param_string(traversal, escape = true)
|
44
|
+
traversal[:params].collect { |param|
|
45
|
+
escape ? "#{Typhoeus::Utils.escape(param[0])}=#{Typhoeus::Utils.escape(param[1])}" : "#{param[0]}=#{param[1]}"
|
46
|
+
}.join('&')
|
47
|
+
end
|
48
|
+
module_function :traversal_to_param_string
|
49
|
+
|
50
|
+
# Return the bytesize of String; uses String#size under Ruby 1.8 and
|
51
|
+
# String#bytesize under 1.9.
|
52
|
+
if ''.respond_to?(:bytesize)
|
53
|
+
def bytesize(string)
|
54
|
+
string.bytesize
|
55
|
+
end
|
56
|
+
else
|
57
|
+
def bytesize(string)
|
58
|
+
string.size
|
59
|
+
end
|
60
|
+
end
|
61
|
+
module_function :bytesize
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# go to ext/typhoeus and run ruby extconf.rb && make before running
|
3
|
+
# this.
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../ext")
|
6
|
+
require File.dirname(__FILE__) + "/../lib/typhoeus"
|
7
|
+
|
8
|
+
klass = Class.new { include Typhoeus }
|
9
|
+
|
10
|
+
loops = ENV['LOOPS'].to_i
|
11
|
+
url = ARGV.first || (raise "requires URL!")
|
12
|
+
|
13
|
+
loops.times do |i|
|
14
|
+
puts "On loop #{i}" if i % 10 == 0
|
15
|
+
results = []
|
16
|
+
5.times do
|
17
|
+
results << klass.get(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
# fire requests
|
21
|
+
results[0].code
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "Ran #{loops} loops on #{url}!"
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
This file is used to test uploading.
|
File without changes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
<result_set>
|
2
|
+
<ttl>20</ttl>
|
3
|
+
<result>
|
4
|
+
<id>1</id>
|
5
|
+
<name>hello</name>
|
6
|
+
<description>
|
7
|
+
this is a long description for a text field of some kind.
|
8
|
+
this is a long description for a text field of some kind.
|
9
|
+
this is a long description for a text field of some kind.
|
10
|
+
this is a long description for a text field of some kind.
|
11
|
+
this is a long description for a text field of some kind.
|
12
|
+
this is a long description for a text field of some kind.
|
13
|
+
this is a long description for a text field of some kind.
|
14
|
+
this is a long description for a text field of some kind.
|
15
|
+
this is a long description for a text field of some kind.
|
16
|
+
this is a long description for a text field of some kind.
|
17
|
+
this is a long description for a text field of some kind.
|
18
|
+
this is a long description for a text field of some kind.
|
19
|
+
this is a long description for a text field of some kind.
|
20
|
+
</description>
|
21
|
+
</result>
|
22
|
+
<result>
|
23
|
+
<id>2</id>
|
24
|
+
<name>hello</name>
|
25
|
+
<description>
|
26
|
+
this is a long description for a text field of some kind.
|
27
|
+
this is a long description for a text field of some kind.
|
28
|
+
this is a long description for a text field of some kind.
|
29
|
+
this is a long description for a text field of some kind.
|
30
|
+
this is a long description for a text field of some kind.
|
31
|
+
this is a long description for a text field of some kind.
|
32
|
+
this is a long description for a text field of some kind.
|
33
|
+
this is a long description for a text field of some kind.
|
34
|
+
this is a long description for a text field of some kind.
|
35
|
+
this is a long description for a text field of some kind.
|
36
|
+
this is a long description for a text field of some kind.
|
37
|
+
this is a long description for a text field of some kind.
|
38
|
+
this is a long description for a text field of some kind.
|
39
|
+
</description>
|
40
|
+
</result>
|
41
|
+
<result>
|
42
|
+
<id>3</id>
|
43
|
+
<name>hello</name>
|
44
|
+
<description>
|
45
|
+
this is a long description for a text field of some kind.
|
46
|
+
this is a long description for a text field of some kind.
|
47
|
+
this is a long description for a text field of some kind.
|
48
|
+
this is a long description for a text field of some kind.
|
49
|
+
this is a long description for a text field of some kind.
|
50
|
+
this is a long description for a text field of some kind.
|
51
|
+
this is a long description for a text field of some kind.
|
52
|
+
this is a long description for a text field of some kind.
|
53
|
+
this is a long description for a text field of some kind.
|
54
|
+
this is a long description for a text field of some kind.
|
55
|
+
this is a long description for a text field of some kind.
|
56
|
+
this is a long description for a text field of some kind.
|
57
|
+
this is a long description for a text field of some kind.
|
58
|
+
</description>
|
59
|
+
</result>
|
60
|
+
</result_set>
|
data/spec/servers/app.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'sinatra'
|
4
|
+
require 'json'
|
5
|
+
require 'zlib'
|
6
|
+
|
7
|
+
@@fail_count = 0
|
8
|
+
|
9
|
+
post '/file' do
|
10
|
+
{
|
11
|
+
'content-type' => params[:file][:type],
|
12
|
+
'filename' => params[:file][:filename],
|
13
|
+
'content' => params[:file][:tempfile].read,
|
14
|
+
'request-content-type' => request.env['CONTENT_TYPE']
|
15
|
+
}.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/fail/:number' do
|
19
|
+
if @@fail_count >= params[:number].to_i
|
20
|
+
"ok"
|
21
|
+
else
|
22
|
+
@@fail_count += 1
|
23
|
+
error 500, "oh noes!"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
get '/fail_forever' do
|
28
|
+
error 500, "oh noes!"
|
29
|
+
end
|
30
|
+
|
31
|
+
get '/redirect' do
|
32
|
+
redirect '/'
|
33
|
+
end
|
34
|
+
|
35
|
+
get '/bad_redirect' do
|
36
|
+
redirect '/bad_redirect'
|
37
|
+
end
|
38
|
+
|
39
|
+
get '/auth_basic/:username/:password' do
|
40
|
+
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
41
|
+
# Check that we've got a basic auth, and that it's credentials match the ones
|
42
|
+
# provided in the request
|
43
|
+
if @auth.provided? && @auth.basic? && @auth.credentials == [ params[:username], params[:password] ]
|
44
|
+
# auth is valid - confirm it
|
45
|
+
true
|
46
|
+
else
|
47
|
+
# invalid auth - request the authentication
|
48
|
+
response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
|
49
|
+
throw(:halt, [401, "Not authorized\n"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
get '/auth_ntlm' do
|
54
|
+
# we're just checking for the existence if NTLM auth header here. It's validation
|
55
|
+
# is too troublesome and really doesn't bother is much, it's up to libcurl to make
|
56
|
+
# it valid
|
57
|
+
response['WWW-Authenticate'] = 'NTLM'
|
58
|
+
is_ntlm_auth = /^NTLM/ =~ request.env['HTTP_AUTHORIZATION']
|
59
|
+
true if is_ntlm_auth
|
60
|
+
throw(:halt, [401, "Not authorized\n"]) if !is_ntlm_auth
|
61
|
+
end
|
62
|
+
|
63
|
+
get '/gzipped' do
|
64
|
+
req_env = request.env.to_json
|
65
|
+
z = Zlib::Deflate.new
|
66
|
+
gzipped_env = z.deflate(req_env, Zlib::FINISH)
|
67
|
+
z.close
|
68
|
+
response['Content-Encoding'] = 'gzip'
|
69
|
+
gzipped_env
|
70
|
+
end
|
71
|
+
|
72
|
+
get '/**' do
|
73
|
+
sleep params["delay"].to_i if params.has_key?("delay")
|
74
|
+
request.env.merge!(:body => request.body.read).to_json
|
75
|
+
end
|
76
|
+
|
77
|
+
head '/**' do
|
78
|
+
sleep params["delay"].to_i if params.has_key?("delay")
|
79
|
+
end
|
80
|
+
|
81
|
+
put '/**' do
|
82
|
+
puts request.inspect
|
83
|
+
request.env.merge!(:body => request.body.read).to_json
|
84
|
+
end
|
85
|
+
|
86
|
+
post '/**' do
|
87
|
+
puts request.inspect
|
88
|
+
request.env.merge!(:body => request.body.read).to_json
|
89
|
+
end
|
90
|
+
|
91
|
+
delete '/**' do
|
92
|
+
puts request.inspect
|
93
|
+
request.env.merge!(:body => request.body.read).to_json
|
94
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'json'
|
3
|
+
require "spec"
|
4
|
+
|
5
|
+
# gem install redgreen for colored test output
|
6
|
+
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
|
7
|
+
|
8
|
+
path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
|
9
|
+
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
|
10
|
+
|
11
|
+
require path + '/typhoeus'
|
12
|
+
|
13
|
+
Spec::Runner.configure do |config|
|
14
|
+
end
|
@@ -0,0 +1,343 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Typhoeus::Easy do
|
4
|
+
describe "#supports_zlib" do
|
5
|
+
before(:each) do
|
6
|
+
@easy = Typhoeus::Easy.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return true if the version string has zlib" do
|
10
|
+
@easy.stub!(:curl_version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3 libidn/1.16")
|
11
|
+
@easy.supports_zlib?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return false if the version string doesn't have zlib" do
|
15
|
+
@easy.stub!(:curl_version).and_return("libcurl/7.20.0 OpenSSL/0.9.8l libidn/1.16")
|
16
|
+
@easy.supports_zlib?.should be_false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "curl errors" do
|
21
|
+
it "should provide the CURLE_OPERATION_TIMEDOUT return code when a request times out" do
|
22
|
+
e = Typhoeus::Easy.new
|
23
|
+
e.url = "http://localhost:3001/?delay=1"
|
24
|
+
e.method = :get
|
25
|
+
e.timeout = 100
|
26
|
+
e.perform
|
27
|
+
e.curl_return_code.should == 28
|
28
|
+
e.curl_error_message.should == "Timeout was reached"
|
29
|
+
e.response_code.should == 0
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should provide the CURLE_COULDNT_CONNECT return code when trying to connect to a non existent port" do
|
33
|
+
e = Typhoeus::Easy.new
|
34
|
+
e.url = "http://localhost:3999"
|
35
|
+
e.method = :get
|
36
|
+
e.connect_timeout = 100
|
37
|
+
e.perform
|
38
|
+
e.curl_return_code.should == 7
|
39
|
+
e.curl_error_message.should == "Couldn't connect to server"
|
40
|
+
e.response_code.should == 0
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not return an error message on a successful easy operation" do
|
44
|
+
easy = Typhoeus::Easy.new
|
45
|
+
easy.url = "http://localhost:3002"
|
46
|
+
easy.method = :get
|
47
|
+
easy.curl_error_message.should == nil
|
48
|
+
easy.perform
|
49
|
+
easy.response_code.should == 200
|
50
|
+
easy.curl_return_code.should == 0
|
51
|
+
easy.curl_error_message.should == "No error"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "options" do
|
57
|
+
it "should not follow redirects if not instructed to" do
|
58
|
+
e = Typhoeus::Easy.new
|
59
|
+
e.url = "http://localhost:3001/redirect"
|
60
|
+
e.method = :get
|
61
|
+
e.perform
|
62
|
+
e.response_code.should == 302
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should allow for following redirects" do
|
66
|
+
e = Typhoeus::Easy.new
|
67
|
+
e.url = "http://localhost:3001/redirect"
|
68
|
+
e.method = :get
|
69
|
+
e.follow_location = true
|
70
|
+
e.perform
|
71
|
+
e.response_code.should == 200
|
72
|
+
JSON.parse(e.response_body)["REQUEST_METHOD"].should == "GET"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should allow you to set the user agent" do
|
76
|
+
easy = Typhoeus::Easy.new
|
77
|
+
easy.url = "http://localhost:3002"
|
78
|
+
easy.method = :get
|
79
|
+
easy.user_agent = "myapp"
|
80
|
+
easy.perform
|
81
|
+
easy.response_code.should == 200
|
82
|
+
JSON.parse(easy.response_body)["HTTP_USER_AGENT"].should == "myapp"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should provide a timeout in milliseconds" do
|
86
|
+
e = Typhoeus::Easy.new
|
87
|
+
e.url = "http://localhost:3001/?delay=1"
|
88
|
+
e.method = :get
|
89
|
+
e.timeout = 10
|
90
|
+
e.perform
|
91
|
+
e.timed_out?.should == true
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should allow the setting of the max redirects to follow" do
|
95
|
+
e = Typhoeus::Easy.new
|
96
|
+
e.url = "http://localhost:3001/redirect"
|
97
|
+
e.method = :get
|
98
|
+
e.follow_location = true
|
99
|
+
e.max_redirects = 5
|
100
|
+
e.perform
|
101
|
+
e.response_code.should == 200
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should handle our bad redirect action, provided we've set max_redirects properly" do
|
105
|
+
e = Typhoeus::Easy.new
|
106
|
+
e.url = "http://localhost:3001/bad_redirect"
|
107
|
+
e.method = :get
|
108
|
+
e.follow_location = true
|
109
|
+
e.max_redirects = 5
|
110
|
+
e.perform
|
111
|
+
e.response_code.should == 302
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "authentication" do
|
116
|
+
it "should allow to set username and password" do
|
117
|
+
e = Typhoeus::Easy.new
|
118
|
+
username, password = 'foo', 'bar'
|
119
|
+
e.auth = { :username => username, :password => password }
|
120
|
+
e.url = "http://localhost:3001/auth_basic/#{username}/#{password}"
|
121
|
+
e.method = :get
|
122
|
+
e.perform
|
123
|
+
e.response_code.should == 200
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should allow to query auth methods support by the server" do
|
127
|
+
e = Typhoeus::Easy.new
|
128
|
+
e.url = "http://localhost:3001/auth_basic/foo/bar"
|
129
|
+
e.method = :get
|
130
|
+
e.perform
|
131
|
+
e.auth_methods.should == Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_BASIC]
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should allow to set authentication method" do
|
135
|
+
e = Typhoeus::Easy.new
|
136
|
+
e.auth = { :username => 'username', :password => 'password', :method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM] }
|
137
|
+
e.url = "http://localhost:3001/auth_ntlm"
|
138
|
+
e.method = :get
|
139
|
+
e.perform
|
140
|
+
e.response_code.should == 200
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "get" do
|
145
|
+
it "should perform a get" do
|
146
|
+
easy = Typhoeus::Easy.new
|
147
|
+
easy.url = "http://localhost:3002"
|
148
|
+
easy.method = :get
|
149
|
+
easy.perform
|
150
|
+
easy.response_code.should == 200
|
151
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "purge" do
|
156
|
+
it "should set custom request to purge" do
|
157
|
+
easy = Typhoeus::Easy.new
|
158
|
+
easy.should_receive(:set_option).with(Typhoeus::Easy::OPTION_VALUES[:CURLOPT_CUSTOMREQUEST], "PURGE").once
|
159
|
+
easy.method = :purge
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "head" do
|
164
|
+
it "should perform a head" do
|
165
|
+
easy = Typhoeus::Easy.new
|
166
|
+
easy.url = "http://localhost:3002"
|
167
|
+
easy.method = :head
|
168
|
+
easy.perform
|
169
|
+
easy.response_code.should == 200
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "start_time" do
|
174
|
+
it "should be get/settable" do
|
175
|
+
time = Time.now
|
176
|
+
easy = Typhoeus::Easy.new
|
177
|
+
easy.start_time.should be_nil
|
178
|
+
easy.start_time = time
|
179
|
+
easy.start_time.should == time
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "params=" do
|
184
|
+
it "should handle arrays of params" do
|
185
|
+
easy = Typhoeus::Easy.new
|
186
|
+
easy.url = "http://localhost:3002/index.html"
|
187
|
+
easy.method = :get
|
188
|
+
easy.request_body = "this is a body!"
|
189
|
+
easy.params = {
|
190
|
+
:foo => 'bar',
|
191
|
+
:username => ['dbalatero', 'dbalatero2']
|
192
|
+
}
|
193
|
+
easy.url.should =~ /\?.*foo=bar&username=dbalatero&username=dbalatero2/
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
describe "put" do
|
199
|
+
it "should perform a put" do
|
200
|
+
easy = Typhoeus::Easy.new
|
201
|
+
easy.url = "http://localhost:3002"
|
202
|
+
easy.method = :put
|
203
|
+
easy.perform
|
204
|
+
easy.response_code.should == 200
|
205
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "PUT"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should send a request body" do
|
209
|
+
easy = Typhoeus::Easy.new
|
210
|
+
easy.url = "http://localhost:3002"
|
211
|
+
easy.method = :put
|
212
|
+
easy.request_body = "this is a body!"
|
213
|
+
easy.perform
|
214
|
+
easy.response_code.should == 200
|
215
|
+
easy.response_body.should include("this is a body!")
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should be able perform put with empty bodies on the same easy handle" do
|
219
|
+
easy = Typhoeus::Easy.new
|
220
|
+
easy.url = "http://localhost:3002"
|
221
|
+
easy.method = :put
|
222
|
+
easy.perform
|
223
|
+
easy.response_code.should == 200
|
224
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "PUT"
|
225
|
+
|
226
|
+
easy.reset
|
227
|
+
|
228
|
+
easy.url = "http://localhost:3002"
|
229
|
+
easy.method = :put
|
230
|
+
easy.perform
|
231
|
+
easy.response_code.should == 200
|
232
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "PUT"
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
describe "post" do
|
238
|
+
it "should perform a post" do
|
239
|
+
easy = Typhoeus::Easy.new
|
240
|
+
easy.url = "http://localhost:3002"
|
241
|
+
easy.method = :post
|
242
|
+
easy.perform
|
243
|
+
easy.response_code.should == 200
|
244
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "POST"
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should send a request body" do
|
248
|
+
easy = Typhoeus::Easy.new
|
249
|
+
easy.url = "http://localhost:3002"
|
250
|
+
easy.method = :post
|
251
|
+
easy.request_body = "this is a body!"
|
252
|
+
easy.perform
|
253
|
+
easy.response_code.should == 200
|
254
|
+
easy.response_body.should include("this is a body!")
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should handle params" do
|
258
|
+
easy = Typhoeus::Easy.new
|
259
|
+
easy.url = "http://localhost:3002"
|
260
|
+
easy.method = :post
|
261
|
+
easy.params = {:foo => "bar"}
|
262
|
+
easy.perform
|
263
|
+
easy.response_code.should == 200
|
264
|
+
easy.response_body.should =~ /foo=bar/
|
265
|
+
end
|
266
|
+
|
267
|
+
it "should use Content-Type: application/x-www-form-urlencoded for normal posts" do
|
268
|
+
easy = Typhoeus::Easy.new
|
269
|
+
easy.url = "http://localhost:3002/normal_post"
|
270
|
+
easy.method = :post
|
271
|
+
easy.params = { :a => 'b', :c => 'd',
|
272
|
+
:e => { :f => { :g => 'h' } } }
|
273
|
+
easy.perform
|
274
|
+
|
275
|
+
request = JSON.parse(easy.response_body)
|
276
|
+
request['CONTENT_TYPE'].should == 'application/x-www-form-urlencoded'
|
277
|
+
request['rack.request.form_vars'].should == 'a=b&c=d&e[f][g]=h'
|
278
|
+
end
|
279
|
+
|
280
|
+
it "should handle a file upload, as multipart" do
|
281
|
+
easy = Typhoeus::Easy.new
|
282
|
+
easy.url = "http://localhost:3002/file"
|
283
|
+
easy.method = :post
|
284
|
+
easy.params = {:file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")}
|
285
|
+
easy.perform
|
286
|
+
easy.response_code.should == 200
|
287
|
+
result = JSON.parse(easy.response_body)
|
288
|
+
|
289
|
+
{ 'content-type' => 'text/plain',
|
290
|
+
'filename' => 'placeholder.txt',
|
291
|
+
'content' => 'This file is used to test uploading.'
|
292
|
+
}.each do |key, val|
|
293
|
+
result[key].should == val
|
294
|
+
end
|
295
|
+
|
296
|
+
result['request-content-type'].should =~ /multipart/
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
describe "delete" do
|
301
|
+
it "should perform a delete" do
|
302
|
+
easy = Typhoeus::Easy.new
|
303
|
+
easy.url = "http://localhost:3002"
|
304
|
+
easy.method = :delete
|
305
|
+
easy.perform
|
306
|
+
easy.response_code.should == 200
|
307
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "DELETE"
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should send a request body" do
|
311
|
+
easy = Typhoeus::Easy.new
|
312
|
+
easy.url = "http://localhost:3002"
|
313
|
+
easy.method = :delete
|
314
|
+
easy.request_body = "this is a body!"
|
315
|
+
easy.perform
|
316
|
+
easy.response_code.should == 200
|
317
|
+
easy.response_body.should include("this is a body!")
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
describe "encoding/compression support" do
|
322
|
+
|
323
|
+
it "should send valid encoding headers and decode the response" do
|
324
|
+
easy = Typhoeus::Easy.new
|
325
|
+
easy.url = "http://localhost:3002/gzipped"
|
326
|
+
easy.method = :get
|
327
|
+
easy.perform
|
328
|
+
easy.response_code.should == 200
|
329
|
+
JSON.parse(easy.response_body)["HTTP_ACCEPT_ENCODING"].should == "deflate, gzip"
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should send valid encoding headers and decode the response after reset" do
|
333
|
+
easy = Typhoeus::Easy.new
|
334
|
+
easy.reset
|
335
|
+
easy.url = "http://localhost:3002/gzipped"
|
336
|
+
easy.method = :get
|
337
|
+
easy.perform
|
338
|
+
easy.response_code.should == 200
|
339
|
+
JSON.parse(easy.response_body)["HTTP_ACCEPT_ENCODING"].should == "deflate, gzip"
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|
343
|
+
end
|