diff options
Diffstat (limited to 'test/httptest.lua')
-rwxr-xr-x | test/httptest.lua | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index 479c286..7e38533 100755 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -29,40 +29,43 @@ local function printf(...) return write(format(...)) end -local domain, port = 'www.google.com', 'http' ---local domain, port = 'localhost', '8080' -local done = false +local domain, port = arg[1] or 'www.google.com', 'http' +local running = 0 -utils.spawn(function() - local conn, name, port = assert(io.tcp.connect(domain, port)) +local function get(n, close) + running = running + 1 - printf('\nConnected to %s:%u\n', name, port) + local req + if close then + req = 'GET / HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n' + else + req = 'GET / HTTP/1.1\r\nHost: %s\r\n\r\n' + end - for i = 1, 2 do - --assert(conn:write('GET / HTTP/1.1\r\nHost: '..domain..'\r\nConnection: close\r\n\r\n')) - assert(conn:write('GET / HTTP/1.1\r\nHost: '..domain..'\r\n\r\n')) + local conn = assert(io.tcp.connect(domain, port)) + assert(conn:write(req:format(domain))) + local res = assert(conn:read('HTTPResponse')) - local res = assert(conn:read('HTTPResponse')) + printf('\n%d: HTTP/%s %d %s\n', n, res.version, res.status, res.text) + for k, v in pairs(res.headers) do + printf('%d: %s: %s\n', n, k, v) + end - printf('\nHTTP/%s %d %s\n', res.version, res.status, res.text) - for k, v in pairs(res.headers) do - printf('%s: %s\n', k, v) - end + local body = assert(res:body()) + printf('\n%d: #body = %d\n', n, #body) - local body = assert(res:body()) - printf('\n#body = %d\n', #body) - --write(body, '\n') - end + conn:close() + running = running - 1 +end - done = true -end) +for i = 1, 2 do + utils.spawn(get, i, (i % 2) == 0) +end -local yield = utils.yield local sleeper = utils.newsleeper() repeat write('.') - --yield() sleeper:sleep(0.001) -until done +until running == 0 -- vim: set ts=2 sw=2 noet: |