diff --git a/src/import/OnlinePanel.lua b/src/import/OnlinePanel.lua index 9d7568aa..d1d042e6 100644 --- a/src/import/OnlinePanel.lua +++ b/src/import/OnlinePanel.lua @@ -1763,6 +1763,8 @@ function OnlinePanel.doConnect(imp) if not ok then st.status = tostring(err or "the relay didn't answer") st.statusOk = false + else + st.status, st.statusOk = nil, false end return ok end @@ -1776,7 +1778,7 @@ function OnlinePanel.connect(imp) st.connectWanted = true st.ticketState = "pending" local started = OnlinePanel.startJob(imp, "ticket", function(client) - return client:lobbyTicket() + return client:lobbyTicket(st.name) end) if started then return true end st.ticketState = "unsupported" diff --git a/src/sync/SyncClient.lua b/src/sync/SyncClient.lua index 3faa44f6..6883a625 100644 --- a/src/sync/SyncClient.lua +++ b/src/sync/SyncClient.lua @@ -74,9 +74,13 @@ function SyncClient:send(method, path, body, opts) local headers = { ["Accept"] = "application/json" } local payload if body ~= nil then - local ok, encoded = pcall(Json.encode, body) - if not ok then return nil, "could not encode the request" end - payload = encoded + if type(body) == "table" and next(body) == nil then + payload = "{}" + else + local ok, encoded = pcall(Json.encode, body) + if not ok then return nil, "could not encode the request" end + payload = encoded + end headers["Content-Type"] = "application/json" end if not opts.noAuth then @@ -190,8 +194,12 @@ function SyncClient:fetchShare(code) { noAuth = true, params = { code = trimmed } }) end -function SyncClient:lobbyTicket() - return self:send("POST", "/lobby/ticket", {}) +function SyncClient:lobbyTicket(displayName) + local body = {} + if type(displayName) == "string" and displayName ~= "" then + body.displayName = displayName + end + return self:send("POST", "/lobby/ticket", body) end function SyncClient:setDisplayName(name) diff --git a/tests/online_panel.lua b/tests/online_panel.lua index 0c13f69c..48dba4b8 100644 --- a/tests/online_panel.lua +++ b/tests/online_panel.lua @@ -1972,4 +1972,31 @@ do ArenaData.speciesIds = savedSpecies end +-- ------------------------------------------- lobby ticket request shape + +do + local SyncClient = require("src.sync.SyncClient") + local sent + local transport = { + begin = function(_, req) sent = req return { req = req } end, + poll = function() return { status = "pending" } end, + release = function() end, + } + local client = SyncClient.new({ baseUrl = "http://relay.test", + transport = transport }) + client:setAuth("account", "token") + + client:lobbyTicket("RED#417") + T.eq(sent.body, '{"displayName":"RED#417"}', + "the ticket request carries the name the relay needs to mint one") + + client:lobbyTicket(nil) + T.eq(sent.body, "{}", + "a nameless ticket request is still a JSON object, not an empty array") + + client:send("POST", "/sync/unlink", {}) + T.eq(sent.body, "{}", "any empty body is sent as an object") +end + + T.finish("online panel")