diff --git a/src/lcmime.c b/src/lcmime.c index 06c3550..33e12a9 100644 --- a/src/lcmime.c +++ b/src/lcmime.c @@ -135,7 +135,7 @@ int lcurl_mime_set_lua(lua_State *L, lcurl_mime_t *p, lua_State *v){ #define IS_NILORSTR(L, i) (lua_type(L, i) == LUA_TSTRING) || (lua_type(L, i) == LUA_TNIL) #define IS_TABLE(L, i) lua_type(L, i) == LUA_TTABLE -#define IS_FALSE(L, i) (lua_type(L, i) == LUA_TBOOLEAN) && (!lua_toboolean(L, i)) +#define IS_FALSE(L, i) ((lua_type(L, i) == LUA_TBOOLEAN) && (!lua_toboolean(L, i))) || lutil_is_null(L,i) #define IS_OPTSTR(L, i) (IS_FALSE(L, i)) || (IS_NILORSTR(L, i)) static int lutil_isarray(lua_State *L, int i){ @@ -512,13 +512,13 @@ static int lcurl_mime_part_headers(lua_State *L){ } else{ list = lcurl_util_to_slist(L, 2); - luaL_argcheck(L, list, 2, "array or nil expected"); + luaL_argcheck(L, list || IS_TABLE(L, 2), 2, "array or null expected"); } ret = curl_mime_headers(p->part, list, 1); if(ret != CURLE_OK){ - curl_slist_free_all(list); + if(list) curl_slist_free_all(list); return lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_EASY, ret); } diff --git a/test/test_mime.lua b/test/test_mime.lua index e7239f4..762d896 100644 --- a/test/test_mime.lua +++ b/test/test_mime.lua @@ -19,6 +19,8 @@ local weak_ptr, gc_collect, dump_mime_ = utils.import('weak_ptr', 'gc_collect', local GET_URL = 'http://127.0.0.1:7090/get' +local null = curl.null + local function is_freed(c) return not not string.find(tostring(c), '%(freed%)') end @@ -313,6 +315,15 @@ function test_unset_name() assert_not_match('Content%-Disposition:.-name="test"', info) end +function test_unset_name_by_null() + mime:addpart():data('hello', 'test/html', 'test'):name(null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+test/html', info) + assert_not_match('Content%-Disposition:.-name="test"', info) +end + function test_unset_type() mime:addpart():data('hello', 'test/html'):type(false) @@ -321,6 +332,14 @@ function test_unset_type() assert_not_match('Content%-Type:%s+test/html', info) end +function test_unset_type_by_null() + mime:addpart():data('hello', 'test/html'):type(null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_not_match('Content%-Type:%s+test/html', info) +end + function test_unset_headers() mime:addpart():data('hello', 'test/html',{ 'X-Custom-Header: hello' @@ -331,6 +350,26 @@ function test_unset_headers() assert_not_match('X%-Custom%-Header:%s*hello', info) end +function test_unset_headers_by_null() + mime:addpart():data('hello', 'test/html',{ + 'X-Custom-Header: hello' + }):headers(null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_not_match('X%-Custom%-Header:%s*hello', info) +end + +function test_unset_headers_by_empty_array() + mime:addpart():data('hello', 'test/html',{ + 'X-Custom-Header: hello' + }):headers({}) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_not_match('X%-Custom%-Header:%s*hello', info) +end + function test_unset_data() mime:addpart():data('hello', 'test/html', 'test'):data(false) @@ -340,6 +379,15 @@ function test_unset_data() assert_match('Content%-Disposition:.-name="test"', info) end +function test_unset_data_by_null() + mime:addpart():data('hello', 'test/html', 'test'):data(null) + + local info = assert_string(dump_mime(mime)) + assert_not_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+test/html', info) + assert_match('Content%-Disposition:.-name="test"', info) +end + function test_unset_data_type_1() local part = mime:addpart():data('hello', 'test/html', 'test', { 'X-Custom-Header: hello' @@ -352,6 +400,18 @@ function test_unset_data_type_1() assert_match('X%-Custom%-Header:%s*hello', info) end +function test_unset_data_type_1_by_null() + local part = mime:addpart():data('hello', 'test/html', 'test', { + 'X-Custom-Header: hello' + }):data('hello', null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_not_match('Content%-Type:%s+test/html', info) + assert_match('Content%-Disposition:.-name="test"', info) + assert_match('X%-Custom%-Header:%s*hello', info) +end + function test_unset_data_type_2() local part = mime:addpart():data('hello', 'test/html', 'test', { 'X-Custom-Header: hello' @@ -376,6 +436,18 @@ function test_unset_data_name_1() assert_match('X%-Custom%-Header:%s*hello', info) end +function test_unset_data_name_1_by_null() + local part = mime:addpart():data('hello', 'test/html', 'test', { + 'X-Custom-Header: hello' + }):data('hello', nil, null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+test/html', info) + assert_not_match('Content%-Disposition:.-name="test"', info) + assert_match('X%-Custom%-Header:%s*hello', info) +end + function test_unset_data_name_2() local part = mime:addpart():data('hello', 'test/html', 'test', { 'X-Custom-Header: hello' @@ -400,6 +472,18 @@ function test_unset_data_header() assert_not_match('X%-Custom%-Header:%s*hello', info) end +function test_unset_data_header_by_null() + local part = mime:addpart():data('hello', 'test/html', 'test', { + 'X-Custom-Header: hello' + }):data('hello', nil, nil, nil, null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+test/html', info) + assert_match('Content%-Disposition:.-name="test"', info) + assert_not_match('X%-Custom%-Header:%s*hello', info) +end + function test_unset_data_filename_1() local part = mime:addpart():data('hello', 'test/html', 'test', 'test.html', { 'X-Custom-Header: hello' @@ -409,7 +493,20 @@ function test_unset_data_filename_1() assert_match('\r\n\r\nhello', info) assert_match('Content%-Type:%s+test/html', info) assert_match('Content%-Disposition:.-%sname="test"', info) - assert_not_match('Content%-Disposition:.-%sname="test%.html"', info) + assert_not_match('Content%-Disposition:.-%sfilename="test%.html"', info) + assert_match('X%-Custom%-Header:%s*hello', info) +end + +function test_unset_data_filename_1_by_null() + local part = mime:addpart():data('hello', 'test/html', 'test', 'test.html', { + 'X-Custom-Header: hello' + }):data('hello', nil, nil, null) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+test/html', info) + assert_match('Content%-Disposition:.-%sname="test"', info) + assert_not_match('Content%-Disposition:.-%sfilename="test%.html"', info) assert_match('X%-Custom%-Header:%s*hello', info) end