Lua-cURLv3/test/test_safe.lua
2014-09-01 11:39:11 +05:00

89 lines
2.0 KiB
Lua

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 _ENV = TEST_CASE'error_mode' do
local c
function teardown()
if c then c:close() end
c = nil
end
function test_safe()
local curl = require "lcurl.safe"
c = assert(curl.easy())
assert_equal(c, c:setopt_url("aaaaa://123"))
assert_nil(c:perform())
end
function test_raise()
local curl = require "lcurl"
c = assert(curl.easy())
assert_equal(c, c:setopt_url("aaaaa://123"))
assert_error(function() c:perform() end)
end
end
local _ENV = TEST_CASE'setopt' do
local c
function setup()
c = assert(require"lcurl.safe".easy())
end
function teardown()
if c then c:close() end
c = nil
end
function test_number()
assert_equal(c, c:setopt_verbose(false))
assert_equal(c, c:setopt_verbose(true))
assert_equal(c, c:setopt_verbose(1))
assert_equal(c, c:setopt_verbose(0))
assert_error(function() c:setopt_verbose("1") end)
assert_error(function() c:setopt_verbose("true") end)
end
function test_string()
assert_error(function() c:setopt_url(true) end)
assert_error(function() c:setopt_url(1) end)
assert_equal(c, c:setopt_url("1"))
end
function test_array()
assert_error(function() c:setopt_httpheader(true) end)
assert_error(function() c:setopt_httpheader(1) end)
assert_error(function() c:setopt_httpheader("k:v")end)
assert_equal(c, c:setopt_httpheader{"k:v"})
end
end
local _ENV = TEST_CASE'error_object' do
local curl = require "lcurl"
function test()
local e1 = curl.error(curl.ERROR_EASY, 0) -- ok
assert_equal(curl.ERROR_EASY, e1:category())
assert_equal(curl.E_OK, e1:no())
assert_equal("OK", e1:name())
local e2 = curl.error(curl.ERROR_MULTI, 0) -- ok
local e3 = curl.error(curl.ERROR_MULTI, 0) -- ok
assert_equal(0, e1:no())
assert_equal(0, e2:no())
assert(e1 ~= e2)
assert(e3 == e2)
end
end
if not HAS_RUNNER then lunit.run() end