@@ -35,6 +35,8 @@ local POST_URL = "http://127.0.0.1:7090/post"
3535local weak_ptr , gc_collect , is_curl_ge , read_file , stream , Stream , dump_request =
3636 utils .import (' weak_ptr' , ' gc_collect' , ' is_curl_ge' , ' read_file' , ' stream' , ' Stream' , ' dump_request' )
3737
38+ local null = curl .null
39+
3840local ENABLE = true
3941
4042local _ENV = TEST_CASE ' curl error' if ENABLE then
@@ -151,9 +153,16 @@ function test_reset_write_callback()
151153 assert_equal (c , c :setopt_writefunction (f ))
152154 assert_equal (c , c :setopt_writefunction (f .write , f ))
153155 assert_equal (c , c :setopt_writefunction (print ))
156+ assert_equal (c , c :setopt_writefunction (print , null ))
157+ assert_equal (c , c :setopt_writefunction (null ))
158+ assert_equal (c , c :setopt_writefunction (null , nil ))
159+ assert_equal (c , c :setopt_writefunction (null , null ))
154160 assert_error (function ()c :setopt_writefunction ()end )
155161 assert_error (function ()c :setopt_writefunction (nil )end )
156162 assert_error (function ()c :setopt_writefunction (nil , f )end )
163+ assert_error (function ()c :setopt_writefunction (null , {})end )
164+ assert_error (function ()c :setopt_writefunction (print , {}, nil )end )
165+ assert_error (function ()c :setopt_writefunction (print , {}, null )end )
157166end
158167
159168function test_write_pass_01 ()
@@ -208,6 +217,38 @@ function test_write_coro()
208217 assert_equal (co2 , called )
209218end
210219
220+ function test_write_pass_null_context ()
221+ c = assert (curl .easy {
222+ url = GET_URL ;
223+ })
224+
225+ local context
226+ assert_equal (c , c :setopt_writefunction (function (ctx )
227+ context = ctx
228+ return true
229+ end , null ))
230+
231+ assert_equal (c , c :perform ())
232+ assert_equal (null , context )
233+ end
234+
235+ function test_write_pass_nil_context ()
236+ c = assert (curl .easy {
237+ url = GET_URL ;
238+ })
239+
240+ local context , called
241+ assert_equal (c , c :setopt_writefunction (function (ctx )
242+ context = ctx
243+ called = true
244+ return true
245+ end , nil ))
246+
247+ assert_equal (c , c :perform ())
248+ assert_true (called )
249+ assert_nil (context )
250+ end
251+
211252end
212253
213254local _ENV = TEST_CASE ' progress_callback' if ENABLE then
@@ -379,9 +420,14 @@ function test_reset_header_callback()
379420 assert_equal (c , c :setopt_headerfunction (f ))
380421 assert_equal (c , c :setopt_headerfunction (f .header , f ))
381422 assert_equal (c , c :setopt_headerfunction (print ))
423+ assert_equal (c , c :setopt_headerfunction (null ))
424+ assert_equal (c , c :setopt_headerfunction (null , nil ))
425+ assert_equal (c , c :setopt_headerfunction (null , null ))
382426 assert_error (function ()c :setopt_headerfunction ()end )
383427 assert_error (function ()c :setopt_headerfunction (nil )end )
384428 assert_error (function ()c :setopt_headerfunction (nil , f )end )
429+ assert_error (function ()c :setopt_headerfunction (null , {})end )
430+ assert_error (function ()c :setopt_headerfunction (print , {}, nil )end )
385431end
386432
387433function test_header_pass_01 ()
@@ -1060,4 +1106,62 @@ end
10601106
10611107end
10621108
1109+ local _ENV = TEST_CASE ' set_null' if ENABLE then
1110+
1111+ local c , m
1112+
1113+ function teardown ()
1114+ if c then c :close () end
1115+ if m then m :close () end
1116+ m , c = nil
1117+ end
1118+
1119+ function test_string ()
1120+ c = curl .easy ()
1121+ c :setopt_accept_encoding (' gzip' )
1122+ local body , headers = assert_string (dump_request (c ))
1123+ assert_match (" Accept%-Encoding:%s*gzip" , headers )
1124+
1125+ c :setopt_accept_encoding (null )
1126+ body , headers = assert_string (dump_request (c ))
1127+ assert_not_match (" Accept%-Encoding:%s*gzip" , headers )
1128+ end
1129+
1130+ function test_string_via_table ()
1131+ c = curl .easy ()
1132+ c :setopt_accept_encoding (' gzip' )
1133+ local body , headers = assert_string (dump_request (c ))
1134+ assert_match (" Accept%-Encoding:%s*gzip" , headers )
1135+
1136+ c :setopt { accept_encoding = null }
1137+ body , headers = assert_string (dump_request (c ))
1138+ assert_not_match (" Accept%-Encoding:%s*gzip" , headers )
1139+ end
1140+
1141+ function test_slist ()
1142+ c = curl .easy ()
1143+ c :setopt_httpheader ({' X-Custom: value' })
1144+ c :setopt_httpheader (null )
1145+ local body , headers = assert_string (dump_request (c ))
1146+ assert_not_match (" X%-Custom:%s*value\r\n " , headers )
1147+ end
1148+
1149+ function test_slist_via_table ()
1150+ c = curl .easy ()
1151+ c :setopt_httpheader ({' X-Custom: value' })
1152+ c :setopt {httpheader = null }
1153+ local body , headers = assert_string (dump_request (c ))
1154+ assert_not_match (" X%-Custom:%s*value\r\n " , headers )
1155+ end
1156+
1157+ function test_multi_set_array ()
1158+ m = curl .multi ()
1159+ m :setopt_pipelining_site_bl {
1160+ ' 127.0.0.1'
1161+ }
1162+ assert_equal (m , m :setopt_pipelining_site_bl (null ))
1163+ end
1164+
1165+ end
1166+
10631167RUN ()
0 commit comments