@@ -13,16 +13,11 @@ local TEST_CASE = assert(lunit.TEST_CASE)
1313local skip = lunit .skip or function () end
1414
1515local curl = require " lcurl"
16+ local utils = require " utils"
1617
17- local function weak_ptr (val )
18- return setmetatable ({value = val }, {__mode = ' v' })
19- end
18+ local weak_ptr , gc_collect , dump_mime_ = utils .import (' weak_ptr' , ' gc_collect' , ' dump_mime' )
2019
21- local function gc_collect (n )
22- for i = 1 , (n or 10 ) do
23- collectgarbage (" collect" )
24- end
25- end
20+ local dump_mime_url = ' http://127.0.0.1:7090'
2621
2722local function is_freed (c )
2823 return not not string.find (tostring (c ), ' %(freed%)' )
185180
186181end
187182
183+ local _ENV = TEST_CASE ' mime basic' if not curl .OPT_MIMEPOST then
184+ function test () skip (" MIMI API supports since cURL 7.56.0" ) end
185+ else
186+
187+ local easy , mime
188+
189+ local function dump_mime (mime )
190+ return dump_mime_ (easy , mime , dump_mime_url )
191+ end
192+
193+ function setup ()
194+ easy = curl .easy ()
195+ mime = easy :mime ()
196+ end
197+
198+ function teardown ()
199+ if easy then easy :close () end
200+ if mime then mime :free () end
201+ easy , mime = nil
202+ end
203+
204+ function test_data ()
205+ mime :addpart ():data (' hello' )
206+ local info = assert_string (dump_mime (mime ))
207+ assert_match (' \r\n\r\n hello' , info )
208+ end
209+
210+ function test_data_type ()
211+ mime :addpart ():data (' hello' , ' text/html' )
212+ local info = assert_string (dump_mime (mime ))
213+ assert_match (' \r\n\r\n hello' , info )
214+ assert_match (' Content%-Type:%s+text/html' , info )
215+ end
216+
217+ function test_data_type_name ()
218+ mime :addpart ():data (' hello' , ' text/html' , ' test' )
219+ local info = assert_string (dump_mime (mime ))
220+ assert_match (' \r\n\r\n hello' , info )
221+ assert_match (' Content%-Type:%s+text/html' , info )
222+ assert_match (' Content%-Disposition:.-name="test"' , info )
223+ end
224+
225+ function test_data_name ()
226+ mime :addpart ():data (' hello' , nil , ' test' )
227+ local info = assert_string (dump_mime (mime ))
228+ assert_match (' \r\n\r\n hello' , info )
229+ assert_match (' Content%-Disposition:.-name="test"' , info )
230+ end
231+
232+ function test_data_headers ()
233+ mime :addpart ():data (' hello' , {
234+ ' X-Custom-Header: hello'
235+ })
236+ local info = assert_string (dump_mime (mime ))
237+ assert_match (' \r\n\r\n hello' , info )
238+ assert_match (' X%-Custom%-Header:%s*hello' , info )
239+ end
240+
241+ function test_unset_name ()
242+ mime :addpart ():data (' hello' , ' text/html' , ' test' ):name (false )
243+
244+ local info = assert_string (dump_mime (mime ))
245+ assert_match (' \r\n\r\n hello' , info )
246+ assert_match (' Content%-Type:%s+text/html' , info )
247+ assert_not_match (' Content%-Disposition:.-name="test"' , info )
248+ end
249+
250+ function test_unset_type ()
251+ mime :addpart ():data (' hello' , ' text/html' ):type (false )
252+
253+ local info = assert_string (dump_mime (mime ))
254+ assert_match (' \r\n\r\n hello' , info )
255+ assert_not_match (' Content%-Type:%s+text/html' , info )
256+ end
257+
258+ end
259+
188260RUN ()
0 commit comments