diff --git a/src/lceasy.c b/src/lceasy.c index 344f5f4..a85dc6b 100644 --- a/src/lceasy.c +++ b/src/lceasy.c @@ -365,7 +365,7 @@ static int lcurl_opt_set_slist_(lua_State *L, int opt, int list_no){ CURLcode code; int ref = p->lists[list_no]; - luaL_argcheck(L, list, 2, "array expected"); + luaL_argcheck(L, list || lua_istable(L, 2), 2, "array expected"); if(ref != LUA_NOREF){ struct curl_slist *tmp = lcurl_storage_remove_slist(L, p->storage, ref); @@ -380,7 +380,10 @@ static int lcurl_opt_set_slist_(lua_State *L, int opt, int list_no){ return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, code); } - p->lists[list_no] = lcurl_storage_preserve_slist(L, p->storage, list); + if (list) { + p->lists[list_no] = lcurl_storage_preserve_slist(L, p->storage, list); + } + lua_settop(L, 1); return 1; } diff --git a/test/test_easy.lua b/test/test_easy.lua index 3ffb408..baef535 100644 --- a/test/test_easy.lua +++ b/test/test_easy.lua @@ -32,8 +32,8 @@ local POST_URL = "http://127.0.0.1:7090/post" -- print("------------------------------------") -- print("") -local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream = - utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream') +local weak_ptr, gc_collect, is_curl_ge, read_file, stream, Stream, dump_request = + utils.import('weak_ptr', 'gc_collect', 'is_curl_ge', 'read_file', 'stream', 'Stream', 'dump_request') local ENABLE = true @@ -1026,4 +1026,38 @@ function test_chunk_end() test_cb('chunk_end_function') end end +local _ENV = TEST_CASE'set_slist' if ENABLE then + +local c + +function teardown() + if c then c:close() end + c = nil +end + +function test_set() + c = curl.easy() + c:setopt_httpheader({'X-Custom: value'}) + local body, headers = assert_string(dump_request(c)) + assert_match("X%-Custom:%s*value\r\n", headers) +end + +function test_unset() + c = curl.easy() + c:setopt_httpheader({'X-Custom: value'}) + c:unsetopt_httpheader() + local body, headers = assert_string(dump_request(c)) + assert_not_match("X%-Custom:%s*value\r\n", headers) +end + +function test_set_empty_array() + c = curl.easy() + c:setopt_httpheader({'X-Custom: value'}) + c:setopt_httpheader({}) + local body, headers = assert_string(dump_request(c)) + assert_not_match("X%-Custom:%s*value\r\n", headers) +end + +end + RUN() diff --git a/test/utils.lua b/test/utils.lua index 6dce917..bc50ca9 100644 --- a/test/utils.lua +++ b/test/utils.lua @@ -93,16 +93,49 @@ local function easy_dump_mime(easy, mime, url) return table.concat(buffer) end +local function easy_dump_request(easy, url) + local buffer = {} + local headers = {} + + local function dump_mime(type, data) + if type == curl.INFO_DATA_OUT then + buffer[#buffer + 1] = data + end + + if type == curl.INFO_HEADER_OUT then + headers[#headers + 1] = data + end + end + + local ok, err = easy:setopt{ + url = url or "http://127.0.0.1:7090"; + customrequest = "GET"; + mimepost = mime; + verbose = true; + debugfunction = dump_mime; + writefunction = function()end; + } + + if not ok then return nil, err end + + ok, err = easy:perform() + + if not ok then return nil, err end + + return table.concat(buffer), table.concat(headers) +end + local utils = { - weak_ptr = weak_ptr; - gc_collect = gc_collect; - is_curl_ge = is_curl_ge; - is_curl_eq = is_curl_eq; - get_bin_by = get_bin_by; - read_file = read_file; - dump_mime = easy_dump_mime; - stream = stream; - Stream = Stream; + weak_ptr = weak_ptr; + gc_collect = gc_collect; + is_curl_ge = is_curl_ge; + is_curl_eq = is_curl_eq; + get_bin_by = get_bin_by; + read_file = read_file; + dump_mime = easy_dump_mime; + dump_request = easy_dump_request; + stream = stream; + Stream = Stream; } utils.import = function(...)