diff --git a/doc/lcurl.ldoc b/doc/lcurl.ldoc index 14d6c7a..ffac7e5 100644 --- a/doc/lcurl.ldoc +++ b/doc/lcurl.ldoc @@ -228,7 +228,7 @@ function getinfo() end -- -- A callback accepting one or two parameters. -- The first is the writer context if any, and the second is a string with the data to be written. --- Function must return `true` or full data length or nothing to continue operation. +-- Function must return `true` (any non number true value) or full data length or nothing to continue operation. -- Otherwise the transfer will be aborted with an error. -- -- @tparam function writer @@ -250,7 +250,7 @@ function setopt_writefunction() end -- -- A callback accepting one or two parameters. -- The first is the writer context if any, and the second is a string with the data to be written. --- Function must return `true` or full data length or nothing to continue operation. +-- Function must return `true` (any non number true value) or full data length or nothing to continue operation. -- Otherwise the transfer will be aborted with an error. -- -- @tparam function writer diff --git a/src/lceasy.c b/src/lceasy.c index 9c963d6..3bc84b0 100644 --- a/src/lceasy.c +++ b/src/lceasy.c @@ -478,10 +478,12 @@ static int lcurl_write_callback_(lua_State*L, if(lua_gettop(L) > top){ if(lua_isnil(L, top + 1)) return 0; - if(lua_isboolean(L, top + 1)){ + if(lua_isnumber(L, top + 1)){ + ret = (size_t)lua_tonumber(L, top + 1); + } + else{ if(!lua_toboolean(L, top + 1)) ret = 0; } - else ret = (size_t)lua_tonumber(L, top + 1); } lua_settop(L, top); diff --git a/test/run.lua b/test/run.lua index 6ed4696..25a1eec 100644 --- a/test/run.lua +++ b/test/run.lua @@ -12,3 +12,4 @@ print("------------------------------------") print("") require "test_safe" +require "test_easy" diff --git a/test/test_easy.lua b/test/test_easy.lua new file mode 100644 index 0000000..8539621 --- /dev/null +++ b/test/test_easy.lua @@ -0,0 +1,48 @@ +local HAS_RUNNER = not not lunit +local lunit = require "lunit" +local TEST_CASE = assert(lunit.TEST_CASE) +local skip = lunit.skip or function() end + +local curl = require "lcurl" +local scurl = require "lcurl.safe" +local url = "http://example.com" +local fname = "./test.download" + +local _ENV = TEST_CASE'write_callback' do + +local c, f + +function teardown() + if f then f:close() end + os.remove(fname) + if c then c:close() end + f, c = nil +end + +function test_write_to_file() + f = assert(io.open(fname, "w+b")) + c = assert(curl.easy{ + url = url; + writefunction = f; + }) + + assert_equal(c, c:perform()) +end + +function test_write_to_file_abort() + f = assert(io.open(fname, "w+b")) + c = assert(scurl.easy{ + url = url; + writefunction = function(str) + return #str - 1 + end; + }) + + local _, e = assert_nil(c:perform()) + assert_equal(e, curl.error(curl.ERROR_EASY, curl.E_WRITE_ERROR)) + +end + +end + +if not HAS_RUNNER then lunit.run() end