local cfg = dstest.config({
substrate = "docker",
seed = 0x7C97,
http_timeout = 10,
})
local server = dstest.setup(cfg, {
image = "kennethreitz/httpbin",
ports = { 80 },
})
dstest.info("waiting for httpbin to accept connections...")
dstest.exec(server, { "sleep", "5" })
local function protocol_rounds(n)
return coroutine.wrap(function()
for r = 1, n do
coroutine.yield(r)
end
end)
end
local function pick_path()
return dstest.random.choice({ "/get", "/status/200", "/headers" })
end
local function raw_round_trip(path)
local conn, err = dstest.net.tcp(server, 80)
assert(conn, "raw TCP connect failed: " .. tostring(err))
conn:set_timeout(10)
conn:send(string.format(
"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
path, tostring(conn:addr())
))
local status_line = conn:recv_line()
assert(status_line, "connection closed before a status line")
conn:recv_until("\r\n\r\n")
conn:close()
return status_line:gsub("\r\n", "")
end
local count = 0
for _ in protocol_rounds(3) do
local path = pick_path() local status_line = raw_round_trip(path)
dstest.info(string.format(" %-12s -> %s", path, status_line))
assert(status_line:match("^HTTP/1%.[01] 200"), path .. " did not return 200")
count = count + 1
end
dstest.info(string.format("raw tcp exchanged %d requests", count))
assert(count == 3, "expected all three requests to complete")
dstest.dst.clear(server)
dstest.info("tcp example complete")