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.
@@ -0,0 +1,461 @@
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_download_01
312
+ # test collecting response buffers to file e.g. on_body
313
+ root_uri = 'http://127.0.0.1:9129/ext/'
314
+ urls = []
315
+ downloads = []
316
+ file_info = {}
317
+ FileUtils.mkdir("tmp/")
318
+ Dir[File.dirname(__FILE__) + "/../ext/*.c"].each do|path|
319
+ urls << (root_uri + File.basename(path))
320
+ downloads << "tmp/" + File.basename(path)
321
+ file_info[File.basename(path)] = {:size => File.size(path)}
322
+ end
323
+ Curl::Multi.download(urls,{},{},downloads) do|curl,download_path|
324
+ assert_equal 200, curl.response_code
325
+ assert File.exist?(download_path)
326
+ assert file_info[File.basename(download_path)][:size], File.size(download_path)
327
+ end
328
+ ensure
329
+ FileUtils.rm_rf("tmp/")
330
+ end
331
+
332
+ def test_multi_easy_post_01
333
+ urls = [
334
+ { :url => TestServlet.url + '?q=1', :post_fields => {'field1' => 'value1', 'k' => 'j'}},
335
+ { :url => TestServlet.url + '?q=2', :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},
336
+ { :url => TestServlet.url + '?q=3', :post_fields => {'field3' => 'value3', 'field4' => 'value4'}}
337
+ ]
338
+ Curl::Multi.post(urls, {:follow_location => true, :multipart_form_post => true}, {:pipeline => true}) do|easy|
339
+ str = easy.body_str
340
+ assert_match /POST/, str
341
+ fields = {}
342
+ str.gsub(/POST\n/,'').split('&').map{|sv| k, v = sv.split('='); fields[k] = v }
343
+ expected = urls.find{|s| s[:url] == easy.last_effective_url }
344
+ assert_equal expected[:post_fields], fields
345
+ #puts "#{easy.last_effective_url} #{fields.inspect}"
346
+ end
347
+ end
348
+
349
+ def test_multi_easy_put_01
350
+ urls = [{ :url => TestServlet.url, :method => :put, :put_data => "message",
351
+ :headers => {'Content-Type' => 'application/json' } },
352
+ { :url => TestServlet.url, :method => :put, :put_data => "message",
353
+ :headers => {'Content-Type' => 'application/json' } }]
354
+ Curl::Multi.put(urls, {}, {:pipeline => true}) do|easy|
355
+ assert_match /PUT/, easy.body_str
356
+ assert_match /message/, easy.body_str
357
+ end
358
+ end
359
+
360
+ def test_multi_easy_http_01
361
+ urls = [
362
+ { :url => TestServlet.url + '?q=1', :method => :post, :post_fields => {'field1' => 'value1', 'k' => 'j'}},
363
+ { :url => TestServlet.url + '?q=2', :method => :post, :post_fields => {'field2' => 'value2', 'foo' => 'bar', 'i' => 'j' }},
364
+ { :url => TestServlet.url + '?q=3', :method => :post, :post_fields => {'field3' => 'value3', 'field4' => 'value4'}},
365
+ { :url => TestServlet.url, :method => :put, :put_data => "message",
366
+ :headers => {'Content-Type' => 'application/json' } },
367
+ { :url => TestServlet.url, :method => :get }
368
+ ]
369
+ Curl::Multi.http(urls, {:pipeline => true}) do|easy, code, method|
370
+ assert_equal nil, code
371
+ case method
372
+ when :post
373
+ assert_match /POST/, easy.body_str
374
+ when :get
375
+ assert_match /GET/, easy.body_str
376
+ when :put
377
+ assert_match /PUT/, easy.body_str
378
+ end
379
+ #puts "#{easy.body_str.inspect}, #{method.inspect}, #{code.inspect}"
380
+ end
381
+ end
382
+
383
+ def test_mutli_recieves_500
384
+ m = Curl::Multi.new
385
+ e = Curl::Easy.new("http://127.0.0.1:9129/methods")
386
+ failure = false
387
+ e.post_body = "hello=world&s=500"
388
+ e.on_failure{|c,r| failure = true }
389
+ e.on_success{|c| failure = false }
390
+ m.add(e)
391
+ m.perform
392
+ assert failure
393
+ e2 = Curl::Easy.new(TestServlet.url)
394
+ e2.post_body = "hello=world"
395
+ e2.on_failure{|c,r| failure = true }
396
+ m.add(e2)
397
+ m.perform
398
+ failure = false
399
+ assert !failure
400
+ assert_equal "POST\nhello=world", e2.body_str
401
+ end
402
+
403
+ def test_remove_exception_is_descriptive
404
+ m = Curl::Multi.new
405
+ c = Curl::Easy.new("http://127.9.9.9:999110")
406
+ m.remove(c)
407
+ rescue => e
408
+ assert_equal 'Invalid easy handle', e.message
409
+ assert_equal 0, m.requests.size
410
+ end
411
+
412
+ def test_retry_easy_handle
413
+ m = Curl::Multi.new
414
+
415
+ tries = 10
416
+
417
+ c1 = Curl::Easy.new('http://127.1.1.1:99911') do |curl|
418
+ curl.on_failure {|c,e|
419
+ assert_equal [Curl::Err::MalformedURLError, "URL using bad/illegal format or missing URL"], e
420
+ if tries > 0
421
+ tries -= 1
422
+ m.add(c)
423
+ end
424
+ }
425
+ end
426
+
427
+ tries -= 1
428
+ m.add(c1)
429
+
430
+ m.perform
431
+ assert_equal 0, tries
432
+ assert_equal 0, m.requests.size
433
+ end
434
+
435
+ def test_reusing_handle
436
+ m = Curl::Multi.new
437
+
438
+ c = Curl::Easy.new('http://127.0.0.1') do|easy|
439
+ easy.on_complete{|e,r| puts e.inspect }
440
+ end
441
+
442
+ m.add(c)
443
+ m.add(c)
444
+ rescue => e
445
+ assert_equal Curl::Err::MultiBadEasyHandle, e.class
446
+ end
447
+
448
+ def test_multi_default_timeout
449
+ assert_equal 100, Curl::Multi.default_timeout
450
+ Curl::Multi.default_timeout = 12
451
+ assert_equal 12, Curl::Multi.default_timeout
452
+ assert_equal 100, (Curl::Multi.default_timeout = 100)
453
+ end
454
+
455
+ include TestServerMethods
456
+
457
+ def setup
458
+ server_setup
459
+ end
460
+
461
+ end