@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;
44
55repeat_each(2 );
66
7- plan tests => repeat_each() * 199 ;
7+ plan tests => repeat_each() * 219 ;
88
99our $ HtmlDir = html_dir;
1010
@@ -3238,3 +3238,291 @@ connected: 1
32383238-- - error_log
32393239cleanup lua tcp socket request
32403240GC cycle done
3241+
3242+
3243+
3244+ === TEST 62 : receiveany method in cosocket
3245+ -- - config
3246+ location = /foo {
3247+ server_tokens off;
3248+
3249+ content_by_lua_block {
3250+ local resp = {
3251+ ' 1' ,
3252+ ' 22' ,
3253+ ' hello world' ,
3254+ }
3255+
3256+ local length = 0
3257+ for _, v in ipairs(resp) do
3258+ length = length + # v
3259+ end
3260+
3261+ -- flush http header
3262+ ngx. header[' Content-Length' ] = length
3263+ ngx. flush (true)
3264+ ngx. sleep (0.01 )
3265+
3266+ -- send http body
3267+ for _, v in ipairs(resp) do
3268+ ngx. print (v)
3269+ ngx. flush (true)
3270+ ngx. sleep (0.01 )
3271+ end
3272+ }
3273+ }
3274+ --- stream_server_config
3275+ content_by_lua_block {
3276+ local sock = ngx. socket. tcp()
3277+ sock: settimeout(500 )
3278+
3279+ assert(sock: connect(" 127.0.0.1" , $ TEST_NGINX_SERVER_PORT ))
3280+ local req = {
3281+ ' GET /foo HTTP/1.0\r\n' ,
3282+ ' Host: localhost\r\n' ,
3283+ ' Connection: close\r\n\r\n' ,
3284+ }
3285+ local ok , err = sock: send(req)
3286+ if not ok then
3287+ ngx. say (" send request failed: " , err)
3288+ return
3289+ end
3290+
3291+ -- skip http header
3292+ while true do
3293+ local data, err, _ = sock: receive(' *l' )
3294+ if err then
3295+ ngx. say (' unexpected error occurs when receiving http head: ' , err)
3296+ return
3297+ end
3298+
3299+ if # data == 0 then -- read last line of head
3300+ break
3301+ end
3302+ end
3303+
3304+ -- receive http body
3305+ while true do
3306+ local data, err = sock: receiveany(1024 )
3307+ if err then
3308+ if err ~ = ' closed' then
3309+ ngx. say (' unexpected err: ' , err)
3310+ end
3311+ break
3312+ end
3313+ ngx. say (data)
3314+ end
3315+
3316+ sock: close()
3317+ }
3318+ --- stream_response
3319+ 1
3320+ 22
3321+ hello world
3322+ --- no_error_log
3323+ [error]
3324+ --- error_log
3325+ lua tcp socket read any
3326+
3327+
3328+
3329+ === TEST 63: receiveany send data after read side closed
3330+ --- stream_server_config
3331+ content_by_lua_block {
3332+ local sock = ngx. socket. tcp()
3333+ sock: settimeout(500 )
3334+ assert(sock: connect(" 127.0.0.1" , 7658 ))
3335+
3336+ while true do
3337+ local data, err = sock: receiveany(1024 )
3338+ if err then
3339+ if err ~ = ' closed' then
3340+ ngx. say (' unexpected err: ' , err)
3341+ break
3342+ end
3343+
3344+ local data = " send data after read side closed"
3345+ local bytes , err = sock: send(data)
3346+ if not bytes then
3347+ ngx. say (err)
3348+ end
3349+
3350+ break
3351+ end
3352+ ngx. say (data)
3353+ end
3354+
3355+ sock: close()
3356+ }
3357+ --- tcp_listen: 7658
3358+ --- tcp_shutdown: 1
3359+ --- tcp_query eval: "send data after read side closed"
3360+ --- tcp_query_len: 32
3361+ --- stream_response
3362+ --- no_error_log
3363+ [error]
3364+
3365+
3366+
3367+ === TEST 64: receiveany with limited, max <= 0
3368+ --- stream_server_config
3369+ content_by_lua_block {
3370+ local sock = ngx .socket.tcp ()
3371+ sock :settimeout (500 )
3372+ assert (sock: connect(" 127.0.0.1" , $ TEST_NGINX_SERVER_PORT ))
3373+
3374+ local function receiveany_say_err (... )
3375+ local ok , err = pcall (sock. receiveany, sock, ... )
3376+ if not ok then
3377+ ngx .say (err)
3378+ end
3379+ end
3380+
3381+
3382+ receiveany_say_err (0 )
3383+ receiveany_say_err (-1 )
3384+ receiveany_say_err ()
3385+ receiveany_say_err (nil)
3386+ }
3387+ --- stream_response
3388+ bad argument #2 to '?' (bad max argument )
3389+ bad argument #2 to '?' (bad max argument )
3390+ expecting 2 arguments (including the object ), but got 1
3391+ bad argument #2 to '?' (bad max argument )
3392+ --- no_error_log
3393+ [ error ]
3394+
3395+
3396+
3397+ === TEST 65 : receiveany with limited , max is larger than data
3398+ --- config
3399+ location = /foo {
3400+ server_tokens off ;
3401+
3402+ content_by_lua_block {
3403+ local resp = 'hello world'
3404+ local length = #resp
3405+
3406+ ngx .header [ 'Content-Length' ] = length
3407+ ngx .flush (true)
3408+ ngx .sleep (0.01 )
3409+
3410+ ngx .print (resp)
3411+ }
3412+ }
3413+ --- stream_server_config
3414+ content_by_lua_block {
3415+ local sock = ngx .socket.tcp ()
3416+ sock :settimeout (500 )
3417+
3418+ assert (sock: connect(" 127.0.0.1" , $ TEST_NGINX_SERVER_PORT ))
3419+ local req = {
3420+ 'GET /foo HTTP/1.0\r\n' ,
3421+ 'Host: localhost\r\n' ,
3422+ 'Connection: close\r\n\r\n' ,
3423+ }
3424+ local ok , err = sock :send (req)
3425+ if not ok then
3426+ ngx .say (" send request failed: " , err)
3427+ return
3428+ end
3429+
3430+ while true do
3431+ local data , err , _ = sock :receive (' *l' )
3432+ if err then
3433+ ngx .say (' unexpected error occurs when receiving http head: ' , err)
3434+ return
3435+ end
3436+
3437+ if #data == 0 then -- read last line of head
3438+ break
3439+ end
3440+ end
3441+
3442+ local data , err = sock :receiveany (128 )
3443+ if err then
3444+ if err ~= 'closed' then
3445+ ngx .say (' unexpected err: ' , err)
3446+ end
3447+ else
3448+ ngx .say (data)
3449+ end
3450+
3451+ sock :close ()
3452+ }
3453+ --- stream_response
3454+ hello world
3455+ --- no_error_log
3456+ [ error ]
3457+ --- error_log
3458+ lua tcp socket calling receiveany () method to read at most 128 bytes
3459+
3460+
3461+
3462+ === TEST 66 : receiveany with limited , max is smaller than data
3463+ --- config
3464+ location = /foo {
3465+ server_tokens off ;
3466+
3467+ content_by_lua_block {
3468+ local resp = 'hello world'
3469+ local length = #resp
3470+
3471+ ngx .header [ 'Content-Length' ] = length
3472+ ngx .flush (true)
3473+ ngx .sleep (0.01 )
3474+
3475+ ngx .print (resp)
3476+ }
3477+ }
3478+ --- stream_server_config
3479+ content_by_lua_block {
3480+ local sock = ngx .socket.tcp ()
3481+ sock :settimeout (500 )
3482+
3483+ assert (sock: connect(" 127.0.0.1" , $ TEST_NGINX_SERVER_PORT ))
3484+ local req = {
3485+ 'GET /foo HTTP/1.0\r\n' ,
3486+ 'Host: localhost\r\n' ,
3487+ 'Connection: close\r\n\r\n' ,
3488+ }
3489+ local ok , err = sock :send (req)
3490+ if not ok then
3491+ ngx .say (" send request failed: " , err)
3492+ return
3493+ end
3494+
3495+ while true do
3496+ local data , err , _ = sock :receive (' *l' )
3497+ if err then
3498+ ngx .say (' unexpected error occurs when receiving http head: ' , err)
3499+ return
3500+ end
3501+
3502+ if #data == 0 then -- read last line of head
3503+ break
3504+ end
3505+ end
3506+
3507+ while true do
3508+ local data , err = sock :receiveany (7 )
3509+ if err then
3510+ if err ~= 'closed' then
3511+ ngx .say (' unexpected err: ' , err)
3512+ end
3513+ break
3514+
3515+ else
3516+ ngx .say (data)
3517+ end
3518+ end
3519+
3520+ sock :close ()
3521+ }
3522+ --- stream_response
3523+ hello w
3524+ orld
3525+ --- no_error_log
3526+ [ error ]
3527+ --- error_log
3528+ lua tcp socket calling receiveany () method to read at most 7 bytes
0 commit comments