Skip to content

Commit 4817dad

Browse files
committed
Merge pull request #30 from moteus/master
Add. test to set/unset postfields
2 parents 7f8395c + e6c2ffc commit 4817dad

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Lua-cURLv2 binding has several problems:
3232
* it has memory leak when send multipart/formdata
3333
* it does not save string for curl options that may result crush in libcurl
3434
* there no way to get result for operations in multi interface (e.g. if one of easy operation fail you can not get result code/error message)
35-
* you can not use multi interface for upload operation (?)
3635
* you can not use your own callback function to perform operation with multi interface
3736
* you can not pass your context to callback functions
3837

@@ -62,7 +61,7 @@ curl.easy()
6261
curl.easy()
6362
:setopt_url('http://posttestserver.com/post.php')
6463
:setopt_writefunction(io.write)
65-
:setopt_httppost(curl.form() -- lcurl guarantee that form will be alive
64+
:setopt_httppost(curl.form() -- Lua-cURL guarantee that form will be alive
6665
:add_content("test_content", "some data", {
6766
"MyHeader: SomeValue"
6867
})
@@ -101,16 +100,12 @@ curl.easy()
101100
-- Multi FTP Upload
102101

103102
-- We get error E_LOGIN_DENIED for this operation
104-
e1 = curl.easy()
105-
:setopt_url("ftp://moteus:999999@127.0.0.1/test1.dat")
106-
:setopt_upload(true)
103+
e1 = curl.easy{url = "ftp://moteus:999999@127.0.0.1/test1.dat", upload = true}
107104
:setopt_readfunction(
108105
function(t) return table.remove(t) end, {"1111", "2222"}
109106
)
110107

111-
e2 = curl.easy()
112-
:setopt_url("ftp://moteus:123456@127.0.0.1/test2.dat")
113-
:setopt_upload(true)
108+
e2 = curl.easy{url = "ftp://moteus:123456@127.0.0.1/test2.dat", upload = true}
114109
:setopt_readfunction(get_bin_by(("e"):rep(1000), 5))
115110

116111
m = curl.multi()

test/test_easy.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,61 @@ function test_reset()
700700
assert(not pfrom.value)
701701
end
702702

703+
end
704+
705+
local _ENV = TEST_CASE'setopt_postfields' if ENABLE then
706+
707+
local c
708+
709+
function teardown()
710+
if c then c:close() end
711+
c = nil
712+
end
713+
714+
function test()
715+
716+
do local fields = {}
717+
for i = 1, 100 do fields[#fields + 1] = "key" .. i .. "=value"..i end
718+
fields = table.concat(fields, '&')
719+
c = assert(curl.easy{
720+
url = "http://httpbin.org/post",
721+
postfields = fields,
722+
writefunction = function()end,
723+
})
724+
end
725+
726+
-- call gc to try clear `fields` string
727+
for i = 1, 4 do collectgarbage"collect" end
728+
729+
c:perform()
730+
end
731+
732+
function test_unset()
733+
local pfields
734+
735+
do local fields = {}
736+
for i = 1, 100 do fields[#fields + 1] = "key" .. i .. "=value"..i end
737+
fields = table.concat(fields, '&')
738+
c = assert(curl.easy{
739+
url = "http://httpbin.org/post",
740+
postfields = fields,
741+
writefunction = function()end,
742+
})
743+
pfields = weak_ptr(fields)
744+
end
745+
746+
-- call gc to try clear `fields` string
747+
for i = 1, 4 do collectgarbage"collect" end
748+
assert_string(pfields.value)
749+
750+
assert_equal(c, c:unsetopt_postfields())
751+
752+
-- @todo check internal storage because gc really do not clear `weak` string
753+
-- for i = 1, 4 do collectgarbage"collect" end
754+
-- assert_nil(pfields.value)
755+
756+
-- c:perform()
757+
end
703758

704759
end
705760

0 commit comments

Comments
 (0)