fotonauts-curb 0.7.7

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/curl.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'curb'
2
+
data/tests/alltests.rb ADDED
@@ -0,0 +1,3 @@
1
+ $: << $TESTDIR = File.expand_path(File.dirname(__FILE__))
2
+ require 'unittests'
3
+ Dir[File.join($TESTDIR, 'bug_*.rb')].each { |lib| require lib }
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ require 'webrick'
3
+ class ::WEBrick::HTTPServer ; def access_log(config, req, res) ; end ; end
4
+ class ::WEBrick::BasicLog ; def log(level, data) ; end ; end
5
+
6
+ class BugTestInstancePostDiffersFromClassPost < Test::Unit::TestCase
7
+ def test_bug
8
+ server = WEBrick::HTTPServer.new( :Port => 9999 )
9
+ server.mount_proc("/test") do|req,res|
10
+ sleep 0.5
11
+ res.body = "hi"
12
+ res['Content-Type'] = "text/html"
13
+ end
14
+
15
+ thread = Thread.new(server) do|srv|
16
+ srv.start
17
+ end
18
+
19
+ threads = []
20
+ timer = Time.now
21
+
22
+ 5.times do |i|
23
+ t = Thread.new do
24
+ c = Curl::Easy.perform('http://localhost:9999/test')
25
+ c.header_str
26
+ end
27
+ threads << t
28
+ end
29
+
30
+ multi_responses = threads.collect do|t|
31
+ t.value
32
+ end
33
+
34
+ multi_time = (Time.now - timer)
35
+ puts "requested in #{multi_time}"
36
+
37
+ timer = Time.now
38
+ single_responses = []
39
+ 5.times do |i|
40
+ c = Curl::Easy.perform('http://localhost:9999/test')
41
+ single_responses << c.header_str
42
+ end
43
+
44
+ single_time = (Time.now - timer)
45
+ puts "requested in #{single_time}"
46
+
47
+ assert single_time > multi_time
48
+
49
+ server.shutdown
50
+ thread.join
51
+ end
52
+ end
@@ -0,0 +1,83 @@
1
+ =begin
2
+ From jwhitmire
3
+ Todd, I'm trying to use curb to post data to a REST url. We're using it to post support questions from our iphone app directly to tender. The post looks good to me, but curl is not adding the content-length header so I get a 411 length required response from the server.
4
+
5
+ Here's my post block, do you see anything obvious? Do I need to manually add the Content-Length header?
6
+
7
+ c = Curl::Easy.http_post(url) do |curl|
8
+ curl.headers["User-Agent"] = "Curl/Ruby"
9
+ if user
10
+ curl.headers["X-Multipass"] = user.multipass
11
+ else
12
+ curl.headers["X-Tender-Auth"] = TOKEN
13
+ end
14
+ curl.headers["Accept"] = "application/vnd.tender-v1+json"
15
+
16
+ curl.post_body = params.map{|f,k| "#{curl.escape(f)}=#{curl.escape(k)}"}.join('&')
17
+
18
+ curl.verbose = true
19
+ curl.follow_location = true
20
+ curl.enable_cookies = true
21
+ end
22
+ Any insight you care to share would be helpful. Thanks.
23
+ =end
24
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
25
+ require 'webrick'
26
+ class ::WEBrick::HTTPServer ; def access_log(config, req, res) ; end ; end
27
+ class ::WEBrick::BasicLog ; def log(level, data) ; end ; end
28
+
29
+ class BugCurbEasyPostWithStringNoContentLengthHeader < Test::Unit::TestCase
30
+ def test_bug_workaround
31
+ server = WEBrick::HTTPServer.new( :Port => 9999 )
32
+ server.mount_proc("/test") do|req,res|
33
+ assert_equal '15', req['Content-Length']
34
+ res.body = "hi"
35
+ res['Content-Type'] = "text/html"
36
+ end
37
+
38
+ thread = Thread.new(server) do|srv|
39
+ srv.start
40
+ end
41
+ params = {:cat => "hat", :foo => "bar"}
42
+
43
+ post_body = params.map{|f,k| "#{Curl::Easy.new.escape(f)}=#{Curl::Easy.new.escape(k)}"}.join('&')
44
+ c = Curl::Easy.http_post("http://127.0.0.1:9999/test",post_body) do |curl|
45
+ curl.headers["User-Agent"] = "Curl/Ruby"
46
+ curl.headers["X-Tender-Auth"] = "A Token"
47
+ curl.headers["Accept"] = "application/vnd.tender-v1+json"
48
+
49
+ curl.follow_location = true
50
+ curl.enable_cookies = true
51
+ end
52
+
53
+ server.shutdown
54
+ thread.join
55
+ end
56
+ def test_bug
57
+ server = WEBrick::HTTPServer.new( :Port => 9999 )
58
+ server.mount_proc("/test") do|req,res|
59
+ assert_equal '15', req['Content-Length']
60
+ res.body = "hi"
61
+ res['Content-Type'] = "text/html"
62
+ end
63
+
64
+ thread = Thread.new(server) do|srv|
65
+ srv.start
66
+ end
67
+ params = {:cat => "hat", :foo => "bar"}
68
+
69
+ c = Curl::Easy.http_post("http://127.0.0.1:9999/test") do |curl|
70
+ curl.headers["User-Agent"] = "Curl/Ruby"
71
+ curl.headers["X-Tender-Auth"] = "A Token"
72
+ curl.headers["Accept"] = "application/vnd.tender-v1+json"
73
+
74
+ curl.post_body = params.map{|f,k| "#{curl.escape(f)}=#{curl.escape(k)}"}.join('&')
75
+
76
+ curl.follow_location = true
77
+ curl.enable_cookies = true
78
+ end
79
+
80
+ server.shutdown
81
+ thread.join
82
+ end
83
+ end
@@ -0,0 +1,53 @@
1
+ # From Vlad Jebelev:
2
+ #
3
+ # - Second thing - I think you just probably didn't have the time to update
4
+ # instance methods yet but when I POST with a reusal of a previous curl
5
+ # instance, it doesnt' work for me, e.g. when I create a curl previously and
6
+ # then issue:
7
+ #
8
+ # c.http_post(login_url, *fields)
9
+ #
10
+ # instead of:
11
+ #
12
+ # c = Curl::Easy.http_post(login_url, *fields) do |curl|
13
+ # ...
14
+ # end
15
+ #
16
+ # then the result I am getting is quite different.
17
+ #
18
+ # ================
19
+ #
20
+ # Update:
21
+ #
22
+ # It seems that class httpost is incorrectly passing arguments down to
23
+ # instance httppost. This bug is intermittent, but results in an
24
+ # exception from the first post when it occurs.
25
+ #
26
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
27
+
28
+ class BugTestInstancePostDiffersFromClassPost < Test::Unit::TestCase
29
+ def test_bug
30
+ 5.times do |i|
31
+ puts "Test ##{i}"
32
+ do_test
33
+ sleep 2
34
+ end
35
+ end
36
+
37
+ def do_test
38
+ c = Curl::Easy.http_post('https://www.google.com/accounts/ServiceLoginAuth',
39
+ Curl::PostField.content('ltmpl','m_blanco'))
40
+ body_c, header_c = c.body_str, c.header_str
41
+
42
+ sleep 2
43
+
44
+ c.http_post('https://www.google.com/accounts/ServiceLoginAuth',
45
+ Curl::PostField.content('ltmpl','m_blanco'))
46
+ body_i, header_i = c.body_str, c.header_str
47
+
48
+ # timestamps will differ, just check first bit. We wont get here if
49
+ # the bug bites anyway...
50
+ assert_equal header_c[0..50], header_i[0..50]
51
+ end
52
+ end
53
+
@@ -0,0 +1,14 @@
1
+ # From safis http://github.com/taf2/curb/issues#issue/5
2
+ # irb: require 'curb'
3
+ # irb: multi = Curl::Multi.new
4
+ # irb: exit
5
+ # <main>:47140: [BUG] Bus Error
6
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'..','ext'))
7
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
8
+ require 'curb'
9
+
10
+ class BugMultiSegfault < Test::Unit::TestCase
11
+ def test_bug
12
+ multi = Curl::Multi.new
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ # From GICodeWarrior:
2
+ #
3
+ # $ ruby crash_curb.rb
4
+ # crash_curb.rb:7: [BUG] Segmentation fault
5
+ # ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
6
+ #
7
+ # Aborted
8
+ # crash_curb.rb:
9
+ # #!/usr/bin/ruby
10
+ # require 'rubygems'
11
+ # require 'curb'
12
+ #
13
+ # curl = Curl::Easy.new('http://example.com/')
14
+ # curl.multipart_form_post = true
15
+ # curl.http_post(Curl::PostField.file('test', 'test.xml'){'example data'})
16
+ # Ubuntu 9.10
17
+ # curb gem version 0.6.2.1
18
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
19
+
20
+ class BugPostFieldsCrash < Test::Unit::TestCase
21
+ def test_crash
22
+ curl = Curl::Easy.new('http://example.com/')
23
+ curl.multipart_form_post = true
24
+ curl.http_post(Curl::PostField.file('test', 'test.xml'){'example data'})
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ # Not sure if this is an IRB bug, but thought you guys should know.
2
+ #
3
+ # ** My Ruby version: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
4
+ # ** Version of Rubygems: 1.3.5
5
+ # ** Version of the Curb gem: 0.6.6.0
6
+ #
7
+ #
8
+ # Transcript of IRB session:
9
+ # ------------------------------------------------------------------------------------------------------------------
10
+ # irb(main):001:0> a = {
11
+ # irb(main):002:1* :type => :pie,
12
+ # irb(main):003:1* :series => {
13
+ # irb(main):004:2* :names => [:a,:b],
14
+ # irb(main):005:2* :values => [70,30],
15
+ # irb(main):006:2* :colors => [:red,:green]
16
+ # irb(main):007:2> },
17
+ # irb(main):008:1* :output_format => :png
18
+ # irb(main):009:1> }
19
+ # => {:type=>:pie, :output_format=>:png, :series=>{:names=>[:a, :b], :values=>[70, 30], :colors=>[:red, :green]}}
20
+ # irb(main):010:0> post = []
21
+ # => []
22
+ # irb(main):011:0> require 'rubygems'
23
+ # => true
24
+ # irb(main):012:0> require 'curb'
25
+ # => true
26
+ # irb(main):013:0> include Curl
27
+ # => Object
28
+ # irb(main):014:0> a.each_pair do |k,v|
29
+ # irb(main):015:1* post << PostField.content(k,v)
30
+ # irb(main):016:1> end
31
+ # => {:type=>:pie, :output_format=>:png, :series=>{:names=>[:a, :b], :values=>[70, 30], :colors=>[:red, :green]}}
32
+ # irb(main):017:0> post
33
+ # /usr/lib/ruby/1.8/irb.rb:302: [BUG] Segmentation fault
34
+ # ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
35
+ #
36
+ # Aborted
37
+ # ------------------------------------------------------------------------------------------------------------------
38
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
39
+
40
+ class BugPostFieldsCrash2 < Test::Unit::TestCase
41
+ def test_crash
42
+ a = {
43
+ :type => :pie,
44
+ :series => {
45
+ :names => [:a,:b],
46
+ :values => [70,30],
47
+ :colors => [:red,:green]
48
+ },
49
+ :output_format => :png
50
+ }
51
+ post = []
52
+ a.each_pair do |k,v|
53
+ post << Curl::PostField.content(k,v)
54
+ end
55
+ post.inspect
56
+ end
57
+ end
@@ -0,0 +1,40 @@
1
+ # From Vlad Jebelev:
2
+ #
3
+ # - if I have a require statement after "require 'curb'" and there is a
4
+ # POST with at least 1 field, the script will fail with a segmentation
5
+ # fault, e.g. the following sequence fails every time for me (Ruby 1.8.5):
6
+ # -----------------------------------------------------------------
7
+ # require 'curb'
8
+ # require 'uri'
9
+ #
10
+ # url = 'https://www.google.com/accounts/ServiceLoginAuth'
11
+ #
12
+ # c = Curl::Easy.http_post(
13
+ # 'https://www.google.com/accounts/ServiceLoginAuth',
14
+ # [Curl:: PostField.content('ltmpl','m_blanco')] ) do |curl|
15
+ # end
16
+ # ------------------------------------------------------------------
17
+ # :..dev/util$ ruby seg.rb
18
+ # seg.rb:6: [BUG] Segmentation fault
19
+ # ruby 1.8.5 (2006-08-25) [i686-linux]
20
+ #
21
+ # Aborted
22
+ # ------------------------------------------------------------------
23
+ #
24
+ require 'test/unit'
25
+ require 'rbconfig'
26
+
27
+ $rubycmd = Config::CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
28
+
29
+ class BugTestRequireLastOrSegfault < Test::Unit::TestCase
30
+ def test_bug
31
+ 5.times do |i|
32
+ puts "Test ##{i}"
33
+
34
+ # will be empty string if it segfaults...
35
+ assert_equal 'success', `#$rubycmd #{File.dirname(__FILE__)}/require_last_or_segfault_script.rb`.chomp
36
+ sleep 5
37
+ end
38
+ end
39
+ end
40
+
data/tests/bugtests.rb ADDED
@@ -0,0 +1,9 @@
1
+ $: << $TESTDIR = File.expand_path(File.dirname(__FILE__))
2
+ puts "start"
3
+ begin
4
+ Dir[File.join($TESTDIR, 'bug_*.rb')].each { |lib| require lib }
5
+ rescue Object => e
6
+ puts e.message
7
+ ensure
8
+ puts "done"
9
+ end
data/tests/helper.rb ADDED
@@ -0,0 +1,178 @@
1
+ # DO NOT REMOVE THIS COMMENT - PART OF TESTMODEL.
2
+ # Copyright (c)2006 Ross Bamford. See LICENSE.
3
+ $CURB_TESTING = true
4
+ require 'uri'
5
+
6
+ $TOPDIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
7
+ $EXTDIR = File.join($TOPDIR, 'ext')
8
+ $LIBDIR = File.join($TOPDIR, 'lib')
9
+ $:.unshift($LIBDIR)
10
+ $:.unshift($EXTDIR)
11
+
12
+ require 'curb'
13
+ require 'test/unit'
14
+ require 'fileutils'
15
+
16
+ $TEST_URL = "file://#{URI.escape(File.expand_path(__FILE__).tr('\\','/').tr(':','|'))}"
17
+
18
+ require 'thread'
19
+ require 'webrick'
20
+
21
+ # set this to true to avoid testing with multiple threads
22
+ # or to test with multiple threads set it to false
23
+ # this is important since, some code paths will change depending
24
+ # on the presence of multiple threads
25
+ TEST_SINGLE_THREADED=false
26
+
27
+ # keep webrick quiet
28
+ class ::WEBrick::HTTPServer
29
+ def access_log(config, req, res)
30
+ # nop
31
+ end
32
+ end
33
+ class ::WEBrick::BasicLog
34
+ def log(level, data)
35
+ # nop
36
+ end
37
+ end
38
+
39
+ #
40
+ # Simple test server to record number of times a request is sent/recieved of a specific
41
+ # request type, e.g. GET,POST,PUT,DELETE
42
+ #
43
+ class TestServlet < WEBrick::HTTPServlet::AbstractServlet
44
+
45
+ def self.port=(p)
46
+ @port = p
47
+ end
48
+
49
+ def self.port
50
+ (@port or 9129)
51
+ end
52
+
53
+ def self.path
54
+ '/methods'
55
+ end
56
+
57
+ def self.url
58
+ "http://127.0.0.1:#{port}#{path}"
59
+ end
60
+
61
+ def respond_with(method,req,res)
62
+ res.body = method.to_s
63
+ $auth_header = req['Authorization']
64
+ res['Content-Type'] = "text/plain"
65
+ end
66
+
67
+ def do_GET(req,res)
68
+ respond_with(:GET,req,res)
69
+ end
70
+
71
+ def do_HEAD(req,res)
72
+ res['Location'] = "/nonexistent"
73
+ respond_with(:HEAD, req, res)
74
+ end
75
+
76
+ def do_POST(req,res)
77
+ if req.query['filename'].nil?
78
+ if req.body
79
+ params = {}
80
+ req.body.split('&').map{|s| k,v=s.split('='); params[k] = v }
81
+ end
82
+ if params and params['s'] == '500'
83
+ res.status = 500
84
+ else
85
+ respond_with("POST\n#{req.body}",req,res)
86
+ end
87
+ else
88
+ respond_with(req.query['filename'],req,res)
89
+ end
90
+ end
91
+
92
+ def do_PUT(req,res)
93
+ res['X-Requested-Content-Type'] = req.content_type
94
+ respond_with("PUT\n#{req.body}",req,res)
95
+ end
96
+
97
+ def do_DELETE(req,res)
98
+ respond_with(:DELETE,req,res)
99
+ end
100
+
101
+ def do_PURGE(req,res)
102
+ respond_with(:PURGE,req,res)
103
+ end
104
+
105
+ end
106
+
107
+ module TestServerMethods
108
+ def locked_file
109
+ File.join(File.dirname(__FILE__),"server_lock-#{@__port}")
110
+ end
111
+
112
+ def server_setup(port=9129,servlet=TestServlet)
113
+ @__port = port
114
+ if @server.nil? and !File.exist?(locked_file)
115
+
116
+ File.open(locked_file,'w') {|f| f << 'locked' }
117
+ if TEST_SINGLE_THREADED
118
+ rd, wr = IO.pipe
119
+ @__pid = fork do
120
+ rd.close
121
+ rd = nil
122
+
123
+ # start up a webrick server for testing delete
124
+ server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => File.expand_path(File.dirname(__FILE__))
125
+
126
+ server.mount(servlet.path, servlet)
127
+ server.mount("/ext", WEBrick::HTTPServlet::FileHandler, File.join(File.dirname(__FILE__),'..','ext'))
128
+
129
+ trap("INT") { server.shutdown }
130
+ GC.start
131
+ wr.flush
132
+ wr.close
133
+ server.start
134
+ end
135
+ wr.close
136
+ rd.read
137
+ rd.close
138
+ else
139
+ # start up a webrick server for testing delete
140
+ @server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => File.expand_path(File.dirname(__FILE__))
141
+
142
+ @server.mount(servlet.path, servlet)
143
+ @server.mount("/ext", WEBrick::HTTPServlet::FileHandler, File.join(File.dirname(__FILE__),'..','ext'))
144
+ queue = Queue.new # synchronize the thread startup to the main thread
145
+
146
+ @test_thread = Thread.new { queue << 1; @server.start }
147
+
148
+ # wait for the queue
149
+ value = queue.pop
150
+ if !value
151
+ STDERR.puts "Failed to startup test server!"
152
+ exit(1)
153
+ end
154
+
155
+ end
156
+
157
+ exit_code = lambda do
158
+ begin
159
+ if File.exist?(locked_file)
160
+ File.unlink locked_file
161
+ if TEST_SINGLE_THREADED
162
+ Process.kill 'INT', @__pid
163
+ else
164
+ @server.shutdown unless @server.nil?
165
+ end
166
+ end
167
+ #@server.shutdown unless @server.nil?
168
+ rescue Object => e
169
+ puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
170
+ end
171
+ end
172
+
173
+ trap("INT"){exit_code.call}
174
+ at_exit{exit_code.call}
175
+
176
+ end
177
+ end
178
+ end