Skip to content

Commit

Permalink
Update hs.websocket to not use a deprecated method, and enhance the t…
Browse files Browse the repository at this point in the history
…esting slightly
  • Loading branch information
cmsj committed Dec 19, 2023
1 parent fe98228 commit 70a1cda
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Hammerspoon Tests/HSwebsocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ - (void)testNew {
RUN_LUA_TEST()
}

- (void)testEcho {
- (void)testEchoData {
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(8)
}

- (void)testEchoText {
RUN_TWO_PART_LUA_TEST_WITH_TIMEOUT(8)
}

Expand Down
7 changes: 6 additions & 1 deletion extensions/websocket/libwebsocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ static int websocket_send(lua_State *L) {
BOOL isData = (lua_gettop(L) > 2) ? (BOOL)(lua_toboolean(L, 3)) : YES ;

NSUInteger options = isData ? LS_NSLuaStringAsDataOnly : LS_NSPreserveLuaStringExactly;
[ws.webSocket send:[skin toNSObjectAtIndex:2 withOptions:options]];

if (isData) {
[ws.webSocket sendData:[skin toNSObjectAtIndex:2 withOptions:options] error:nil];
} else {
[ws.webSocket sendString:[skin toNSObjectAtIndex:2 withOptions:options] error:nil];
}

lua_pushvalue(L, 1);
return 1;
Expand Down
38 changes: 34 additions & 4 deletions extensions/websocket/test_websocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,57 @@ function testNew()
end

--
-- Test sending an echo:
-- Test sending a binary echo:
--
local echoTestObj = nil
local event = ""
local message = ""

function testEcho()
function testEchoData()
echoTestObj = websocket.new(ECHO_URL, function(e, m)
event = e
message = m
end)
requestTimer = doAfter(2, function()
log("testEcho() sending test string")
echoTestObj:send(TEST_STRING)
echoTestObj:send(TEST_STRING, true)
echoTestObj:close()
end)
return success()
end

function testEchoValues()
function testEchoDataValues()
if type(event) == "string" and event == "received" and type(message) == "string" and message == TEST_STRING then
echoTestObj = nil
event = ""
message = ""
return success()
else
return "Waiting for echo...'"..echoTestObj:status().."', "
end
end

--
-- Test sending a text echo:
--
function testEchoText()
echoTestObj = websocket.new(ECHO_URL, function(e, m)
event = e
message = m
end)
requestTimer = doAfter(2, function()
log("testEcho() sending test string")
echoTestObj:send(TEST_STRING, false)
echoTestObj:close()
end)
return success()
end

function testEchoTextValues()
if type(event) == "string" and event == "received" and type(message) == "string" and message == TEST_STRING then
echoTestObj = nil
event = ""
message = ""
return success()
else
return "Waiting for echo...'"..echoTestObj:status().."', "
Expand Down

0 comments on commit 70a1cda

Please sign in to comment.