@@ -6,6 +6,8 @@ local skip = lunit.skip or function() end
66local curl = require " lcurl"
77local scurl = require " lcurl.safe"
88local json = require " dkjson"
9+ local path = require " path"
10+ local upath = require " path" .new (' /' )
911local url = " http://example.com"
1012local fname = " ./test.download"
1113
@@ -24,9 +26,38 @@ local function gc_collect()
2426 collectgarbage (" collect" )
2527end
2628
29+ local function cver (min , maj , pat )
30+ return min * 2 ^ 16 + maj * 2 ^ 8 + pat
31+ end
32+
33+ local function is_curl_ge (min , maj , pat )
34+ assert (0x070908 == cver (7 ,9 ,8 ))
35+ return curl .version_info (' version_num' ) >= cver (min , maj , pat )
36+ end
37+
38+ local function read_file (n )
39+ local f , e = io.open (n , " r" )
40+ if not f then return nil , e end
41+ local d , e = f :read (" *all" )
42+ f :close ()
43+ return d , e
44+ end
45+
46+ local function get_bin_by (str ,n )
47+ local pos = 1 - n
48+ return function ()
49+ pos = pos + n
50+ return (str :sub (pos ,pos + n - 1 ))
51+ end
52+ end
53+
54+ local function strem (ch , n , m )
55+ return n , get_bin_by ( (ch ):rep (n ), m )
56+ end
57+
2758local ENABLE = true
2859
29- local _ENV = TEST_CASE ' write_callback' if ENABLE then
60+ local _ENV = TEST_CASE ' write_callback' if ENABLE then
3061
3162local c , f
3263
127158
128159end
129160
130- local _ENV = TEST_CASE ' progress_callback' if ENABLE then
161+ local _ENV = TEST_CASE ' progress_callback' if ENABLE then
131162
132163local c
133164
236267
237268end
238269
239- local _ENV = TEST_CASE ' header_callback' if ENABLE then
270+ local _ENV = TEST_CASE ' header_callback' if ENABLE then
240271
241272local c , f
242273local function dummy () end
@@ -334,24 +365,14 @@ end
334365
335366end
336367
337- local _ENV = TEST_CASE ' read_callback' if ENABLE then
368+ local _ENV = TEST_CASE ' read_stream_callback' if ENABLE and is_curl_ge (7 ,30 ,0 ) then
369+
370+ -- tested on WinXP(x32)/Win8(x64) libcurl/7.37.1 / libcurl/7.30.0
338371
339372local url = " http://httpbin.org/post"
340373
341374local c , f , t
342375
343- local function get_bin_by (str ,n )
344- local pos = 1 - n
345- return function ()
346- pos = pos + n
347- return (str :sub (pos ,pos + n - 1 ))
348- end
349- end
350-
351- local function strem (ch , n , m )
352- return n , get_bin_by ( (ch ):rep (n ), m )
353- end
354-
355376local function json_data ()
356377 return json .decode (table.concat (t ))
357378end
@@ -453,7 +474,111 @@ end
453474
454475end
455476
456- local _ENV = TEST_CASE ' escape' if ENABLE then
477+ local _ENV = TEST_CASE ' read_callback' if ENABLE then
478+
479+ local uname = upath :normolize (path .fullpath (fname ))
480+
481+ local url = " FILE://" .. uname
482+
483+ local c
484+
485+ function setup ()
486+ c = assert (scurl .easy {
487+ url = url ,
488+ upload = true ,
489+ })
490+ end
491+
492+ function teardown ()
493+ os.remove (fname )
494+ if c then c :close () end
495+ c = nil
496+ end
497+
498+ function test_abort_01 ()
499+ assert_equal (c , c :setopt_readfunction (function () end ))
500+
501+ local _ , e = assert_nil (c :perform ())
502+ assert_equal (curl .error (curl .ERROR_EASY , curl .E_ABORTED_BY_CALLBACK ), e )
503+ end
504+
505+ function test_abort_02 ()
506+ assert_equal (c , c :setopt_readfunction (function () return nil , " READERROR" end ))
507+
508+ local _ , e = assert_nil (c :perform ())
509+ assert_equal (" READERROR" , e )
510+ end
511+
512+ function test_abort_03 ()
513+ assert_equal (c , c :setopt_readfunction (function () return 1 end ))
514+
515+ local _ , e = assert_nil (c :perform ())
516+ assert_equal (curl .error (curl .ERROR_EASY , curl .E_ABORTED_BY_CALLBACK ), e )
517+ end
518+
519+ function test_abort_04 ()
520+ assert_equal (c , c :setopt_readfunction (function () return true end ))
521+
522+ local _ , e = assert_nil (c :perform ())
523+ assert_equal (curl .error (curl .ERROR_EASY , curl .E_ABORTED_BY_CALLBACK ), e )
524+ end
525+
526+ function test_abort_05 ()
527+ assert_equal (c , c :setopt_readfunction (function () error (" READERROR" ) end ))
528+
529+ assert_error_match (" READERROR" , function () c :perform () end )
530+ end
531+
532+ function test_pause ()
533+ local counter = 0
534+ assert_equal (c , c :setopt_readfunction (function ()
535+ if counter == 0 then
536+ counter = counter + 1
537+ return curl .READFUNC_PAUSE
538+ end
539+ if counter == 1 then
540+ counter = counter + 1
541+ return (' X' ):rep (128 )
542+ end
543+ return ' '
544+ end ))
545+
546+ assert_equal (c , c :setopt_progressfunction (function ()
547+ if counter == 1 then
548+ c :pause (curl .PAUSE_CONT )
549+ end
550+ end ))
551+
552+ assert_equal (c , c :setopt_noprogress (false ))
553+
554+ assert_equal (c , c :perform ())
555+
556+ assert_equal (0 , c :getinfo_response_code ())
557+ end
558+
559+ function test_readbuffer ()
560+ local flag = false
561+ local N
562+ assert_equal (c , c :setopt_readfunction (function (n )
563+ if not flag then
564+ flag = true
565+ N = math.floor (n * 2 + n / 3 )
566+ assert (N > n )
567+ return (" s" ):rep (N )
568+ end
569+ return ' '
570+ end ))
571+
572+ assert_equal (c , c :perform ())
573+ c :close ()
574+ local data = read_file (fname )
575+ assert_equal (N , # data )
576+ assert_equal ((" s" ):rep (N ), data )
577+ end
578+
579+ end
580+
581+ local _ENV = TEST_CASE ' escape' if ENABLE then
457582
458583local c
459584
472597
473598end
474599
475- local _ENV = TEST_CASE ' setopt_form' if ENABLE then
600+ local _ENV = TEST_CASE ' setopt_form' if ENABLE then
476601
477602local c
478603
0 commit comments