@@ -3,13 +3,10 @@ local test = require("test.gambarina")
33
44test (' TCP client blocking test' , function ()
55 local host = " www.freebsd.no"
6- local sock = socket .create (" inet" , " stream" , " tcp" )
7- ok (sock ~= nil , " socket creation should succeed " )
6+ local sock = assert ( socket .create (" inet" , " stream" , " tcp" ) )
7+ assert (sock : connect ( host , " http " ) )
88
9- local connected = sock :connect (host , " http" )
10- ok (connected , " connection should succeed" )
11-
12- local sent = sock :send (
9+ local sent = assert (sock :send (
1310 " GET / HTTP/1.1\r\n " ..
1411 " Host: " .. host .. " \r\n " ..
1512 " User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n " ..
@@ -19,21 +16,13 @@ test('TCP client blocking test', function()
1916 " Connection: keep-alive\r\n " ..
2017 " Upgrade-Insecure-Requests: 1\r\n " ..
2118 " \r\n "
22- )
23- ok (sent , " sending HTTP request should succeed" )
19+ ))
2420
2521 local total_length
2622 local str = " "
2723
2824 while true do
29- local chunk = sock :receive ()
30- ok (chunk ~= nil or chunk == false , " receive should not error" )
31-
32- if not chunk then
33- break
34- end
35-
36- str = str .. chunk
25+ str = str .. assert (sock :receive ())
3726
3827 if not total_length then
3928 total_length = tonumber (str :match (" Content%-Length: (%d+)" ))
5241
5342test (' TCP client non-blocking test' , function ()
5443 local host = " www.freebsd.no"
55- local sock = socket .create (" inet" , " stream" , " tcp" )
56- ok (sock ~= nil , " socket creation should succeed" )
57-
58- local connected = sock :connect (host , " http" )
59- ok (connected , " connection should succeed" )
60-
61- local blocking_set = sock :set_blocking (false )
62- ok (blocking_set , " setting non-blocking mode should succeed" )
44+ local sock = assert (socket .create (" inet" , " stream" , " tcp" ))
45+ assert (sock :connect (host , " http" ))
46+ assert (sock :set_blocking (false ))
6347
6448 local str = " "
6549 local total_length
6650
6751 while true do
6852 if sock :is_connected () then
69- local sent = sock :send (
53+ assert ( sock :send (
7054 " GET / HTTP/1.1\r\n " ..
7155 " Host: " .. host .. " \r\n " ..
7256 " User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n " ..
@@ -76,11 +60,10 @@ test('TCP client non-blocking test', function()
7660 " Connection: keep-alive\r\n " ..
7761 " Upgrade-Insecure-Requests: 1\r\n " ..
7862 " \r\n "
79- )
80- ok (sent , " sending HTTP request should succeed" )
63+ ))
8164
8265 while true do
83- local chunk , err , num = sock :receive ()
66+ local chunk , err = sock :receive ()
8467
8568 if chunk then
8669 str = str .. chunk
@@ -95,8 +78,8 @@ test('TCP client non-blocking test', function()
9578 ok (string.find (str , " </html>" , nil , true ) ~= nil , " response should contain closing html tag" )
9679 return
9780 end
98- elseif err ~= " timeout " then
99- ok ( false , " receive error: " .. tostring (err ))
81+ elseif err ~= " tryagain " then
82+ error ( " receive error: " .. tostring (err ))
10083 return
10184 end
10285 end
0 commit comments