diff --git a/src/lchttppost.c b/src/lchttppost.c index 0c86355..373793e 100644 --- a/src/lchttppost.c +++ b/src/lchttppost.c @@ -209,6 +209,8 @@ static int lcurl_hpost_add_file(lua_State *L){ } static int lcurl_hpost_add_stream(lua_State *L){ + static const char *EMPTY = ""; + // add_stream(name, [filename, [type,]] [headers,] size, reader [,context]) lcurl_hpost_t *p = lcurl_gethpost(L); size_t name_len; const char *name = luaL_checklstring(L, 2, &name_len); @@ -234,8 +236,14 @@ static int lcurl_hpost_add_stream(lua_State *L){ ilist = i++; break; } - else if(!fname) fname = luaL_checkstring(L, i); - else if(!type) type = luaL_checkstring(L, i); + else if(!fname){ + if(lua_isnil(L, i)) fname = EMPTY; + else fname = luaL_checkstring(L, i); + } + else if(!type){ + if(lua_isnil(L, i)) type = EMPTY; + else type = luaL_checkstring(L, i); + } else{ if(lua_isnil(L, i) && (!ilist)){ ++i; // empty headers @@ -253,6 +261,8 @@ static int lcurl_hpost_add_stream(lua_State *L){ luaL_argcheck(L, rd.cb_ref != LUA_NOREF, i + 1, "function expected"); if(ilist) list = lcurl_util_to_slist(L, ilist); + if(fname == EMPTY) fname = NULL; + if(type == EMPTY) type = NULL; n = 0; if(fname){ forms[n].option = CURLFORM_FILENAME; forms[n++].value = fname; } diff --git a/test/test_form.lua b/test/test_form.lua index fe1d891..7fb2ee1 100644 --- a/test/test_form.lua +++ b/test/test_form.lua @@ -2,7 +2,8 @@ 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 curl = require "lcurl" + +local curl = require "lcurl" local _ENV = TEST_CASE'add_content' do @@ -111,7 +112,6 @@ function test_04() assert_match('Content%-Encoding: gzip\r\n', data) end - function test_05() assert_equal(post, post:add_buffer('name05', 'file05', 'value05', nil, {"Content-Encoding: gzip"})) local data = assert_string(post:get()) @@ -124,4 +124,90 @@ end end +local _ENV = TEST_CASE'add_stream' do + +local post + +local dummy = function()end + +local stream = function() return 128, dummy end + +function setup() + post = curl.form() +end + +function teardown() + if post then post:free() end + post = nil +end + +function test_01() + assert_equal(post, post:add_stream('name01', stream())) + local data = assert_string(post:get()) + assert_match('name="name01"', data) + assert_not_match('filename', data) +end + +function test_02() + assert_equal(post, post:add_stream('name02', 'file02', stream())) + local data = assert_string(post:get()) + assert_match('name="name02"', data) + assert_match('filename="file02"', data) +end + +function test_03() + assert_equal(post, post:add_stream('name03', 'file03', 'text/plain', stream())) + local data = assert_string(post:get()) + assert_match('name="name03"', data) + assert_match('filename="file03"', data) + assert_match('Content%-Type: text/plain\r\n', data) +end + +function test_04() + assert_equal(post, post:add_stream('name04', 'file04', 'text/plain', {"Content-Encoding: gzip"}, stream())) + local data = assert_string(post:get()) + assert_match('name="name04"', data) + assert_match('filename="file04"', data) + assert_match('Content%-Type: text/plain\r\n', data) + assert_match('Content%-Encoding: gzip\r\n', data) +end + +function test_05() + assert_equal(post, post:add_stream('name05', 'file05', {"Content-Encoding: gzip"}, stream())) + local data = assert_string(post:get()) + assert_match('name="name05"', data) + assert_match('filename="file05"', data) + assert_match('Content%-Encoding: gzip\r\n', data) +end + +function test_06() + assert_equal(post, post:add_stream('name06', {"Content-Encoding: gzip"}, stream())) + local data = assert_string(post:get()) + assert_match('name="name06"', data) + assert_match('Content%-Encoding: gzip\r\n', data) +end + +function test_07() + assert_equal(post, post:add_stream('name07', 'file07', nil, {"Content-Encoding: gzip"}, stream())) + local data = assert_string(post:get()) + assert_match('name="name07"', data) + assert_match('filename="file07"', data) + assert_match('Content%-Encoding: gzip\r\n', data) +end + +function test_08() + assert_equal(post, post:add_stream('name08', nil, nil, {"Content-Encoding: gzip"}, stream())) + local data = assert_string(post:get()) + assert_match('name="name08"', data) + assert_match('Content%-Encoding: gzip\r\n', data) +end + +function test_09() + assert_equal(post, post:add_stream('name09', nil, nil, nil, stream())) + local data = assert_string(post:get()) + assert_match('name="name09"', data) +end + +end + if not HAS_RUNNER then lunit.run() end