2023-05-14 21:16:58 +01:00
|
|
|
local net = require("@lune/net")
|
|
|
|
local process = require("@lune/process")
|
2023-08-20 22:55:06 +01:00
|
|
|
local stdio = require("@lune/stdio")
|
2023-06-08 10:21:00 +01:00
|
|
|
local task = require("@lune/task")
|
2023-05-14 21:16:58 +01:00
|
|
|
|
2024-05-12 12:30:32 +01:00
|
|
|
local PORT = 8082
|
2023-02-11 22:29:17 +00:00
|
|
|
local URL = `http://127.0.0.1:{PORT}`
|
2024-01-19 19:39:57 +00:00
|
|
|
local URL_EXTERNAL = `http://0.0.0.0`
|
2023-02-04 00:27:56 +00:00
|
|
|
local RESPONSE = "Hello, lune!"
|
|
|
|
|
2024-01-19 19:39:57 +00:00
|
|
|
-- A server should never be running before testing
|
|
|
|
local isRunning = pcall(net.request, URL)
|
|
|
|
assert(not isRunning, `a server is already running at {URL}`)
|
|
|
|
|
2023-08-20 22:55:06 +01:00
|
|
|
-- Serve should not block the thread from continuing
|
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
local thread = task.delay(1, function()
|
2023-08-20 22:55:06 +01:00
|
|
|
stdio.ewrite("Serve must not block the current thread\n")
|
2023-08-21 00:54:34 +01:00
|
|
|
task.wait(1)
|
2023-02-14 12:17:07 +00:00
|
|
|
process.exit(1)
|
|
|
|
end)
|
|
|
|
|
2023-02-11 21:40:14 +00:00
|
|
|
local handle = net.serve(PORT, function(request)
|
2023-08-22 02:59:43 +01:00
|
|
|
-- print("Request:", request)
|
|
|
|
-- print("Responding with", RESPONSE)
|
2023-02-11 13:25:53 +00:00
|
|
|
assert(request.path == "/some/path")
|
|
|
|
assert(request.query.key == "param2")
|
|
|
|
assert(request.query.key2 == "param3")
|
|
|
|
return RESPONSE
|
2023-02-04 00:27:56 +00:00
|
|
|
end)
|
|
|
|
|
2023-08-20 22:55:06 +01:00
|
|
|
task.cancel(thread)
|
|
|
|
|
|
|
|
-- Serve should respond to a request we send to it
|
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
local thread2 = task.delay(1, function()
|
2023-08-20 22:55:06 +01:00
|
|
|
stdio.ewrite("Serve should respond to requests in a reasonable amount of time\n")
|
2023-08-21 00:54:34 +01:00
|
|
|
task.wait(1)
|
2023-08-20 22:55:06 +01:00
|
|
|
process.exit(1)
|
|
|
|
end)
|
|
|
|
|
2023-02-11 22:29:17 +00:00
|
|
|
local response = net.request(URL .. "/some/path?key=param1&key=param2&key2=param3").body
|
2023-02-04 00:27:56 +00:00
|
|
|
assert(response == RESPONSE, "Invalid response from server")
|
|
|
|
|
2023-08-20 22:55:06 +01:00
|
|
|
task.cancel(thread2)
|
2023-02-04 00:27:56 +00:00
|
|
|
|
2023-02-11 13:25:53 +00:00
|
|
|
-- Stopping is not guaranteed to happen instantly since it is async, but
|
|
|
|
-- it should happen on the next yield, so we wait the minimum amount here
|
2023-08-20 22:55:06 +01:00
|
|
|
handle.stop()
|
2023-02-11 13:25:53 +00:00
|
|
|
task.wait()
|
|
|
|
|
|
|
|
-- Sending a net request may error if there was
|
|
|
|
-- a connection issue, we should handle that here
|
2023-02-11 22:29:17 +00:00
|
|
|
local success, response2 = pcall(net.request, URL)
|
2023-02-11 13:25:53 +00:00
|
|
|
if not success then
|
|
|
|
local message = tostring(response2)
|
|
|
|
assert(
|
|
|
|
string.find(message, "Connection reset")
|
|
|
|
or string.find(message, "Connection closed")
|
2023-09-25 20:14:29 +01:00
|
|
|
or string.find(message, "Connection refused")
|
|
|
|
or string.find(message, "No connection could be made"), -- Windows Request Error
|
2023-02-11 13:25:53 +00:00
|
|
|
"Server did not stop responding to requests"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
assert(not response2.ok, "Server did not stop responding to requests")
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Trying to stop the server again should error and
|
|
|
|
mention that the server has already been stopped
|
|
|
|
|
|
|
|
Note that we cast pcall to any because of a
|
|
|
|
Luau limitation where it throws a type error for
|
|
|
|
`err` because handle.stop doesn't return any value
|
|
|
|
]]
|
|
|
|
local success2, err = (pcall :: any)(handle.stop)
|
|
|
|
assert(not success2, "Calling stop twice on the net serve handle should error")
|
|
|
|
local message = tostring(err)
|
|
|
|
assert(
|
|
|
|
string.find(message, "stop")
|
|
|
|
or string.find(message, "shutdown")
|
|
|
|
or string.find(message, "shut down"),
|
|
|
|
"The error message for calling stop twice on the net serve handle should be descriptive"
|
|
|
|
)
|
2024-01-19 19:39:57 +00:00
|
|
|
|
|
|
|
-- Serve should be able to bind to other IP addresses
|
|
|
|
local handle2 = net.serve(PORT, {
|
|
|
|
address = URL_EXTERNAL,
|
|
|
|
handleRequest = function(request)
|
|
|
|
return `Response from {URL_EXTERNAL}:{PORT}`
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2024-04-11 13:04:40 +01:00
|
|
|
if process.os == "windows" then
|
|
|
|
-- In Windows, client cannot directly connect to `0.0.0.0`.
|
|
|
|
-- `0.0.0.0` is a non-routable meta-address.
|
|
|
|
URL_EXTERNAL = "http://localhost"
|
|
|
|
end
|
|
|
|
|
2024-01-19 19:39:57 +00:00
|
|
|
-- And any requests to that IP should succeed
|
|
|
|
local response3 = net.request(`{URL_EXTERNAL}:{PORT}`).body
|
|
|
|
assert(response3 ~= nil, "Invalid response from server")
|
|
|
|
|
|
|
|
handle2.stop()
|
|
|
|
|
|
|
|
-- Attempting to serve with a malformed IP address should throw an error
|
|
|
|
local success3 = pcall(function()
|
|
|
|
net.serve(8080, {
|
|
|
|
address = "a.b.c.d",
|
|
|
|
handleRequest = function()
|
|
|
|
return RESPONSE
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert(not success3, "Server was created with malformed address")
|
|
|
|
|
|
|
|
-- We have to manually exit so Windows CI doesn't get stuck forever
|
|
|
|
process.exit(0)
|