taf2-curb 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,524 @@
1
+ require 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_nil c.url
26
+ assert_nil c.body_str
27
+ assert_nil c.header_str
28
+ end
29
+
30
+ def test_new_02
31
+ c = Curl::Easy.new($TEST_URL)
32
+ assert_equal $TEST_URL, c.url
33
+ end
34
+
35
+ def test_new_03
36
+ blk = lambda { |i| i.length }
37
+
38
+ c = Curl::Easy.new do |curl|
39
+ curl.on_body(&blk)
40
+ end
41
+
42
+ assert_nil c.url
43
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
44
+ assert_equal nil, c.on_body
45
+ end
46
+
47
+ def test_new_04
48
+ blk = lambda { |i| i.length }
49
+
50
+ c = Curl::Easy.new($TEST_URL) do |curl|
51
+ curl.on_body(&blk)
52
+ end
53
+
54
+ assert_equal $TEST_URL, c.url
55
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
56
+ assert_equal nil, c.on_body
57
+ end
58
+
59
+ def test_escape
60
+ c = Curl::Easy.new
61
+
62
+ assert_equal "one%20two", c.escape('one two')
63
+ assert_equal "one%00two%20three", c.escape("one\000two three")
64
+ end
65
+
66
+ def test_unescape
67
+ c = Curl::Easy.new
68
+
69
+ assert_equal "one two", c.unescape('one%20two')
70
+
71
+ # prior to 7.15.4 embedded nulls cannot be unescaped
72
+ if Curl::VERNUM >= 0x070f04
73
+ assert_equal "one\000two three", c.unescape("one%00two%20three")
74
+ end
75
+ end
76
+
77
+ def test_headers
78
+ c = Curl::Easy.new($TEST_URL)
79
+
80
+ assert_equal({}, c.headers)
81
+ c.headers = "Expect:"
82
+ assert_equal "Expect:", c.headers
83
+ c.headers = ["Expect:", "User-Agent: myapp-0.0.0"]
84
+ assert_equal ["Expect:", "User-Agent: myapp-0.0.0"], c.headers
85
+ end
86
+
87
+ def test_get_01
88
+ c = Curl::Easy.new($TEST_URL)
89
+ assert_equal true, c.http_get
90
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
91
+ assert_equal "", c.header_str
92
+ end
93
+
94
+ def test_get_02
95
+ data = ""
96
+ c = Curl::Easy.new($TEST_URL) do |curl|
97
+ curl.on_body { |d| data << d; d.length }
98
+ end
99
+
100
+ assert_equal true, c.http_get
101
+
102
+ assert_nil c.body_str
103
+ assert_equal "", c.header_str
104
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)
105
+ end
106
+
107
+ def test_get_03
108
+ c = Curl::Easy.new($TEST_URL + "nonexistent")
109
+ assert_raise(Curl::Err::CouldntReadError) { c.http_get }
110
+ assert_equal "", c.body_str
111
+ assert_equal "", c.header_str
112
+ end
113
+
114
+ def test_last_effective_url_01
115
+ c = Curl::Easy.new($TEST_URL)
116
+
117
+ assert_equal $TEST_URL, c.url
118
+ assert_nil c.last_effective_url
119
+
120
+ assert c.http_get
121
+
122
+ assert_equal c.url, c.last_effective_url
123
+ c.url = "file://some/new.url"
124
+
125
+ assert_not_equal c.last_effective_url, c.url
126
+ end
127
+
128
+ def test_local_port_01
129
+ c = Curl::Easy.new($TEST_URL)
130
+
131
+ assert_nil c.local_port
132
+ assert_nil c.local_port_range
133
+ assert_nil c.proxy_port
134
+
135
+ c.local_port = 88
136
+
137
+ assert_equal 88, c.local_port
138
+ assert_nil c.local_port_range
139
+ assert_nil c.proxy_port
140
+
141
+ c.local_port = nil
142
+
143
+ assert_nil c.local_port
144
+ assert_nil c.local_port_range
145
+ assert_nil c.proxy_port
146
+ end
147
+
148
+ def test_local_port_02
149
+ c = Curl::Easy.new($TEST_URL)
150
+
151
+ assert_nil c.local_port
152
+ assert_raise(ArgumentError) { c.local_port = 0 }
153
+ assert_raise(ArgumentError) { c.local_port = 65536 }
154
+ assert_raise(ArgumentError) { c.local_port = -1 }
155
+ end
156
+
157
+ def test_local_port_range_01
158
+ c = Curl::Easy.new($TEST_URL)
159
+
160
+ assert_nil c.local_port_range
161
+ assert_nil c.local_port
162
+ assert_nil c.proxy_port
163
+
164
+ c.local_port_range = 88
165
+ assert_equal 88, c.local_port_range
166
+ assert_nil c.local_port
167
+ assert_nil c.proxy_port
168
+
169
+ c.local_port_range = nil
170
+
171
+ assert_nil c.local_port_range
172
+ assert_nil c.local_port
173
+ assert_nil c.proxy_port
174
+ end
175
+
176
+ def test_local_port_range_02
177
+ c = Curl::Easy.new($TEST_URL)
178
+
179
+ assert_nil c.local_port_range
180
+ assert_raise(ArgumentError) { c.local_port_range = 0 }
181
+ assert_raise(ArgumentError) { c.local_port_range = 65536 }
182
+ assert_raise(ArgumentError) { c.local_port_range = -1 }
183
+ end
184
+
185
+ def test_proxy_url_01
186
+ c = Curl::Easy.new($TEST_URL)
187
+
188
+ assert_equal $TEST_URL, c.url
189
+ assert_nil c.proxy_url
190
+
191
+ c.proxy_url = "http://some.proxy"
192
+
193
+ assert_equal $TEST_URL, c.url
194
+ assert_equal "http://some.proxy", c.proxy_url
195
+
196
+ c.proxy_url = nil
197
+ assert_equal $TEST_URL, c.url
198
+ assert_nil c.proxy_url
199
+ end
200
+
201
+ def test_proxy_port_01
202
+ c = Curl::Easy.new($TEST_URL)
203
+
204
+ assert_nil c.local_port
205
+ assert_nil c.local_port_range
206
+ assert_nil c.proxy_port
207
+
208
+ c.proxy_port = 88
209
+
210
+ assert_equal 88, c.proxy_port
211
+ assert_nil c.local_port
212
+ assert_nil c.local_port_range
213
+
214
+ c.proxy_port = nil
215
+ assert_nil c.proxy_port
216
+ assert_nil c.local_port
217
+ assert_nil c.local_port_range
218
+ end
219
+
220
+ def test_proxy_port_02
221
+ c = Curl::Easy.new($TEST_URL)
222
+
223
+ assert_nil c.proxy_port
224
+ assert_raise(ArgumentError) { c.proxy_port = 0 }
225
+ assert_raise(ArgumentError) { c.proxy_port = 65536 }
226
+ assert_raise(ArgumentError) { c.proxy_port = -1 }
227
+ end
228
+
229
+ def test_proxy_type_01
230
+ c = Curl::Easy.new($TEST_URL)
231
+
232
+ assert_nil c.proxy_type
233
+
234
+ c.proxy_type = 3
235
+ assert_equal 3, c.proxy_type
236
+
237
+ c.proxy_type = nil
238
+ assert_nil c.proxy_type
239
+ end
240
+
241
+ def test_http_auth_types_01
242
+ c = Curl::Easy.new($TEST_URL)
243
+
244
+ assert_nil c.http_auth_types
245
+
246
+ c.http_auth_types = 3
247
+ assert_equal 3, c.http_auth_types
248
+
249
+ c.http_auth_types = nil
250
+ assert_nil c.http_auth_types
251
+ end
252
+
253
+ def test_proxy_auth_types_01
254
+ c = Curl::Easy.new($TEST_URL)
255
+
256
+ assert_nil c.proxy_auth_types
257
+
258
+ c.proxy_auth_types = 3
259
+ assert_equal 3, c.proxy_auth_types
260
+
261
+ c.proxy_auth_types = nil
262
+ assert_nil c.proxy_auth_types
263
+ end
264
+
265
+ def test_max_redirects_01
266
+ c = Curl::Easy.new($TEST_URL)
267
+
268
+ assert_nil c.max_redirects
269
+
270
+ c.max_redirects = 3
271
+ assert_equal 3, c.max_redirects
272
+
273
+ c.max_redirects = nil
274
+ assert_nil c.max_redirects
275
+ end
276
+
277
+ def test_timeout_01
278
+ c = Curl::Easy.new($TEST_URL)
279
+
280
+ assert_nil c.timeout
281
+
282
+ c.timeout = 3
283
+ assert_equal 3, c.timeout
284
+
285
+ c.timeout = nil
286
+ assert_nil c.timeout
287
+ end
288
+
289
+ def test_connect_timeout_01
290
+ c = Curl::Easy.new($TEST_URL)
291
+
292
+ assert_nil c.connect_timeout
293
+
294
+ c.connect_timeout = 3
295
+ assert_equal 3, c.connect_timeout
296
+
297
+ c.connect_timeout = nil
298
+ assert_nil c.connect_timeout
299
+ end
300
+
301
+ def test_ftp_response_timeout_01
302
+ c = Curl::Easy.new($TEST_URL)
303
+
304
+ assert_nil c.ftp_response_timeout
305
+
306
+ c.ftp_response_timeout = 3
307
+ assert_equal 3, c.ftp_response_timeout
308
+
309
+ c.ftp_response_timeout = nil
310
+ assert_nil c.ftp_response_timeout
311
+ end
312
+
313
+ def test_dns_cache_timeout_01
314
+ c = Curl::Easy.new($TEST_URL)
315
+
316
+ assert_equal 60, c.dns_cache_timeout
317
+
318
+ c.dns_cache_timeout = nil
319
+ assert_nil c.dns_cache_timeout
320
+
321
+ c.dns_cache_timeout = 30
322
+ assert_equal 30, c.dns_cache_timeout
323
+ end
324
+
325
+ def test_on_body
326
+ blk = lambda { |i| i.length }
327
+
328
+ c = Curl::Easy.new
329
+ c.on_body(&blk)
330
+
331
+ assert_equal blk, c.on_body # sets handler nil, returns old handler
332
+ assert_equal nil, c.on_body
333
+ end
334
+
335
+ def test_on_header
336
+ blk = lambda { |i| i.length }
337
+
338
+ c = Curl::Easy.new
339
+ c.on_header(&blk)
340
+
341
+ assert_equal blk, c.on_header # sets handler nil, returns old handler
342
+ assert_equal nil, c.on_header
343
+ end
344
+
345
+ def test_on_progress
346
+ blk = lambda { |*args| true }
347
+
348
+ c = Curl::Easy.new
349
+ c.on_progress(&blk)
350
+
351
+ assert_equal blk, c.on_progress # sets handler nil, returns old handler
352
+ assert_equal nil, c.on_progress
353
+ end
354
+
355
+ def test_on_debug
356
+ blk = lambda { |*args| true }
357
+
358
+ c = Curl::Easy.new
359
+ c.on_debug(&blk)
360
+
361
+ assert_equal blk, c.on_debug # sets handler nil, returns old handler
362
+ assert_equal nil, c.on_debug
363
+ end
364
+
365
+ def test_proxy_tunnel
366
+ c = Curl::Easy.new
367
+ assert !c.proxy_tunnel?
368
+ assert c.proxy_tunnel = true
369
+ assert c.proxy_tunnel?
370
+ end
371
+
372
+ def test_fetch_file_time
373
+ c = Curl::Easy.new
374
+ assert !c.fetch_file_time?
375
+ assert c.fetch_file_time = true
376
+ assert c.fetch_file_time?
377
+ end
378
+
379
+ def test_ssl_verify_peer
380
+ c = Curl::Easy.new
381
+ assert c.ssl_verify_peer?
382
+ assert !c.ssl_verify_peer = false
383
+ assert !c.ssl_verify_peer?
384
+ end
385
+
386
+ def test_ssl_verify_host
387
+ c = Curl::Easy.new
388
+ assert c.ssl_verify_host?
389
+ assert !c.ssl_verify_host = false
390
+ assert !c.ssl_verify_host?
391
+ end
392
+
393
+ def test_header_in_body
394
+ c = Curl::Easy.new
395
+ assert !c.header_in_body?
396
+ assert c.header_in_body = true
397
+ assert c.header_in_body?
398
+ end
399
+
400
+ def test_use_netrc
401
+ c = Curl::Easy.new
402
+ assert !c.use_netrc?
403
+ assert c.use_netrc = true
404
+ assert c.use_netrc?
405
+ end
406
+
407
+ def test_follow_location
408
+ c = Curl::Easy.new
409
+ assert !c.follow_location?
410
+ assert c.follow_location = true
411
+ assert c.follow_location?
412
+ end
413
+
414
+ def test_unrestricted_auth
415
+ c = Curl::Easy.new
416
+ assert !c.unrestricted_auth?
417
+ assert c.unrestricted_auth = true
418
+ assert c.unrestricted_auth?
419
+ end
420
+
421
+ def test_multipart_form_post
422
+ c = Curl::Easy.new
423
+ assert !c.multipart_form_post?
424
+ assert c.multipart_form_post = true
425
+ assert c.multipart_form_post?
426
+ end
427
+
428
+ def test_enable_cookies
429
+ c = Curl::Easy.new
430
+ assert !c.enable_cookies?
431
+ assert c.enable_cookies = true
432
+ assert c.enable_cookies?
433
+ end
434
+
435
+ def test_cookiejar
436
+ c = Curl::Easy.new
437
+ assert_nil c.cookiejar
438
+ assert_equal "some.file", c.cookiejar = "some.file"
439
+ assert_equal "some.file", c.cookiejar
440
+ end
441
+
442
+ def test_on_success
443
+ curl = Curl::Easy.new($TEST_URL)
444
+ on_success_called = false
445
+ curl.on_success {|c|
446
+ on_success_called = true
447
+ assert_not_nil c.body_str
448
+ assert_equal "", c.header_str
449
+ assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)
450
+ }
451
+ curl.perform
452
+ assert on_success_called, "Success handler not called"
453
+ end
454
+
455
+ def test_on_success_with_on_failure
456
+ curl = Curl::Easy.new("#{$TEST_URL.gsub(/file:\/\//,'')}/not_here")
457
+ on_failure_called = false
458
+ curl.on_success {|c| } # make sure we get the failure call even though this handler is defined
459
+ curl.on_failure {|c| on_failure_called = true }
460
+ curl.perform
461
+ assert on_failure_called, "Failure handler not called"
462
+ end
463
+
464
+ def test_get_remote
465
+ curl = Curl::Easy.new(TestServlet.url)
466
+ curl.http_get
467
+ assert_equal 'GET', curl.body_str
468
+ end
469
+
470
+ def test_post_remote
471
+ curl = Curl::Easy.new(TestServlet.url)
472
+ curl.http_post
473
+ assert_equal 'POST', curl.body_str
474
+ end
475
+
476
+ def test_delete_remote
477
+ curl = Curl::Easy.new(TestServlet.url)
478
+ curl.http_delete
479
+ assert_equal 'DELETE', curl.body_str
480
+ end
481
+
482
+ # TODO: Curl::Err::CurlError: Not yet implemented
483
+ # def test_put_remote
484
+ # curl = Curl::Easy.new(TestServlet.url)
485
+ # curl.http_put
486
+ # assert_equal 'PUT', curl.body_str
487
+ # end
488
+
489
+ def self.locked_file
490
+ File.join(File.dirname(__FILE__),'server_lock')
491
+ end
492
+
493
+ def setup
494
+ if @server.nil? and !File.exist?(TestCurbCurlEasy.locked_file)
495
+ #puts "starting"
496
+ File.open(TestCurbCurlEasy.locked_file,'w') {|f| f << 'locked' }
497
+
498
+ # start up a webrick server for testing delete
499
+ @server = WEBrick::HTTPServer.new :Port => 9129, :DocumentRoot => File.expand_path(File.dirname(__FILE__))
500
+
501
+ @server.mount(TestServlet.path, TestServlet)
502
+
503
+ @test_thread = Thread.new { @server.start }
504
+
505
+ exit_code = lambda do
506
+ begin
507
+ #puts "stopping"
508
+ File.unlink TestCurbCurlEasy.locked_file if File.exist?(TestCurbCurlEasy.locked_file)
509
+ @server.shutdown unless @server.nil?
510
+ rescue Object => e
511
+ puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
512
+ end
513
+ end
514
+
515
+ trap("INT"){exit_code.call}
516
+ at_exit{exit_code.call}
517
+
518
+ end
519
+ end
520
+
521
+ def teardown
522
+ end
523
+
524
+ end