Fix. Skip pause test because it can behave differently on different OS/cURL version.

This commit is contained in:
Alexey Melnichuk 2015-06-15 11:46:00 +04:00
parent c71bebbd31
commit 7eeaeb3ce6
2 changed files with 1145 additions and 1139 deletions

View File

@ -1,308 +1,308 @@
local lunit, RUN = lunit do local lunit, RUN = lunit do
RUN = lunit and function()end or function () RUN = lunit and function()end or function ()
local res = lunit.run() local res = lunit.run()
if res.errors + res.failed > 0 then if res.errors + res.failed > 0 then
os.exit(-1) os.exit(-1)
end end
return os.exit(0) return os.exit(0)
end end
lunit = require "lunit" lunit = require "lunit"
end end
local _,luacov = pcall(require, "luacov") local _,luacov = pcall(require, "luacov")
local TEST_CASE = assert(lunit.TEST_CASE) local TEST_CASE = assert(lunit.TEST_CASE)
local skip = lunit.skip or function() end local skip = lunit.skip or function() end
local curl = require "cURL" local curl = require "cURL"
local scurl = require "cURL.safe" local scurl = require "cURL.safe"
local json = require "dkjson" local json = require "dkjson"
local fname = "./test.download" local fname = "./test.download"
local ENABLE = true local ENABLE = true
local _ENV = TEST_CASE'multi_iterator' if ENABLE then local _ENV = TEST_CASE'multi_iterator' if ENABLE then
local url = "http://httpbin.org/get" local url = "http://httpbin.org/get"
local c, t, m local c, t, m
local function json_data() local function json_data()
return json.decode(table.concat(t)) return json.decode(table.concat(t))
end end
function setup() function setup()
t = {} t = {}
m = assert(scurl.multi()) m = assert(scurl.multi())
end end
function teardown() function teardown()
if m then m:close() end if m then m:close() end
if c then c:close() end if c then c:close() end
m, c, t = nil m, c, t = nil
end end
function test_add_handle() function test_add_handle()
local base_url = 'http://httpbin.org/get?key=' local base_url = 'http://httpbin.org/get?key='
local urls = { local urls = {
base_url .. "1", base_url .. "1",
base_url .. "2", base_url .. "2",
"###" .. base_url .. "3", "###" .. base_url .. "3",
base_url .. "4", base_url .. "4",
base_url .. "5", base_url .. "5",
} }
local i = 0 local i = 0
local function next_easy() local function next_easy()
i = i + 1 i = i + 1
local url = urls[i] local url = urls[i]
if url then if url then
c = assert(scurl.easy{url = url}) c = assert(scurl.easy{url = url})
t = {} t = {}
return c return c
end end
end end
m = assert_equal(m, m:add_handle(next_easy())) m = assert_equal(m, m:add_handle(next_easy()))
for data, type, easy in m:iperform() do for data, type, easy in m:iperform() do
if type == "done" or type == "error" then if type == "done" or type == "error" then
assert_equal(urls[i], easy:getinfo_effective_url()) assert_equal(urls[i], easy:getinfo_effective_url())
assert_equal(easy, c) assert_equal(easy, c)
easy:close() easy:close()
c = nil c = nil
if i == 3 then if i == 3 then
assert_equal(curl.error(curl.ERROR_EASY, curl.E_UNSUPPORTED_PROTOCOL), data) assert_equal(curl.error(curl.ERROR_EASY, curl.E_UNSUPPORTED_PROTOCOL), data)
else else
local data = json_data() local data = json_data()
assert_table(data.args) assert_table(data.args)
assert_equal(tostring(i), data.args.key) assert_equal(tostring(i), data.args.key)
end end
easy = next_easy() easy = next_easy()
if easy then m:add_handle(easy) end if easy then m:add_handle(easy) end
end end
if type == "data" then table.insert(t, data) end if type == "data" then table.insert(t, data) end
end end
assert_equal(#urls + 1, i) assert_equal(#urls + 1, i)
assert_nil(c) assert_nil(c)
end end
function test_info_read() function test_info_read()
local url = 'http://httpbin.org/get?key=1' local url = 'http://httpbin.org/get?key=1'
c = assert(curl.easy{url=url, writefunction=function() end}) c = assert(curl.easy{url=url, writefunction=function() end})
assert_equal(m, m:add_handle(c)) assert_equal(m, m:add_handle(c))
while m:perform() > 0 do m:wait() end while m:perform() > 0 do m:wait() end
local h, ok, err = m:info_read() local h, ok, err = m:info_read()
assert_equal(c, h) assert_equal(c, h)
local h, ok, err = m:info_read() local h, ok, err = m:info_read()
assert_equal(0, h) assert_equal(0, h)
end end
end end
local _ENV = TEST_CASE'form' if ENABLE then local _ENV = TEST_CASE'form' if ENABLE then
local post local post
function teardown() function teardown()
if post then post:free() end if post then post:free() end
post = nil post = nil
end end
function test_content_01() function test_content_01()
post = assert(scurl.form{name01 = 'value01'}) post = assert(scurl.form{name01 = 'value01'})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue01\r\n", data) assert_match("\r\n\r\nvalue01\r\n", data)
assert_match('name="name01"', data) assert_match('name="name01"', data)
end end
function test_content_02() function test_content_02()
post = assert(scurl.form{name02 = {'value02', type = "text/plain"}}) post = assert(scurl.form{name02 = {'value02', type = "text/plain"}})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue02\r\n", data) assert_match("\r\n\r\nvalue02\r\n", data)
assert_match('name="name02"', data) assert_match('name="name02"', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
end end
function test_content_03() function test_content_03()
post = assert(scurl.form{name03 = {content = 'value03', headers = {"Content-Encoding: gzip"}}}) post = assert(scurl.form{name03 = {content = 'value03', headers = {"Content-Encoding: gzip"}}})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue03\r\n", data) assert_match("\r\n\r\nvalue03\r\n", data)
assert_match('name="name03"', data) assert_match('name="name03"', data)
assert_match('Content%-Encoding: gzip\r\n', data) assert_match('Content%-Encoding: gzip\r\n', data)
end end
function test_content_04() function test_content_04()
post = assert(scurl.form{name04 = {'value04', type = "text/plain", headers = {"Content-Encoding: gzip"}}}) post = assert(scurl.form{name04 = {'value04', type = "text/plain", headers = {"Content-Encoding: gzip"}}})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue04\r\n", data) assert_match("\r\n\r\nvalue04\r\n", data)
assert_match('name="name04"', data) assert_match('name="name04"', data)
assert_match('Content%-Encoding: gzip\r\n', data) assert_match('Content%-Encoding: gzip\r\n', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
end end
function test_buffer_01() function test_buffer_01()
post = assert(scurl.form{name01 = { post = assert(scurl.form{name01 = {
name = 'file01', name = 'file01',
data = 'value01', data = 'value01',
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue01\r\n", data) assert_match("\r\n\r\nvalue01\r\n", data)
assert_match('name="name01"', data) assert_match('name="name01"', data)
assert_match('filename="file01"', data) assert_match('filename="file01"', data)
assert_match('Content%-Type: application/octet%-stream\r\n', data) assert_match('Content%-Type: application/octet%-stream\r\n', data)
end end
function test_buffer_02() function test_buffer_02()
post = assert(scurl.form{name02 = { post = assert(scurl.form{name02 = {
name = 'file02', name = 'file02',
data = 'value02', data = 'value02',
type = "text/plain", type = "text/plain",
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue02\r\n", data) assert_match("\r\n\r\nvalue02\r\n", data)
assert_match('name="name02"', data) assert_match('name="name02"', data)
assert_match('filename="file02"', data) assert_match('filename="file02"', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
assert_not_match('Content%-Type: application/octet%-stream\r\n', data) assert_not_match('Content%-Type: application/octet%-stream\r\n', data)
end end
function test_buffer_03() function test_buffer_03()
post = assert(scurl.form{name03 = { post = assert(scurl.form{name03 = {
name = 'file03', name = 'file03',
data = 'value03', data = 'value03',
headers = {"Content-Encoding: gzip"}, headers = {"Content-Encoding: gzip"},
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue03\r\n", data) assert_match("\r\n\r\nvalue03\r\n", data)
assert_match('name="name03"', data) assert_match('name="name03"', data)
assert_match('filename="file03"', data) assert_match('filename="file03"', data)
assert_match('Content%-Type: application/octet%-stream\r\n', data) assert_match('Content%-Type: application/octet%-stream\r\n', data)
assert_match('Content%-Encoding: gzip\r\n', data) assert_match('Content%-Encoding: gzip\r\n', data)
end end
function test_buffer_04() function test_buffer_04()
post = assert(scurl.form{name04 = { post = assert(scurl.form{name04 = {
name = 'file04', name = 'file04',
data = 'value04', data = 'value04',
type = "text/plain", type = "text/plain",
headers = {"Content-Encoding: gzip"}, headers = {"Content-Encoding: gzip"},
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match("\r\n\r\nvalue04\r\n", data) assert_match("\r\n\r\nvalue04\r\n", data)
assert_match('name="name04"', data) assert_match('name="name04"', data)
assert_match('filename="file04"', data) assert_match('filename="file04"', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
assert_not_match('Content%-Type: application/octet%-stream\r\n', data) assert_not_match('Content%-Type: application/octet%-stream\r\n', data)
assert_match('Content%-Encoding: gzip\r\n', data) assert_match('Content%-Encoding: gzip\r\n', data)
end end
function test_stream_01() function test_stream_01()
post = assert(scurl.form{name01 = { post = assert(scurl.form{name01 = {
stream = function() end, stream = function() end,
length = 128, length = 128,
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match('name="name01"', data) assert_match('name="name01"', data)
assert_not_match('filename', data) assert_not_match('filename', data)
end end
function test_stream_02() function test_stream_02()
post = assert(scurl.form{name02 = { post = assert(scurl.form{name02 = {
name = 'file02', name = 'file02',
stream = function() end, stream = function() end,
length = 128, length = 128,
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match('name="name02"', data) assert_match('name="name02"', data)
assert_match('filename="file02"', data) assert_match('filename="file02"', data)
end end
function test_stream_03() function test_stream_03()
post = assert(scurl.form{name03 = { post = assert(scurl.form{name03 = {
name = 'file03', name = 'file03',
stream = function() end, stream = function() end,
length = 128, length = 128,
type = 'text/plain', type = 'text/plain',
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match('name="name03"', data) assert_match('name="name03"', data)
assert_match('filename="file03"', data) assert_match('filename="file03"', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
end end
function test_stream_04() function test_stream_04()
post = assert(scurl.form{name04 = { post = assert(scurl.form{name04 = {
name = 'file04', name = 'file04',
stream = function() end, stream = function() end,
length = 128, length = 128,
type = 'text/plain', type = 'text/plain',
headers = {"Content-Encoding: gzip"}, headers = {"Content-Encoding: gzip"},
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match('name="name04"', data) assert_match('name="name04"', data)
assert_match('filename="file04"', data) assert_match('filename="file04"', data)
assert_match('Content%-Type: text/plain\r\n', data) assert_match('Content%-Type: text/plain\r\n', data)
assert_match('Content%-Encoding: gzip\r\n', data) assert_match('Content%-Encoding: gzip\r\n', data)
end end
function test_stream_05() function test_stream_05()
post = assert(scurl.form{name05 = { post = assert(scurl.form{name05 = {
stream = { stream = {
length = function() return 128 end; length = function() return 128 end;
read = function() end; read = function() end;
} }
}}) }})
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_match('name="name05"', data) assert_match('name="name05"', data)
assert_not_match('filename', data) assert_not_match('filename', data)
end end
function test_error() function test_error()
assert_error(function() post = scurl.form{name = {content = 1}} end) assert_error(function() post = scurl.form{name = {content = 1}} end)
assert_error(function() post = scurl.form{name = {1}} end) assert_error(function() post = scurl.form{name = {1}} end)
assert_error(function() post = scurl.form{name = {data = {}}} end) assert_error(function() post = scurl.form{name = {data = {}}} end)
assert_error(function() post = scurl.form{name = {file = true}} end) assert_error(function() post = scurl.form{name = {file = true}} end)
assert_error(function() post = scurl.form{name = {stream = function() end}} end) assert_error(function() post = scurl.form{name = {stream = function() end}} end)
assert_error(function() post = scurl.form{name = {stream = {}}} end) assert_error(function() post = scurl.form{name = {stream = {}}} end)
assert_error(function() post = scurl.form{name = {stream = { assert_error(function() post = scurl.form{name = {stream = {
read=function()end;length=function()end read=function()end;length=function()end
}}}end) }}}end)
assert_error(function() post = scurl.form{name = {stream = { assert_error(function() post = scurl.form{name = {stream = {
read=function()end;length=function() return "123" end read=function()end;length=function() return "123" end
}}}end) }}}end)
assert_error(function() post = scurl.form{name = {stream = { assert_error(function() post = scurl.form{name = {stream = {
read=function()end;length=function() return "hello" end read=function()end;length=function() return "hello" end
}}}end) }}}end)
end end
function test_ignore_unknown() function test_ignore_unknown()
post = assert(scurl.form{ post = assert(scurl.form{
name01 = {}, name01 = {},
name02 = {name = "helo"}, name02 = {name = "helo"},
}) })
local data = assert_string(post:get()) local data = assert_string(post:get())
assert_not_match('name="name01"', data) assert_not_match('name="name01"', data)
assert_not_match('name="name02"', data) assert_not_match('name="name02"', data)
end end
function test_empty() function test_empty()
post = assert(scurl.form{}) post = assert(scurl.form{})
end end
end end
RUN() RUN()

File diff suppressed because it is too large Load Diff