curb 0.5.8.0-x86-linux

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of curb might be problematic. Click here for more details.

@@ -0,0 +1,421 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlMulti < Test::Unit::TestCase
4
+ def teardown
5
+ # get a better read on memory loss when running in valgrind
6
+ ObjectSpace.garbage_collect
7
+ end
8
+
9
+ def test_new_multi_01
10
+ d1 = ""
11
+ c1 = Curl::Easy.new($TEST_URL) do |curl|
12
+ curl.headers["User-Agent"] = "myapp-0.0"
13
+ curl.on_body {|d| d1 << d; d.length }
14
+ end
15
+
16
+ d2 = ""
17
+ c2 = Curl::Easy.new($TEST_URL) do |curl|
18
+ curl.headers["User-Agent"] = "myapp-0.0"
19
+ curl.on_body {|d| d2 << d; d.length }
20
+ end
21
+
22
+ m = Curl::Multi.new
23
+
24
+ m.add( c1 )
25
+ m.add( c2 )
26
+
27
+ m.perform
28
+
29
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, d1)
30
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, d2)
31
+
32
+ m = nil
33
+
34
+ end
35
+
36
+ def test_perform_block
37
+ c1 = Curl::Easy.new($TEST_URL)
38
+ c2 = Curl::Easy.new($TEST_URL)
39
+
40
+ m = Curl::Multi.new
41
+
42
+ m.add( c1 )
43
+ m.add( c2 )
44
+
45
+ m.perform do
46
+ # idle
47
+ puts "idling..."
48
+ end
49
+
50
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c1.body_str)
51
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c2.body_str)
52
+
53
+ m = nil
54
+
55
+ end
56
+
57
+ # NOTE: if this test runs slowly on Mac OSX, it is probably due to the use of a port install curl+ssl+ares install
58
+ # on my MacBook, this causes curl_easy_init to take nearly 0.01 seconds / * 100 below is 1 second too many!
59
+ def test_n_requests
60
+ n = 100
61
+ m = Curl::Multi.new
62
+ responses = []
63
+ n.times do|i|
64
+ responses[i] = ""
65
+ c = Curl::Easy.new($TEST_URL) do|curl|
66
+ curl.on_body{|data| responses[i] << data; data.size }
67
+ end
68
+ m.add c
69
+ end
70
+
71
+ m.perform
72
+
73
+ assert n, responses.size
74
+ n.times do|i|
75
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, responses[i], "response #{i}")
76
+ end
77
+
78
+ m = nil
79
+ end
80
+
81
+ def test_n_requests_with_break
82
+ # process n requests then load the handle again and run it again
83
+ n = 2
84
+ m = Curl::Multi.new
85
+ 5.times do|it|
86
+ responses = []
87
+ n.times do|i|
88
+ responses[i] = ""
89
+ c = Curl::Easy.new($TEST_URL) do|curl|
90
+ curl.on_body{|data| responses[i] << data; data.size }
91
+ end
92
+ m.add c
93
+ end
94
+ m.perform
95
+
96
+ assert n, responses.size
97
+ n.times do|i|
98
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, responses[i], "response #{i}")
99
+ end
100
+ end
101
+
102
+ m = nil
103
+
104
+ end
105
+
106
+ def test_idle_check
107
+ m = Curl::Multi.new
108
+ e = Curl::Easy.new($TEST_URL)
109
+
110
+ assert(m.idle?, 'A new Curl::Multi handle should be idle')
111
+
112
+ m.add(e)
113
+
114
+ assert((not m.idle?), 'A Curl::Multi handle with a request should not be idle')
115
+
116
+ m.perform
117
+
118
+ assert(m.idle?, 'A Curl::Multi handle should be idle after performing its requests')
119
+ end
120
+
121
+ def test_requests
122
+ m = Curl::Multi.new
123
+
124
+ assert_equal([], m.requests, 'A new Curl::Multi handle should have no requests')
125
+
126
+ 10.times do
127
+ m.add(Curl::Easy.new($TEST_URL))
128
+ end
129
+
130
+ assert_equal(10, m.requests.length, 'multi.requests should contain all the active requests')
131
+
132
+ m.perform
133
+
134
+ assert_equal([], m.requests, 'A new Curl::Multi handle should have no requests after a perform')
135
+ end
136
+
137
+ def test_cancel
138
+ m = Curl::Multi.new
139
+ m.cancel! # shouldn't raise anything
140
+
141
+ 10.times do
142
+ m.add(Curl::Easy.new($TEST_URL))
143
+ end
144
+
145
+ m.cancel!
146
+
147
+ assert_equal([], m.requests, 'A new Curl::Multi handle should have no requests after being canceled')
148
+ end
149
+
150
+ def test_with_success
151
+ c1 = Curl::Easy.new($TEST_URL)
152
+ c2 = Curl::Easy.new($TEST_URL)
153
+ success_called1 = false
154
+ success_called2 = false
155
+
156
+ c1.on_success do|c|
157
+ success_called1 = true
158
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
159
+ end
160
+
161
+ c2.on_success do|c|
162
+ success_called2 = true
163
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
164
+ end
165
+
166
+ m = Curl::Multi.new
167
+
168
+ m.add( c1 )
169
+ m.add( c2 )
170
+
171
+ m.perform do
172
+ # idle
173
+ puts "idling..."
174
+ end
175
+
176
+ assert success_called2
177
+ assert success_called1
178
+
179
+ m = nil
180
+ end
181
+
182
+ def test_with_success_cb_with_404
183
+ c1 = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")
184
+ c2 = Curl::Easy.new($TEST_URL)
185
+ success_called1 = false
186
+ success_called2 = false
187
+
188
+ c1.on_success do|c|
189
+ success_called1 = true
190
+ #puts "success 1 called: #{c.body_str.inspect}"
191
+ #assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
192
+ end
193
+
194
+ c1.on_failure do|c,rc|
195
+ # rc => [Curl::Err::MalformedURLError, "URL using bad/illegal format or missing URL"]
196
+ assert_equal Curl::Easy, c.class
197
+ assert_equal Curl::Err::MalformedURLError, rc.first
198
+ assert_equal "URL using bad/illegal format or missing URL", rc.last
199
+ end
200
+
201
+ c2.on_success do|c|
202
+ # puts "success 2 called: #{c.body_str.inspect}"
203
+ success_called2 = true
204
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
205
+ end
206
+
207
+ m = Curl::Multi.new
208
+
209
+ #puts "c1: #{c1.url}"
210
+ m.add( c1 )
211
+ #puts "c2: #{c2.url}"
212
+ m.add( c2 )
213
+
214
+ #puts "calling"
215
+ m.perform do
216
+ # idle
217
+ end
218
+
219
+ assert success_called2
220
+ assert !success_called1
221
+
222
+ m = nil
223
+ end
224
+
225
+ # This tests whether, ruby's GC will trash an out of scope easy handle
226
+ class TestForScope
227
+ attr_reader :buf
228
+
229
+ def t_method
230
+ @buf = ""
231
+ @m = Curl::Multi.new
232
+ 10.times do|i|
233
+ c = Curl::Easy.new($TEST_URL)
234
+ c.on_success{|b| @buf << b.body_str }
235
+ ObjectSpace.garbage_collect
236
+ @m.add(c)
237
+ ObjectSpace.garbage_collect
238
+ end
239
+ ObjectSpace.garbage_collect
240
+ end
241
+
242
+ def t_call
243
+ @m.perform do
244
+ ObjectSpace.garbage_collect
245
+ end
246
+ end
247
+
248
+ def self.test
249
+ ObjectSpace.garbage_collect
250
+ tfs = TestForScope.new
251
+ ObjectSpace.garbage_collect
252
+ tfs.t_method
253
+ ObjectSpace.garbage_collect
254
+ tfs.t_call
255
+ ObjectSpace.garbage_collect
256
+
257
+ tfs.buf
258
+ end
259
+
260
+ end
261
+
262
+ def test_with_garbage_collect
263
+ ObjectSpace.garbage_collect
264
+ buf = TestForScope.test
265
+ ObjectSpace.garbage_collect
266
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, buf)
267
+ end
268
+
269
+ =begin
270
+ def test_remote_requests
271
+ responses = {}
272
+ requests = ["http://google.co.uk/", "http://ruby-lang.org/"]
273
+ m = Curl::Multi.new
274
+ # add a few easy handles
275
+ requests.each do |url|
276
+ responses[url] = ""
277
+ responses["#{url}-header"] = ""
278
+ c = Curl::Easy.new(url) do|curl|
279
+ curl.follow_location = true
280
+ curl.on_header{|data| responses["#{url}-header"] << data; data.size }
281
+ curl.on_body{|data| responses[url] << data; data.size }
282
+ curl.on_success {
283
+ puts curl.last_effective_url
284
+ }
285
+ end
286
+ m.add(c)
287
+ end
288
+
289
+ m.perform
290
+
291
+ requests.each do|url|
292
+ puts responses["#{url}-header"].split("\r\n").inspect
293
+ #puts responses[url].size
294
+ end
295
+ end
296
+ =end
297
+
298
+ def test_multi_easy_get_01
299
+ urls = []
300
+ root_uri = 'http://127.0.0.1:9129/ext/'
301
+ # send a request to fetch all c files in the ext dir
302
+ Dir[File.dirname(__FILE__) + "/../ext/*.c"].each do|path|
303
+ urls << root_uri + File.basename(path)
304
+ end
305
+ urls = urls[0..(urls.size/2)] # keep it fast, webrick...
306
+ Curl::Multi.get(urls, {:follow_location => true}, {:pipeline => true}) do|curl|
307
+ assert_equal 200, curl.response_code
308
+ end
309
+ end
310
+
311
+ def test_multi_easy_post_01
312
+ urls = [
313
+ { :url => TestServlet.url + '?q=1', :post_fields => {'field1' => 'value1', 'k' => 'j'}},
314
+ { :url => TestServlet.url + '?q=2', :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},
315
+ { :url => TestServlet.url + '?q=3', :post_fields => {'field3' => 'value3', 'field4' => 'value4'}}
316
+ ]
317
+ Curl::Multi.post(urls, {:follow_location => true, :multipart_form_post => true}, {:pipeline => true}) do|easy|
318
+ str = easy.body_str
319
+ assert_match /POST/, str
320
+ fields = {}
321
+ str.gsub(/POST\n/,'').split('&').map{|sv| k, v = sv.split('='); fields[k] = v }
322
+ expected = urls.find{|s| s[:url] == easy.last_effective_url }
323
+ assert_equal expected[:post_fields], fields
324
+ #puts "#{easy.last_effective_url} #{fields.inspect}"
325
+ end
326
+ end
327
+
328
+ def test_multi_easy_put_01
329
+ urls = [{ :url => TestServlet.url, :method => :put, :put_data => "message",
330
+ :headers => {'Content-Type' => 'application/json' } },
331
+ { :url => TestServlet.url, :method => :put, :put_data => "message",
332
+ :headers => {'Content-Type' => 'application/json' } }]
333
+ Curl::Multi.put(urls, {}, {:pipeline => true}) do|easy|
334
+ assert_match /PUT/, easy.body_str
335
+ assert_match /message/, easy.body_str
336
+ end
337
+ end
338
+
339
+ def test_multi_easy_http_01
340
+ urls = [
341
+ { :url => TestServlet.url + '?q=1', :method => :post, :post_fields => {'field1' => 'value1', 'k' => 'j'}},
342
+ { :url => TestServlet.url + '?q=2', :method => :post, :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},
343
+ { :url => TestServlet.url + '?q=3', :method => :post, :post_fields => {'field3' => 'value3', 'field4' => 'value4'}},
344
+ { :url => TestServlet.url, :method => :put, :put_data => "message",
345
+ :headers => {'Content-Type' => 'application/json' } },
346
+ { :url => TestServlet.url, :method => :get }
347
+ ]
348
+ Curl::Multi.http(urls, {:pipeline => true}) do|easy, code, method|
349
+ assert_equal nil, code
350
+ case method
351
+ when :post
352
+ assert_match /POST/, easy.body_str
353
+ when :get
354
+ assert_match /GET/, easy.body_str
355
+ when :put
356
+ assert_match /PUT/, easy.body_str
357
+ end
358
+ #puts "#{easy.body_str.inspect}, #{method.inspect}, #{code.inspect}"
359
+ end
360
+ end
361
+
362
+ def test_mutli_recieves_500
363
+ m = Curl::Multi.new
364
+ e = Curl::Easy.new("http://127.0.0.1:9129/methods")
365
+ failure = false
366
+ e.post_body = "hello=world&s=500"
367
+ e.on_failure{|c,r| failure = true }
368
+ e.on_success{|c| failure = false }
369
+ m.add(e)
370
+ m.perform
371
+ assert failure
372
+ e2 = Curl::Easy.new(TestServlet.url)
373
+ e2.post_body = "hello=world"
374
+ e2.on_failure{|c,r| failure = true }
375
+ m.add(e2)
376
+ m.perform
377
+ failure = false
378
+ assert !failure
379
+ assert_equal "POST\nhello=world", e2.body_str
380
+ end
381
+
382
+ def test_remove_exception_is_descriptive
383
+ m = Curl::Multi.new
384
+ c = Curl::Easy.new("http://blah.com")
385
+ m.remove(c)
386
+ rescue => e
387
+ assert_equal 'Invalid easy handle', e.message
388
+ assert_equal 0, m.requests.size
389
+ end
390
+
391
+ def test_retry_easy_handle
392
+ m = Curl::Multi.new
393
+
394
+ tries = 2
395
+
396
+ c1 = Curl::Easy.new('http://127.9.9.9') do |curl|
397
+ curl.on_failure {|c,e|
398
+ assert_equal [Curl::Err::ConnectionFailedError, "Couldn't connect to server"], e
399
+ if tries > 0
400
+ tries -= 1
401
+ m.add(c)
402
+ end
403
+ }
404
+ end
405
+
406
+ tries -= 1
407
+ m.add(c1)
408
+
409
+ while not m.requests.empty?
410
+ m.perform
411
+ end
412
+ assert_equal 0, tries
413
+ end
414
+
415
+ include TestServerMethods
416
+
417
+ def setup
418
+ server_setup
419
+ end
420
+
421
+ end
@@ -0,0 +1,141 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlPostfield < Test::Unit::TestCase
4
+ def test_private_new
5
+ assert_raise(NoMethodError) { Curl::PostField.new }
6
+ end
7
+
8
+ def test_new_content_01
9
+ pf = Curl::PostField.content('foo', 'bar')
10
+
11
+ assert_equal 'foo', pf.name
12
+ assert_equal 'bar', pf.content
13
+ assert_nil pf.content_type
14
+ assert_nil pf.local_file
15
+ assert_nil pf.remote_file
16
+ assert_nil pf.set_content_proc
17
+ end
18
+
19
+ def test_new_content_02
20
+ pf = Curl::PostField.content('foo', 'bar', 'text/html')
21
+
22
+ assert_equal 'foo', pf.name
23
+ assert_equal 'bar', pf.content
24
+ assert_equal 'text/html', pf.content_type
25
+ assert_nil pf.local_file
26
+ assert_nil pf.remote_file
27
+ assert_nil pf.set_content_proc
28
+ end
29
+
30
+ def test_new_content_03
31
+ l = lambda { |field| "never gets run" }
32
+ pf = Curl::PostField.content('foo', &l)
33
+
34
+ assert_equal 'foo', pf.name
35
+ assert_nil pf.content
36
+ assert_nil pf.content_type
37
+ assert_nil pf.local_file
38
+ assert_nil pf.remote_file
39
+
40
+ # N.B. This doesn't just get the proc, but also removes it.
41
+ assert_equal l, pf.set_content_proc
42
+ end
43
+
44
+ def test_new_content_04
45
+ l = lambda { |field| "never gets run" }
46
+ pf = Curl::PostField.content('foo', 'text/html', &l)
47
+
48
+ assert_equal 'foo', pf.name
49
+ assert_nil pf.content
50
+ assert_equal 'text/html', pf.content_type
51
+ assert_nil pf.local_file
52
+ assert_nil pf.remote_file
53
+
54
+ # N.B. This doesn't just get the proc, but also removes it.
55
+ assert_equal l, pf.set_content_proc
56
+ end
57
+
58
+
59
+ def test_new_file_01
60
+ pf = Curl::PostField.file('foo', 'localname')
61
+
62
+ assert_equal 'foo', pf.name
63
+ assert_equal 'localname', pf.local_file
64
+ assert_equal 'localname', pf.remote_file
65
+ assert_nil pf.content_type
66
+ assert_nil pf.content
67
+ assert_nil pf.set_content_proc
68
+ end
69
+
70
+ def test_new_file_02
71
+ pf = Curl::PostField.file('foo', 'localname', 'remotename')
72
+
73
+ assert_equal 'foo', pf.name
74
+ assert_equal 'localname', pf.local_file
75
+ assert_equal 'remotename', pf.remote_file
76
+ assert_nil pf.content_type
77
+ assert_nil pf.content
78
+ assert_nil pf.set_content_proc
79
+ end
80
+
81
+ def test_new_file_03
82
+ l = lambda { |field| "never gets run" }
83
+ pf = Curl::PostField.file('foo', 'remotename', &l)
84
+
85
+ assert_equal 'foo', pf.name
86
+ assert_equal 'remotename', pf.remote_file
87
+ assert_nil pf.local_file
88
+ assert_nil pf.content_type
89
+ assert_nil pf.content
90
+
91
+ # N.B. This doesn't just get the proc, but also removes it.
92
+ assert_equal l, pf.set_content_proc
93
+ end
94
+
95
+ def test_new_file_04
96
+ assert_raise(ArgumentError) do
97
+ # no local name, no block
98
+ Curl::PostField.file('foo')
99
+ end
100
+
101
+ assert_raise(ArgumentError) do
102
+ # no remote name with block
103
+ Curl::PostField.file('foo') { |field| "never runs" }
104
+ end
105
+ end
106
+
107
+ def test_new_file_05
108
+ # local gets ignored when supplying a block, but remote
109
+ # is still set up properly.
110
+ l = lambda { |field| "never runs" }
111
+ pf = Curl::PostField.file('foo', 'local', 'remote', &l)
112
+
113
+ assert_equal 'foo', pf.name
114
+ assert_equal 'remote', pf.remote_file
115
+ assert_nil pf.local_file
116
+ assert_nil pf.content_type
117
+ assert_nil pf.content
118
+
119
+ assert_equal l, pf.set_content_proc
120
+ end
121
+
122
+ def test_to_s_01
123
+ pf = Curl::PostField.content('foo', 'bar')
124
+ assert_equal "foo=bar", pf.to_s
125
+ end
126
+
127
+ def test_to_s_02
128
+ pf = Curl::PostField.content('foo', 'bar ton')
129
+ assert_equal "foo=bar%20ton", pf.to_s
130
+ end
131
+
132
+ def test_to_s_03
133
+ pf = Curl::PostField.content('foo') { |field| field.name.upcase + "BAR" }
134
+ assert_equal "foo=FOOBAR", pf.to_s
135
+ end
136
+
137
+ def test_to_s_04
138
+ pf = Curl::PostField.file('foo.file', 'bar.file')
139
+ assert_raise(Curl::Err::InvalidPostFieldError) { pf.to_s }
140
+ end
141
+ end