From 594e77323629a241da2745fc52c280f40ec38fbd Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 28 Jun 2023 10:38:46 +0200 Subject: [PATCH] Dont output anything from wss test unless its an error --- tests/net/socket/wss.luau | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/net/socket/wss.luau b/tests/net/socket/wss.luau index a8a841c..6594d08 100644 --- a/tests/net/socket/wss.luau +++ b/tests/net/socket/wss.luau @@ -1,25 +1,28 @@ local net = require("@lune/net") -local stdio = require("@lune/stdio") +local serde = require("@lune/serde") local task = require("@lune/task") --- net.socket() will inherently spit out an error if init connection fails, but for --- the sake of this test, prot. call checks are unnecessary. Also, we're going to use --- Discord's WebSocket gateway server for soley testing wss, as we don't need to auth --- or anything for that; we just want to make sure it functions.. -print("Connecting to Discord wss gateway..") +-- We're going to use Discord's WebSocket gateway server +-- for testing wss - it does not require auth, and this test +-- only exists to ensure wss (WebSockets with TLS) works correctly local socket = net.socket("wss://gateway.discord.gg/?v=10&encoding=json") -print("Connected!") -- Would've errored at net.socket() call before this if init connection err while not socket.closeCode do - local nextMessage = socket.next() + local response = socket.next() - if nextMessage then - print(`{stdio.style("bold")}Discord:{stdio.style("reset")}`, nextMessage) - - print("Will (deliberately) close the socket in 2 seconds..") - task.wait(2) - socket.close(1000) - end + if response then + local decodeSuccess, decodeMessage = pcall(serde.decode, "json" :: "json", response) + if not decodeSuccess then + error( + string.format( + "Discord API did not respond with valid JSON!\n%s", + tostring(decodeMessage) + ) + ) + end + + -- Close the connection after a second with the success close code + task.wait(1) + socket.close(1000) + end end - -print(`Connection to socket closed with closeCode {socket.closeCode}`)