Skip to content

Commit 6d5adc1

Browse files
committed
Merge pull request #39 from moteus/master
Add. Supports `content` field in form:add().
2 parents 13e5dd6 + 6b92542 commit 6d5adc1

File tree

3 files changed

+234
-5
lines changed

3 files changed

+234
-5
lines changed

doc/curl.ldoc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do
2020
--- Form description table.
2121
--
2222
-- @table FORM_DESCRIPTION
23-
-- @tfield string|form_stream|form_file|form_buffer content_name
23+
-- @tfield string|form_stream|form_file|form_buffer|form_content content_name
2424
--
2525
-- @usage
2626
-- post_form = cURL.form{
@@ -58,6 +58,14 @@ do
5858
-- type = "text/plain",
5959
-- },
6060
--
61+
-- -- content form string
62+
-- name05 = 'value05',
63+
--
64+
-- -- content form string
65+
-- name06 = { 'value06', -- or content = 'value05'
66+
-- type = "text/plain",
67+
-- },
68+
--
6169
-- }
6270

6371
--- Table describe stream part in form.
@@ -89,6 +97,14 @@ do
8997
-- @tfield[opt] table headers array of headers
9098
--
9199

100+
--- Table describe content part in form.
101+
--
102+
-- @table form_content
103+
-- @tfield string content value (or first field in table)
104+
-- @tfield[opt] string type mime type (e.g. 'text/plain')
105+
-- @tfield[opt] table headers array of headers
106+
--
107+
92108
end
93109

94110
--- HTTP multipart/formdata object

src/lua/cURL/impl/cURL.lua

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ local function make_iterator(self, perform)
115115
end
116116

117117

118-
-- name = <string>/<stream>/<file>/<buffer>
118+
-- name = <string>/<stream>/<file>/<buffer>/<content>
119119
--
120120
-- <stream> = {
121121
-- stream = function/object
@@ -139,15 +139,22 @@ end
139139
-- headers = ?table
140140
-- }
141141
--
142+
-- <content> = {
143+
-- content = string -- or first key in table
144+
-- type = ?string
145+
-- headers = ?table
146+
-- }
147+
--
148+
142149
local function form_add_element(form, name, value)
143150
local vt = type(value)
144151
if vt == "string" then return form:add_content(name, value) end
145152

146153
assert(type(name) == "string")
147154
assert(vt == "table")
148-
assert((value.name == nil) or (type(value.name) == 'string'))
149-
assert((value.type == nil) or (type(value.type) == 'string'))
150-
assert((value.headrs == nil) or (type(value.type) == 'string'))
155+
assert((value.name == nil) or (type(value.name ) == 'string'))
156+
assert((value.type == nil) or (type(value.type ) == 'string'))
157+
assert((value.headers == nil) or (type(value.headers) == 'table' ))
151158

152159
if value.stream then
153160
local vst = type(value.stream)
@@ -177,6 +184,17 @@ local function form_add_element(form, name, value)
177184
assert(type(value.name) == 'string')
178185
return form:add_buffer(name, value.name, value.data, value.type, value.headers)
179186
end
187+
188+
local content = value[1] or value.content
189+
if content then
190+
assert(type(content) == 'string')
191+
if value.type then
192+
return form:add_content(name, content, value.type, value.headers)
193+
end
194+
return form:add_content(name, content, value.headers)
195+
end
196+
197+
return form
180198
end
181199

182200
local function form_add(form, data)

test/test_curl.lua

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,200 @@ end
8787

8888
end
8989

90+
local _ENV = TEST_CASE'form' if ENABLE then
91+
92+
local post
93+
94+
function teardown()
95+
if post then post:free() end
96+
post = nil
97+
end
98+
99+
function test_content_01()
100+
post = assert(scurl.form{name01 = 'value01'})
101+
local data = assert_string(post:get())
102+
assert_match("\r\n\r\nvalue01\r\n", data)
103+
assert_match('name="name01"', data)
104+
end
105+
106+
function test_content_02()
107+
post = assert(scurl.form{name02 = {'value02', type = "text/plain"}})
108+
local data = assert_string(post:get())
109+
assert_match("\r\n\r\nvalue02\r\n", data)
110+
assert_match('name="name02"', data)
111+
assert_match('Content%-Type: text/plain\r\n', data)
112+
end
113+
114+
function test_content_03()
115+
post = assert(scurl.form{name03 = {content = 'value03', headers = {"Content-Encoding: gzip"}}})
116+
local data = assert_string(post:get())
117+
assert_match("\r\n\r\nvalue03\r\n", data)
118+
assert_match('name="name03"', data)
119+
assert_match('Content%-Encoding: gzip\r\n', data)
120+
end
121+
122+
function test_content_04()
123+
post = assert(scurl.form{name04 = {'value04', type = "text/plain", headers = {"Content-Encoding: gzip"}}})
124+
local data = assert_string(post:get())
125+
assert_match("\r\n\r\nvalue04\r\n", data)
126+
assert_match('name="name04"', data)
127+
assert_match('Content%-Encoding: gzip\r\n', data)
128+
assert_match('Content%-Type: text/plain\r\n', data)
129+
end
130+
131+
function test_buffer_01()
132+
post = assert(scurl.form{name01 = {
133+
name = 'file01',
134+
data = 'value01',
135+
}})
136+
137+
local data = assert_string(post:get())
138+
assert_match("\r\n\r\nvalue01\r\n", data)
139+
assert_match('name="name01"', data)
140+
assert_match('filename="file01"', data)
141+
assert_match('Content%-Type: application/octet%-stream\r\n', data)
142+
end
143+
144+
function test_buffer_02()
145+
post = assert(scurl.form{name02 = {
146+
name = 'file02',
147+
data = 'value02',
148+
type = "text/plain",
149+
}})
150+
151+
local data = assert_string(post:get())
152+
assert_match("\r\n\r\nvalue02\r\n", data)
153+
assert_match('name="name02"', data)
154+
assert_match('filename="file02"', data)
155+
assert_match('Content%-Type: text/plain\r\n', data)
156+
assert_not_match('Content%-Type: application/octet%-stream\r\n', data)
157+
end
158+
159+
function test_buffer_03()
160+
post = assert(scurl.form{name03 = {
161+
name = 'file03',
162+
data = 'value03',
163+
headers = {"Content-Encoding: gzip"},
164+
}})
165+
local data = assert_string(post:get())
166+
assert_match("\r\n\r\nvalue03\r\n", data)
167+
assert_match('name="name03"', data)
168+
assert_match('filename="file03"', data)
169+
assert_match('Content%-Type: application/octet%-stream\r\n', data)
170+
assert_match('Content%-Encoding: gzip\r\n', data)
171+
end
172+
173+
function test_buffer_04()
174+
post = assert(scurl.form{name04 = {
175+
name = 'file04',
176+
data = 'value04',
177+
type = "text/plain",
178+
headers = {"Content-Encoding: gzip"},
179+
}})
180+
181+
local data = assert_string(post:get())
182+
assert_match("\r\n\r\nvalue04\r\n", data)
183+
assert_match('name="name04"', data)
184+
assert_match('filename="file04"', data)
185+
assert_match('Content%-Type: text/plain\r\n', data)
186+
assert_not_match('Content%-Type: application/octet%-stream\r\n', data)
187+
assert_match('Content%-Encoding: gzip\r\n', data)
188+
end
189+
190+
function test_stream_01()
191+
post = assert(scurl.form{name01 = {
192+
stream = function() end,
193+
length = 128,
194+
}})
195+
local data = assert_string(post:get())
196+
assert_match('name="name01"', data)
197+
assert_not_match('filename', data)
198+
end
199+
200+
function test_stream_02()
201+
post = assert(scurl.form{name02 = {
202+
name = 'file02',
203+
stream = function() end,
204+
length = 128,
205+
}})
206+
local data = assert_string(post:get())
207+
assert_match('name="name02"', data)
208+
assert_match('filename="file02"', data)
209+
end
210+
211+
function test_stream_03()
212+
post = assert(scurl.form{name03 = {
213+
name = 'file03',
214+
stream = function() end,
215+
length = 128,
216+
type = 'text/plain',
217+
}})
218+
219+
local data = assert_string(post:get())
220+
assert_match('name="name03"', data)
221+
assert_match('filename="file03"', data)
222+
assert_match('Content%-Type: text/plain\r\n', data)
223+
end
224+
225+
function test_stream_04()
226+
post = assert(scurl.form{name04 = {
227+
name = 'file04',
228+
stream = function() end,
229+
length = 128,
230+
type = 'text/plain',
231+
headers = {"Content-Encoding: gzip"},
232+
}})
233+
local data = assert_string(post:get())
234+
assert_match('name="name04"', data)
235+
assert_match('filename="file04"', data)
236+
assert_match('Content%-Type: text/plain\r\n', data)
237+
assert_match('Content%-Encoding: gzip\r\n', data)
238+
end
239+
240+
function test_stream_05()
241+
post = assert(scurl.form{name05 = {
242+
stream = {
243+
length = function() return 128 end;
244+
read = function() end;
245+
}
246+
}})
247+
local data = assert_string(post:get())
248+
assert_match('name="name05"', data)
249+
assert_not_match('filename', data)
250+
end
251+
252+
function test_error()
253+
assert_error(function() post = scurl.form{name = {content = 1}} end)
254+
assert_error(function() post = scurl.form{name = {1}} end)
255+
assert_error(function() post = scurl.form{name = {data = {}}} end)
256+
assert_error(function() post = scurl.form{name = {file = true}} end)
257+
assert_error(function() post = scurl.form{name = {stream = function() end}} end)
258+
assert_error(function() post = scurl.form{name = {stream = {}}} end)
259+
assert_error(function() post = scurl.form{name = {stream = {
260+
read=function()end;length=function()end
261+
}}}end)
262+
assert_error(function() post = scurl.form{name = {stream = {
263+
read=function()end;length=function() return "123" end
264+
}}}end)
265+
assert_error(function() post = scurl.form{name = {stream = {
266+
read=function()end;length=function() return "hello" end
267+
}}}end)
268+
end
269+
270+
function test_ignore_unknown()
271+
post = assert(scurl.form{
272+
name01 = {},
273+
name02 = {name = "helo"},
274+
})
275+
local data = assert_string(post:get())
276+
assert_not_match('name="name01"', data)
277+
assert_not_match('name="name02"', data)
278+
end
279+
280+
function test_empty()
281+
post = assert(scurl.form{})
282+
end
283+
284+
end
90285

91286
if not HAS_RUNNER then lunit.run() end

0 commit comments

Comments
 (0)