@@ -229,6 +229,38 @@ function test_data_name()
229229 assert_match (' Content%-Disposition:.-name="test"' , info )
230230end
231231
232+ function test_data_should_not_unset_on_nil ()
233+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
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 (' Content%-Type:%s+text/html' , info )
239+ assert_match (' Content%-Disposition:.-name="test"' , info )
240+ assert_match (' X%-Custom%-Header:%s*hello' , info )
241+
242+ part :data (' world' , nil , ' test2' )
243+ info = assert_string (dump_mime (mime ))
244+ assert_match (' \r\n\r\n world' , info )
245+ assert_match (' Content%-Type:%s+text/html' , info )
246+ assert_match (' Content%-Disposition:.-name="test2"' , info )
247+ assert_match (' X%-Custom%-Header:%s*hello' , info )
248+
249+ part :data (' !!!' , ' text/xml' , nil )
250+ info = assert_string (dump_mime (mime ))
251+ assert_match (' \r\n\r\n !!!' , info )
252+ assert_match (' Content%-Type:%s+text/xml' , info )
253+ assert_match (' Content%-Disposition:.-name="test2"' , info )
254+ assert_match (' X%-Custom%-Header:%s*hello' , info )
255+
256+ part :data (' !!!' , ' text/xml' , nil , nil )
257+ info = assert_string (dump_mime (mime ))
258+ assert_match (' \r\n\r\n !!!' , info )
259+ assert_match (' Content%-Type:%s+text/xml' , info )
260+ assert_match (' Content%-Disposition:.-name="test2"' , info )
261+ assert_match (' X%-Custom%-Header:%s*hello' , info )
262+ end
263+
232264function test_data_headers ()
233265 mime :addpart ():data (' hello' , {
234266 ' X-Custom-Header: hello'
@@ -255,6 +287,101 @@ function test_unset_type()
255287 assert_not_match (' Content%-Type:%s+text/html' , info )
256288end
257289
290+ function test_unset_headers ()
291+ mime :addpart ():data (' hello' , ' text/html' ,{
292+ ' X-Custom-Header: hello'
293+ }):headers (false )
294+
295+ local info = assert_string (dump_mime (mime ))
296+ assert_match (' \r\n\r\n hello' , info )
297+ assert_not_match (' X%-Custom%-Header:%s*hello' , info )
298+ end
299+
300+ function test_unset_data ()
301+ mime :addpart ():data (' hello' , ' text/html' , ' test' ):data (false )
302+
303+ local info = assert_string (dump_mime (mime ))
304+ assert_not_match (' \r\n\r\n hello' , info )
305+ assert_match (' Content%-Type:%s+text/html' , info )
306+ assert_match (' Content%-Disposition:.-name="test"' , info )
307+ end
308+
309+ function test_unset_data_type_1 ()
310+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
311+ ' X-Custom-Header: hello'
312+ }):data (' hello' , false )
313+
314+ local info = assert_string (dump_mime (mime ))
315+ assert_match (' \r\n\r\n hello' , info )
316+ assert_not_match (' Content%-Type:%s+text/html' , info )
317+ assert_match (' Content%-Disposition:.-name="test"' , info )
318+ assert_match (' X%-Custom%-Header:%s*hello' , info )
319+ end
320+
321+ function test_unset_data_type_2 ()
322+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
323+ ' X-Custom-Header: hello'
324+ }):data (' hello' , false , nil , nil )
325+
326+ local info = assert_string (dump_mime (mime ))
327+ assert_match (' \r\n\r\n hello' , info )
328+ assert_not_match (' Content%-Type:%s+text/html' , info )
329+ assert_match (' Content%-Disposition:.-name="test"' , info )
330+ assert_match (' X%-Custom%-Header:%s*hello' , info )
331+ end
332+
333+ function test_unset_data_name_1 ()
334+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
335+ ' X-Custom-Header: hello'
336+ }):data (' hello' , nil , false )
337+
338+ local info = assert_string (dump_mime (mime ))
339+ assert_match (' \r\n\r\n hello' , info )
340+ assert_match (' Content%-Type:%s+text/html' , info )
341+ assert_not_match (' Content%-Disposition:.-name="test"' , info )
342+ assert_match (' X%-Custom%-Header:%s*hello' , info )
343+ end
344+
345+ function test_unset_data_name_2 ()
346+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
347+ ' X-Custom-Header: hello'
348+ }):data (' hello' , nil , false , nil )
349+
350+ local info = assert_string (dump_mime (mime ))
351+ assert_match (' \r\n\r\n hello' , info )
352+ assert_match (' Content%-Type:%s+text/html' , info )
353+ assert_not_match (' Content%-Disposition:.-name="test"' , info )
354+ assert_match (' X%-Custom%-Header:%s*hello' , info )
355+ end
356+
357+ function test_unset_data_header ()
358+ local part = mime :addpart ():data (' hello' , ' text/html' , ' test' , {
359+ ' X-Custom-Header: hello'
360+ }):data (' hello' , nil , nil , false )
361+
362+ local info = assert_string (dump_mime (mime ))
363+ assert_match (' \r\n\r\n hello' , info )
364+ assert_match (' Content%-Type:%s+text/html' , info )
365+ assert_match (' Content%-Disposition:.-name="test"' , info )
366+ assert_not_match (' X%-Custom%-Header:%s*hello' , info )
367+ end
368+
369+ function test_fail_pass_nil_as_first_arg ()
370+ local part = mime :addpart ()
371+ assert_error (function () part :data () end )
372+ assert_error (function () part :data (nil ) end )
373+
374+ assert_error (function () part :name () end )
375+ assert_error (function () part :name (nil ) end )
376+
377+ assert_error (function () part :type () end )
378+ assert_error (function () part :type (nil ) end )
379+
380+ assert_error (function () part :headers () end )
381+ assert_error (function () part :headers (nil ) end )
382+ end
383+
384+
258385end
259386
260387RUN ()
0 commit comments