1- require (" luacov" )
2- local lunit = require (" lunit" )
31local socket = require (" ljsocket" )
42
5- module (" tcp_test" , lunit .testcase , package.seeall )
6-
7- function tcp_client_blocking_test ()
3+ do -- tcp client blocking test
84 local host = " www.freebsd.no"
9- local socket = socket .create (" inet" , " stream" , " tcp" )
10- lunit .assert (socket )
11- lunit .assert (socket :connect (host , " http" ))
12- lunit .assert (socket :send (
5+ local socket = assert (socket .create (" inet" , " stream" , " tcp" ))
6+ assert (socket :connect (host , " http" ))
7+ assert (socket :send (
138 " GET / HTTP/1.1\r\n " ..
149 " Host: " .. host .. " \r\n " ..
1510 " User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n " ..
@@ -25,8 +20,7 @@ function tcp_client_blocking_test()
2520 local str = " "
2621
2722 while true do
28- local chunk = socket :receive ()
29- lunit .assert (chunk )
23+ local chunk = assert (socket :receive ())
3024
3125 if not chunk then
3226 break
@@ -44,24 +38,23 @@ function tcp_client_blocking_test()
4438 end
4539 end
4640
47- lunit . assert_true (total_length > 1024 )
48- lunit . assert_true (string.find (str , " HTTP/1.1 200 OK" ) > 0 )
49- lunit . assert_true (string.find (str , " </html>" ) > 0 )
41+ assert (total_length > 1024 )
42+ assert (string.find (str , " HTTP/1.1 200 OK" , nil , true ) > 0 )
43+ assert (string.find (str , " </html>" , nil , true ) > 0 )
5044end
5145
52- function tcp_client_blocking_test ()
46+ do -- tcp client non-blocking test
5347 local host = " www.freebsd.no"
54- local socket = socket .create (" inet" , " stream" , " tcp" )
55- lunit .assert (socket )
56- lunit .assert (socket :connect (host , " http" ))
57- lunit .assert (socket :set_blocking (false ))
48+ local socket = assert (socket .create (" inet" , " stream" , " tcp" ))
49+ assert (socket :connect (host , " http" ))
50+ assert (socket :set_blocking (false ))
5851
5952 local str = " "
6053 local total_length
6154
6255 while true do
6356 if socket :is_connected () then
64- lunit . assert (socket :send (
57+ assert (socket :send (
6558 " GET / HTTP/1.1\r\n " ..
6659 " Host: " .. host .. " \r\n " ..
6760 " User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n " ..
@@ -87,7 +80,7 @@ function tcp_client_blocking_test()
8780 if # str >= total_length then
8881 return
8982 end
90- elseif num ~= 11 then
83+ elseif err ~= " timeout " then
9184 error (err )
9285 end
9386 end
@@ -96,9 +89,7 @@ function tcp_client_blocking_test()
9689 end
9790 end
9891
99- lunit .assert_true (total_length > 1024 )
100- lunit .assert_true (string.find (str , " HTTP/1.1 200 OK" ) > 0 )
101- lunit .assert_true (string.find (str , " </html>" ) > 0 )
102- end
103-
104- lunit .main (... )
92+ assert (total_length > 1024 )
93+ assert (string.find (str , " HTTP/1.1 200 OK" , nil , true ) > 0 )
94+ assert (string.find (str , " </html>" , nil , true ) > 0 )
95+ end
0 commit comments