Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lceasy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
38 changes: 36 additions & 2 deletions test/test_easy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
51 changes: 42 additions & 9 deletions test/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
Expand Down