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,65 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ #require 'rubygems'
3
+ #require 'rmem'
4
+
5
+ #
6
+ # Run some tests to measure the memory usage of curb, these tests require fork and ps
7
+ #
8
+ class TestCurbMemory < Test::Unit::TestCase
9
+
10
+ def test_easy_memory
11
+ easy_avg, easy_std = measure_object_memory(Curl::Easy)
12
+ printf "Easy average: %.2f kilobytes +/- %.2f kilobytes\n", easy_avg.to_f, easy_std.to_f
13
+
14
+ multi_avg, multi_std = measure_object_memory(Curl::Multi)
15
+ printf "Multi average: %.2f kilobytes +/- %.2f kilobytes\n", multi_avg.to_f, multi_std.to_f
16
+
17
+ # now that we have the average size of an easy handle lets see how much a multi request consumes with 10 requests
18
+ end
19
+
20
+ def c_avg(report)
21
+ sum = 0
22
+ report.each {|r| sum += r.last }
23
+ (sum.to_f / report.size)
24
+ end
25
+
26
+ def c_std(report,avg)
27
+ var = 0
28
+ report.each {|r| var += (r.last-avg)*(r.last-avg) }
29
+ Math.sqrt(var / (report.size-1))
30
+ end
31
+
32
+ def measure_object_memory(klass)
33
+ report = []
34
+ 200.times do
35
+ res = mem_check do
36
+ obj = klass.new
37
+ end
38
+ report << res
39
+ end
40
+ avg = c_avg(report)
41
+ std = c_std(report,avg)
42
+ [avg,std]
43
+ end
44
+
45
+ def mem_check
46
+ # see: http://gist.github.com/264060 for inspiration of ps command line
47
+ rd, wr = IO.pipe
48
+ memory_usage = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
49
+ fork do
50
+ before = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
51
+ rd.close
52
+ yield
53
+ after = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
54
+ wr.write((after - before))
55
+ wr.flush
56
+ wr.close
57
+ end
58
+ wr.close
59
+ total = rd.read.to_i
60
+ rd.close
61
+ Process.wait
62
+ # return the delta and the total
63
+ [memory_usage, total]
64
+ end
65
+ end
@@ -0,0 +1,36 @@
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
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'ext')))
25
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
26
+ require 'curb'
27
+ require 'uri'
28
+
29
+ url = 'https://www.google.com/accounts/ServiceLoginAuth'
30
+
31
+ c = Curl::Easy.http_post('https://www.google.com/accounts/ServiceLoginAuth',
32
+ Curl:: PostField.content('ltmpl','m_blanco')) #do
33
+ # end
34
+
35
+ puts "success"
36
+
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlDownload < Test::Unit::TestCase
4
+ include TestServerMethods
5
+
6
+ def setup
7
+ server_setup
8
+ end
9
+
10
+ def test_download_url_to_file
11
+ dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
12
+ dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
13
+
14
+ curb = Curl::Easy.download(dl_url, dl_path)
15
+ assert File.exist?(dl_path)
16
+ assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
17
+ ensure
18
+ File.unlink(dl_path) if File.exist?(dl_path)
19
+ end
20
+
21
+ def test_download_bad_url_gives_404
22
+ dl_url = "http://127.0.0.1:9129/this_file_does_not_exist.html"
23
+ dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
24
+
25
+ curb = Curl::Easy.download(dl_url, dl_path)
26
+ assert_equal Curl::Easy, curb.class
27
+ assert_equal 404, curb.response_code
28
+ ensure
29
+ File.unlink(dl_path) if File.exist?(dl_path)
30
+ end
31
+
32
+ end
@@ -0,0 +1,778 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class TestCurbCurlEasy < Test::Unit::TestCase
4
+ def test_class_perform_01
5
+ assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)
6
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
7
+ assert_equal "", c.header_str
8
+ end
9
+
10
+ def test_class_perform_02
11
+ data = ""
12
+ assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL) { |curl| curl.on_body { |d| data << d; d.length } }
13
+
14
+ assert_nil c.body_str
15
+ assert_equal "", c.header_str
16
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
17
+ end
18
+
19
+ def test_class_perform_03
20
+ assert_raise(Curl::Err::CouldntReadError) { c = Curl::Easy.perform($TEST_URL + "nonexistent") }
21
+ end
22
+
23
+ def test_new_01
24
+ c = Curl::Easy.new
25
+ assert_equal Curl::Easy, c.class
26
+ assert_nil c.url
27
+ assert_nil c.body_str
28
+ assert_nil c.header_str
29
+ end
30
+
31
+ def test_new_02
32
+ c = Curl::Easy.new($TEST_URL)
33
+ assert_equal $TEST_URL, c.url
34
+ end
35
+
36
+ def test_new_03
37
+ blk = lambda { |i| i.length }
38
+
39
+ c = Curl::Easy.new do |curl|
40
+ curl.on_body(&blk)
41
+ end
42
+
43
+ assert_nil c.url
44
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
45
+ assert_equal nil, c.on_body
46
+ end
47
+
48
+ def test_new_04
49
+ blk = lambda { |i| i.length }
50
+
51
+ c = Curl::Easy.new($TEST_URL) do |curl|
52
+ curl.on_body(&blk)
53
+ end
54
+
55
+ assert_equal $TEST_URL, c.url
56
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
57
+ assert_equal nil, c.on_body
58
+ end
59
+
60
+ class Foo < Curl::Easy ; end
61
+ def test_new_05
62
+ # can use Curl::Easy as a base class
63
+ c = Foo.new
64
+ assert_equal Foo, c.class
65
+ c.url = $TEST_URL
66
+ c.perform
67
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
68
+ end
69
+
70
+ # test invalid use of new
71
+ def test_new_06
72
+ Curl::Easy.new(TestServlet.url) do|curl|
73
+ curl.http_post
74
+ assert_equal "POST\n", curl.body_str
75
+ end
76
+ end
77
+
78
+ def test_escape
79
+ c = Curl::Easy.new
80
+
81
+ assert_equal "one%20two", c.escape('one two')
82
+ assert_equal "one%00two%20three", c.escape("one\000two three")
83
+ end
84
+
85
+ def test_unescape
86
+ c = Curl::Easy.new
87
+
88
+ assert_equal "one two", c.unescape('one%20two')
89
+
90
+ # prior to 7.15.4 embedded nulls cannot be unescaped
91
+ if Curl::VERNUM >= 0x070f04
92
+ assert_equal "one\000two three", c.unescape("one%00two%20three")
93
+ end
94
+ end
95
+
96
+ def test_headers
97
+ c = Curl::Easy.new($TEST_URL)
98
+
99
+ assert_equal({}, c.headers)
100
+ c.headers = "Expect:"
101
+ assert_equal "Expect:", c.headers
102
+ c.headers = ["Expect:", "User-Agent: myapp-0.0.0"]
103
+ assert_equal ["Expect:", "User-Agent: myapp-0.0.0"], c.headers
104
+ end
105
+
106
+ def test_get_01
107
+ c = Curl::Easy.new($TEST_URL)
108
+ assert_equal true, c.http_get
109
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
110
+ assert_equal "", c.header_str
111
+ end
112
+
113
+ def test_get_02
114
+ data = ""
115
+ c = Curl::Easy.new($TEST_URL) do |curl|
116
+ curl.on_body { |d| data << d; d.length }
117
+ end
118
+
119
+ assert_equal true, c.http_get
120
+
121
+ assert_nil c.body_str
122
+ assert_equal "", c.header_str
123
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
124
+ end
125
+
126
+ def test_get_03
127
+ c = Curl::Easy.new($TEST_URL + "nonexistent")
128
+ assert_raise(Curl::Err::CouldntReadError) { c.http_get }
129
+ assert_equal "", c.body_str
130
+ assert_equal "", c.header_str
131
+ end
132
+
133
+
134
+ def test_last_effective_url_01
135
+ c = Curl::Easy.new($TEST_URL)
136
+
137
+ assert_equal $TEST_URL, c.url
138
+ assert_nil c.last_effective_url
139
+
140
+ assert c.http_get
141
+
142
+ assert_equal c.url, c.last_effective_url
143
+ c.url = "file://some/new.url"
144
+
145
+ assert_not_equal c.last_effective_url, c.url
146
+ end
147
+
148
+ def test_http_get_block
149
+ curl = Curl::Easy.http_get(TestServlet.url) do|c|
150
+ c.follow_location = true
151
+ c.max_redirects = 3
152
+ end
153
+ assert_equal curl.url, curl.last_effective_url
154
+ assert_equal 'GET', curl.body_str
155
+ end
156
+
157
+ def test_local_port_01
158
+ c = Curl::Easy.new($TEST_URL)
159
+
160
+ assert_nil c.local_port
161
+ assert_nil c.local_port_range
162
+ assert_nil c.proxy_port
163
+
164
+ c.local_port = 88
165
+
166
+ assert_equal 88, c.local_port
167
+ assert_nil c.local_port_range
168
+ assert_nil c.proxy_port
169
+
170
+ c.local_port = nil
171
+
172
+ assert_nil c.local_port
173
+ assert_nil c.local_port_range
174
+ assert_nil c.proxy_port
175
+ end
176
+
177
+ def test_local_port_02
178
+ c = Curl::Easy.new($TEST_URL)
179
+
180
+ assert_nil c.local_port
181
+ assert_raise(ArgumentError) { c.local_port = 0 }
182
+ assert_raise(ArgumentError) { c.local_port = 65536 }
183
+ assert_raise(ArgumentError) { c.local_port = -1 }
184
+ end
185
+
186
+ def test_local_port_range_01
187
+ c = Curl::Easy.new($TEST_URL)
188
+
189
+ assert_nil c.local_port_range
190
+ assert_nil c.local_port
191
+ assert_nil c.proxy_port
192
+
193
+ c.local_port_range = 88
194
+ assert_equal 88, c.local_port_range
195
+ assert_nil c.local_port
196
+ assert_nil c.proxy_port
197
+
198
+ c.local_port_range = nil
199
+
200
+ assert_nil c.local_port_range
201
+ assert_nil c.local_port
202
+ assert_nil c.proxy_port
203
+ end
204
+
205
+ def test_local_port_range_02
206
+ c = Curl::Easy.new($TEST_URL)
207
+
208
+ assert_nil c.local_port_range
209
+ assert_raise(ArgumentError) { c.local_port_range = 0 }
210
+ assert_raise(ArgumentError) { c.local_port_range = 65536 }
211
+ assert_raise(ArgumentError) { c.local_port_range = -1 }
212
+ end
213
+
214
+ def test_proxy_url_01
215
+ c = Curl::Easy.new($TEST_URL)
216
+
217
+ assert_equal $TEST_URL, c.url
218
+ assert_nil c.proxy_url
219
+
220
+ c.proxy_url = "http://some.proxy"
221
+
222
+ assert_equal $TEST_URL, c.url
223
+ assert_equal "http://some.proxy", c.proxy_url
224
+
225
+ c.proxy_url = nil
226
+ assert_equal $TEST_URL, c.url
227
+ assert_nil c.proxy_url
228
+ end
229
+
230
+ def test_proxy_port_01
231
+ c = Curl::Easy.new($TEST_URL)
232
+
233
+ assert_nil c.local_port
234
+ assert_nil c.local_port_range
235
+ assert_nil c.proxy_port
236
+
237
+ c.proxy_port = 88
238
+
239
+ assert_equal 88, c.proxy_port
240
+ assert_nil c.local_port
241
+ assert_nil c.local_port_range
242
+
243
+ c.proxy_port = nil
244
+ assert_nil c.proxy_port
245
+ assert_nil c.local_port
246
+ assert_nil c.local_port_range
247
+ end
248
+
249
+ def test_proxy_port_02
250
+ c = Curl::Easy.new($TEST_URL)
251
+
252
+ assert_nil c.proxy_port
253
+ assert_raise(ArgumentError) { c.proxy_port = 0 }
254
+ assert_raise(ArgumentError) { c.proxy_port = 65536 }
255
+ assert_raise(ArgumentError) { c.proxy_port = -1 }
256
+ end
257
+
258
+ def test_proxy_type_01
259
+ c = Curl::Easy.new($TEST_URL)
260
+
261
+ assert_nil c.proxy_type
262
+
263
+ c.proxy_type = 3
264
+ assert_equal 3, c.proxy_type
265
+
266
+ c.proxy_type = nil
267
+ assert_nil c.proxy_type
268
+ end
269
+
270
+ def test_http_auth_types_01
271
+ c = Curl::Easy.new($TEST_URL)
272
+
273
+ assert_nil c.http_auth_types
274
+
275
+ c.http_auth_types = 3
276
+ assert_equal 3, c.http_auth_types
277
+
278
+ c.http_auth_types = nil
279
+ assert_nil c.http_auth_types
280
+ end
281
+
282
+ def test_proxy_auth_types_01
283
+ c = Curl::Easy.new($TEST_URL)
284
+
285
+ assert_nil c.proxy_auth_types
286
+
287
+ c.proxy_auth_types = 3
288
+ assert_equal 3, c.proxy_auth_types
289
+
290
+ c.proxy_auth_types = nil
291
+ assert_nil c.proxy_auth_types
292
+ end
293
+
294
+ def test_max_redirects_01
295
+ c = Curl::Easy.new($TEST_URL)
296
+
297
+ assert_nil c.max_redirects
298
+
299
+ c.max_redirects = 3
300
+ assert_equal 3, c.max_redirects
301
+
302
+ c.max_redirects = nil
303
+ assert_nil c.max_redirects
304
+ end
305
+
306
+ def test_timeout_01
307
+ c = Curl::Easy.new($TEST_URL)
308
+
309
+ assert_nil c.timeout
310
+
311
+ c.timeout = 3
312
+ assert_equal 3, c.timeout
313
+
314
+ c.timeout = nil
315
+ assert_nil c.timeout
316
+ end
317
+
318
+ def test_connect_timeout_01
319
+ c = Curl::Easy.new($TEST_URL)
320
+
321
+ assert_nil c.connect_timeout
322
+
323
+ c.connect_timeout = 3
324
+ assert_equal 3, c.connect_timeout
325
+
326
+ c.connect_timeout = nil
327
+ assert_nil c.connect_timeout
328
+ end
329
+
330
+ def test_ftp_response_timeout_01
331
+ c = Curl::Easy.new($TEST_URL)
332
+
333
+ assert_nil c.ftp_response_timeout
334
+
335
+ c.ftp_response_timeout = 3
336
+ assert_equal 3, c.ftp_response_timeout
337
+
338
+ c.ftp_response_timeout = nil
339
+ assert_nil c.ftp_response_timeout
340
+ end
341
+
342
+ def test_dns_cache_timeout_01
343
+ c = Curl::Easy.new($TEST_URL)
344
+
345
+ assert_equal 60, c.dns_cache_timeout
346
+
347
+ c.dns_cache_timeout = nil
348
+ assert_nil c.dns_cache_timeout
349
+
350
+ c.dns_cache_timeout = 30
351
+ assert_equal 30, c.dns_cache_timeout
352
+ end
353
+
354
+ def test_on_body
355
+ blk = lambda { |i| i.length }
356
+
357
+ c = Curl::Easy.new
358
+ c.on_body(&blk)
359
+
360
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
361
+ assert_equal nil, c.on_body
362
+ end
363
+
364
+ def test_on_header
365
+ blk = lambda { |i| i.length }
366
+
367
+ c = Curl::Easy.new
368
+ c.on_header(&blk)
369
+
370
+ assert_equal blk, c.on_header # sets handler nil, returns old handler
371
+ assert_equal nil, c.on_header
372
+ end
373
+
374
+ def test_on_progress
375
+ blk = lambda { |*args| true }
376
+
377
+ c = Curl::Easy.new
378
+ c.on_progress(&blk)
379
+
380
+ assert_equal blk, c.on_progress # sets handler nil, returns old handler
381
+ assert_equal nil, c.on_progress
382
+ end
383
+
384
+ def test_on_debug
385
+ blk = lambda { |*args| true }
386
+
387
+ c = Curl::Easy.new
388
+ c.on_debug(&blk)
389
+
390
+ assert_equal blk, c.on_debug # sets handler nil, returns old handler
391
+ assert_equal nil, c.on_debug
392
+ end
393
+
394
+ def test_proxy_tunnel
395
+ c = Curl::Easy.new
396
+ assert !c.proxy_tunnel?
397
+ assert c.proxy_tunnel = true
398
+ assert c.proxy_tunnel?
399
+ end
400
+
401
+ def test_fetch_file_time
402
+ c = Curl::Easy.new
403
+ assert !c.fetch_file_time?
404
+ assert c.fetch_file_time = true
405
+ assert c.fetch_file_time?
406
+ end
407
+
408
+ def test_ssl_verify_peer
409
+ c = Curl::Easy.new
410
+ assert c.ssl_verify_peer?
411
+ assert !c.ssl_verify_peer = false
412
+ assert !c.ssl_verify_peer?
413
+ end
414
+
415
+ def test_ssl_verify_host
416
+ c = Curl::Easy.new
417
+ assert c.ssl_verify_host?
418
+ assert !c.ssl_verify_host = false
419
+ assert !c.ssl_verify_host?
420
+ end
421
+
422
+ def test_header_in_body
423
+ c = Curl::Easy.new
424
+ assert !c.header_in_body?
425
+ assert c.header_in_body = true
426
+ assert c.header_in_body?
427
+ end
428
+
429
+ def test_use_netrc
430
+ c = Curl::Easy.new
431
+ assert !c.use_netrc?
432
+ assert c.use_netrc = true
433
+ assert c.use_netrc?
434
+ end
435
+
436
+ def test_follow_location
437
+ c = Curl::Easy.new
438
+ assert !c.follow_location?
439
+ assert c.follow_location = true
440
+ assert c.follow_location?
441
+ end
442
+
443
+ def test_unrestricted_auth
444
+ c = Curl::Easy.new
445
+ assert !c.unrestricted_auth?
446
+ assert c.unrestricted_auth = true
447
+ assert c.unrestricted_auth?
448
+ end
449
+
450
+ def test_multipart_form_post
451
+ c = Curl::Easy.new
452
+ assert !c.multipart_form_post?
453
+ assert c.multipart_form_post = true
454
+ assert c.multipart_form_post?
455
+ end
456
+
457
+ def test_enable_cookies
458
+ c = Curl::Easy.new
459
+ assert !c.enable_cookies?
460
+ assert c.enable_cookies = true
461
+ assert c.enable_cookies?
462
+ end
463
+
464
+ def test_cookies_option
465
+ c = Curl::Easy.new
466
+ assert_nil c.cookies
467
+ assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"
468
+ assert_equal "name1=content1; name2=content2;", c.cookies
469
+ end
470
+
471
+ def test_cookiefile
472
+ c = Curl::Easy.new
473
+ assert_nil c.cookiefile
474
+ assert_equal "some.file", c.cookiefile = "some.file"
475
+ assert_equal "some.file", c.cookiefile
476
+ end
477
+
478
+ def test_cookiejar
479
+ c = Curl::Easy.new
480
+ assert_nil c.cookiejar
481
+ assert_equal "some.file", c.cookiejar = "some.file"
482
+ assert_equal "some.file", c.cookiejar
483
+ end
484
+
485
+ def test_on_success
486
+ curl = Curl::Easy.new($TEST_URL)
487
+ on_success_called = false
488
+ curl.on_success {|c|
489
+ on_success_called = true
490
+ assert_not_nil c.body_str
491
+ assert_equal "", c.header_str
492
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
493
+ }
494
+ curl.perform
495
+ assert on_success_called, "Success handler not called"
496
+ end
497
+
498
+ def test_on_success_with_on_failure
499
+ curl = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")
500
+ on_failure_called = false
501
+ curl.on_success {|c| } # make sure we get the failure call even though this handler is defined
502
+ curl.on_failure {|c,code| on_failure_called = true }
503
+ curl.perform
504
+ assert on_failure_called, "Failure handler not called"
505
+ end
506
+
507
+ def test_get_remote
508
+ curl = Curl::Easy.new(TestServlet.url)
509
+ curl.http_get
510
+ assert_equal 'GET', curl.body_str
511
+ end
512
+
513
+ def test_post_remote
514
+ curl = Curl::Easy.new(TestServlet.url)
515
+ curl.http_post
516
+ assert_equal "POST\n", curl.body_str
517
+ end
518
+
519
+ def test_post_remote_is_easy_handle
520
+ # see: http://pastie.org/560852 and
521
+ # http://groups.google.com/group/curb---ruby-libcurl-bindings/browse_thread/thread/216bb2d9b037f347?hl=en
522
+ [:post, :get,:head,:delete].each do |method|
523
+ count = 0
524
+ curl = Curl::Easy.send("http_#{method}", TestServlet.url) do|c|
525
+ count += 1
526
+ assert_equal Curl::Easy, c.class
527
+ end
528
+ assert_equal 1, count, "For request method: #{method.to_s.upcase}"
529
+ end
530
+ end
531
+
532
+ def test_post_with_body_remote
533
+ curl = Curl::Easy.new(TestServlet.url)
534
+ curl.post_body = 'foo=bar&encoded%20string=val'
535
+
536
+ curl.perform
537
+
538
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
539
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
540
+ end
541
+
542
+ def test_form_post_body_remote
543
+ curl = Curl::Easy.new(TestServlet.url)
544
+ curl.http_post('foo=bar', 'encoded%20string=val')
545
+
546
+ assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str
547
+ assert_equal 'foo=bar&encoded%20string=val', curl.post_body
548
+ end
549
+
550
+ def test_delete_remote
551
+ curl = Curl::Easy.new(TestServlet.url)
552
+ curl.http_delete
553
+ assert_equal 'DELETE', curl.body_str
554
+ end
555
+
556
+ def test_arbitrary_http_verb
557
+ curl = Curl::Easy.new(TestServlet.url)
558
+ curl.http('PURGE')
559
+ assert_equal 'PURGE', curl.body_str
560
+ end
561
+
562
+ def test_head_remote
563
+ curl = Curl::Easy.new(TestServlet.url)
564
+ curl.http_head
565
+
566
+ redirect = curl.header_str.match(/Location: (.*)/)
567
+
568
+ assert_equal '', curl.body_str
569
+ assert_match '/nonexistent', redirect[1]
570
+ end
571
+
572
+ def test_head_accessor
573
+ curl = Curl::Easy.new(TestServlet.url)
574
+ curl.head = true
575
+ curl.perform
576
+
577
+ redirect = curl.header_str.match(/Location: (.*)/)
578
+
579
+ assert_equal '', curl.body_str
580
+ assert_match '/nonexistent', redirect[1]
581
+ curl.head = false
582
+ curl.perform
583
+ assert_equal 'GET', curl.body_str
584
+ end
585
+
586
+ def test_put_remote
587
+ curl = Curl::Easy.new(TestServlet.url)
588
+ curl.headers['Content-Type'] = 'application/json'
589
+ assert curl.http_put("message")
590
+ assert_match /^PUT/, curl.body_str
591
+ assert_match /message$/, curl.body_str
592
+ assert_match /application\/json/, curl.header_str
593
+ end
594
+
595
+ def test_put_data
596
+ curl = Curl::Easy.new(TestServlet.url)
597
+ curl.put_data = 'message'
598
+
599
+ curl.perform
600
+
601
+ assert_match /^PUT/, curl.body_str
602
+ assert_match /message$/, curl.body_str
603
+ end
604
+
605
+ def test_put_remote_file
606
+ curl = Curl::Easy.new(TestServlet.url)
607
+ File.open(__FILE__,'r') do|f|
608
+ assert curl.http_put(f)
609
+ end
610
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
611
+ end
612
+
613
+ def test_put_class_method
614
+ count = 0
615
+ curl = Curl::Easy.http_put(TestServlet.url,File.open(__FILE__,'rb')) do|c|
616
+ count += 1
617
+ assert_equal Curl::Easy, c.class
618
+ end
619
+ assert_equal 1, count
620
+ assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str
621
+ end
622
+
623
+ # Generate a self-signed cert with
624
+ # openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 \
625
+ # -keyout tests/cert.pem -out tests/cert.pem
626
+ def test_cert
627
+ curl = Curl::Easy.new(TestServlet.url)
628
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem")
629
+ assert /cert.pem$/,curl.cert
630
+ end
631
+
632
+ def test_cert_with_password
633
+ curl = Curl::Easy.new(TestServlet.url)
634
+ curl.cert= File.join(File.dirname(__FILE__),"cert.pem:password")
635
+ assert /cert.pem$/,curl.cert
636
+ end
637
+
638
+ def test_cert_type
639
+ curl = Curl::Easy.new(TestServlet.url)
640
+ curl.certtype= "DER"
641
+ assert "DER", curl.certtype
642
+ end
643
+
644
+ def test_default_certtype
645
+ curl = Curl::Easy.new(TestServlet.url)
646
+ assert "PEM", curl.certtype
647
+ end
648
+
649
+ # Generate a CA cert with instructions at
650
+ # http://technocage.com/~caskey/openssl/
651
+ def test_ca_cert
652
+ curl = Curl::Easy.new(TestServlet.url)
653
+ curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")
654
+ assert /cacert.pem$/, curl.cacert
655
+ end
656
+
657
+ def test_user_agent
658
+ curl = Curl::Easy.new(TestServlet.url)
659
+ curl.useragent= "Curb-Easy/Ruby"
660
+ assert /ScrubDog$/,curl.useragent
661
+ end
662
+
663
+ def test_username_password
664
+ curl = Curl::Easy.new(TestServlet.url)
665
+ curl.username = "foo"
666
+ curl.password = "bar"
667
+ if !curl.username.nil?
668
+ assert_equal "foo", curl.username
669
+ assert_equal "bar", curl.password
670
+ else
671
+ curl.userpwd = "foo:bar"
672
+ end
673
+ curl.http_auth_types = :basic
674
+ #curl.verbose = true
675
+ curl.perform
676
+ assert_equal 'Basic Zm9vOmJhcg==', $auth_header
677
+ $auth_header = nil
678
+ # curl checks the auth type supported by the server, so we have to create a
679
+ # new easy handle if we're going to change the auth type...
680
+
681
+ curl = Curl::Easy.new(TestServlet.url)
682
+ curl.username = "foo"
683
+ curl.password = "bar"
684
+ if curl.username.nil?
685
+ curl.userpwd = "foo:bar"
686
+ end
687
+ curl.http_auth_types = :ntlm
688
+ curl.perform
689
+ assert_equal 'NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=', $auth_header
690
+ end
691
+
692
+ def test_primary_ip
693
+ curl = Curl::Easy.new(TestServlet.url)
694
+ if curl.respond_to?(:primary_ip)
695
+ curl.perform
696
+ assert_equal '127.0.0.1', curl.primary_ip
697
+ end
698
+ end
699
+
700
+ def test_post_streaming
701
+ readme = File.expand_path(File.join(File.dirname(__FILE__),'..','README'))
702
+
703
+ pf = Curl::PostField.file("filename", readme)
704
+
705
+ easy = Curl::Easy.new
706
+
707
+ easy.url = TestServlet.url
708
+ easy.multipart_form_post = true
709
+ easy.http_post(pf)
710
+
711
+ assert_not_equal(0,easy.body_str.size)
712
+ assert_equal(easy.body_str,File.read(readme))
713
+ end
714
+
715
+
716
+ def test_easy_close
717
+ easy = Curl::Easy.new
718
+ easy.close
719
+ easy.url = TestServlet.url
720
+ easy.http_get
721
+ end
722
+
723
+ def test_easy_reset
724
+ easy = Curl::Easy.new
725
+ easy.url = TestServlet.url + "?query=foo"
726
+ easy.http_get
727
+ settings = easy.reset
728
+ assert settings.key?(:url)
729
+ assert settings.key?(:body_data)
730
+ assert settings.key?(:header_data)
731
+ easy.url = TestServlet.url
732
+ easy.http_get
733
+ end
734
+
735
+ def test_easy_use_http_versions
736
+ easy = Curl::Easy.new
737
+ easy.url = TestServlet.url + "?query=foo"
738
+ #puts "http none: #{Curl::HTTP_NONE.inspect}"
739
+ #puts "http1.0: #{Curl::HTTP_1_0.inspect}"
740
+ #puts "http1.1: #{Curl::HTTP_1_1.inspect}"
741
+ easy.version = Curl::HTTP_1_1
742
+ #easy.verbose = true
743
+ easy.http_get
744
+ end
745
+
746
+ def test_easy_http_verbs
747
+ curl = Curl::Easy.new(TestServlet.url)
748
+ curl.http_delete
749
+ assert_equal 'DELETE', curl.body_str
750
+ curl.http_get
751
+ assert_equal 'GET', curl.body_str
752
+ curl.http_post
753
+ assert_equal "POST\n", curl.body_str
754
+ curl.http('PURGE')
755
+ assert_equal 'PURGE', curl.body_str
756
+ curl.http_put('hello')
757
+ assert_equal "PUT\nhello", curl.body_str
758
+ end
759
+
760
+ # http://github.com/taf2/curb/issues/#issue/33
761
+ def test_easy_http_verbs_with_errors
762
+ curl = Curl::Easy.new("http://127.0.0.1:9012/") # test will fail if http server on port 9012
763
+ assert_raise Curl::Err::ConnectionFailedError do
764
+ curl.http_delete
765
+ end
766
+ curl.url = TestServlet.url
767
+ curl.http_get
768
+ assert_equal 'GET', curl.body_str
769
+ end
770
+
771
+
772
+ include TestServerMethods
773
+
774
+ def setup
775
+ server_setup
776
+ end
777
+
778
+ end