|
| 1 | +require("luacov") |
| 2 | +local lunit = require("lunit") |
| 3 | +local socket = require("ljsocket") |
| 4 | + |
| 5 | +module("tcp_test", lunit.testcase, package.seeall) |
| 6 | + |
| 7 | +function tcp_client_blocking_test() |
| 8 | + 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( |
| 13 | + "GET / HTTP/1.1\r\n".. |
| 14 | + "Host: " .. host .. "\r\n".. |
| 15 | + "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n".. |
| 16 | + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".. |
| 17 | + "Accept-Language: nb,nb-NO;q=0.9,en;q=0.8,no-NO;q=0.6,no;q=0.5,nn-NO;q=0.4,nn;q=0.3,en-US;q=0.1\r\n".. |
| 18 | + "DNT: 1\r\n".. |
| 19 | + "Connection: keep-alive\r\n".. |
| 20 | + "Upgrade-Insecure-Requests: 1\r\n".. |
| 21 | + "\r\n" |
| 22 | + )) |
| 23 | + |
| 24 | + local total_length |
| 25 | + local str = "" |
| 26 | + |
| 27 | + while true do |
| 28 | + local chunk = socket:receive() |
| 29 | + lunit.assert(chunk) |
| 30 | + |
| 31 | + if not chunk then |
| 32 | + break |
| 33 | + end |
| 34 | + |
| 35 | + str = str .. chunk |
| 36 | + |
| 37 | + if not total_length then |
| 38 | + total_length = tonumber(str:match("Content%-Length: (%d+)")) |
| 39 | + end |
| 40 | + |
| 41 | + local magic = "0\r\n\r\n" |
| 42 | + if str:sub(-#magic) == magic or (total_length and #str >= total_length) then |
| 43 | + break |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 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) |
| 50 | +end |
| 51 | + |
| 52 | +function tcp_client_blocking_test() |
| 53 | + 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)) |
| 58 | + |
| 59 | + local str = "" |
| 60 | + local total_length |
| 61 | + |
| 62 | + while true do |
| 63 | + if socket:is_connected() then |
| 64 | + lunit.assert(socket:send( |
| 65 | + "GET / HTTP/1.1\r\n".. |
| 66 | + "Host: "..host.."\r\n".. |
| 67 | + "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n".. |
| 68 | + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".. |
| 69 | + "Accept-Language: nb,nb-NO;q=0.9,en;q=0.8,no-NO;q=0.6,no;q=0.5,nn-NO;q=0.4,nn;q=0.3,en-US;q=0.1\r\n".. |
| 70 | + --"Accept-Encoding: gzip, deflate\r\n".. |
| 71 | + "DNT: 1\r\n".. |
| 72 | + "Connection: keep-alive\r\n".. |
| 73 | + "Upgrade-Insecure-Requests: 1\r\n".. |
| 74 | + "\r\n" |
| 75 | + )) |
| 76 | + |
| 77 | + while true do |
| 78 | + local chunk, err, num = socket:receive() |
| 79 | + |
| 80 | + if chunk then |
| 81 | + str = str .. chunk |
| 82 | + |
| 83 | + if not total_length then |
| 84 | + total_length = tonumber(str:match("Content%-Length: (%d+)")) |
| 85 | + end |
| 86 | + |
| 87 | + if #str >= total_length then |
| 88 | + return |
| 89 | + end |
| 90 | + elseif num ~= 11 then |
| 91 | + error(err) |
| 92 | + end |
| 93 | + end |
| 94 | + else |
| 95 | + socket:poll_connect() |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 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(...) |
0 commit comments