fix websocket messaging, renamed http functions, start sending message via http
This commit is contained in:
+64
-54
@@ -1,68 +1,78 @@
|
||||
#!/bin/bash
|
||||
# Send a message on the group via WebSocket (requires websocat)
|
||||
# Send message from user_one to the group, verify user_two receives it
|
||||
source "$(dirname "$0")/config.sh"
|
||||
|
||||
TOKEN1=$(load_state "TOKEN1")
|
||||
TOKEN2=$(load_state "TOKEN2")
|
||||
GROUP_ID=$(load_state "GROUP_ID")
|
||||
|
||||
echo "=== Debug: loaded state ==="
|
||||
echo " TOKEN1: ${TOKEN1:0:20}..."
|
||||
echo " GROUP_ID: $GROUP_ID"
|
||||
|
||||
if [[ -z "$TOKEN1" || -z "$GROUP_ID" ]]; then
|
||||
if [[ -z "$TOKEN1" || -z "$TOKEN2" || -z "$GROUP_ID" ]]; then
|
||||
echo "ERROR: Missing state. Run previous scripts first."
|
||||
echo " TOKEN1=$TOKEN1"
|
||||
echo " TOKEN2=$TOKEN2"
|
||||
echo " GROUP_ID=$GROUP_ID"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v websocat &>/dev/null; then
|
||||
echo "ERROR: websocat is not installed."
|
||||
echo "Install it with: cargo install websocat"
|
||||
echo " or: pacman -S websocat"
|
||||
MESSAGE="Hello from user_one!"
|
||||
|
||||
echo "=== Sending message from user_one to group $GROUP_ID ==="
|
||||
|
||||
TMPDIR=$(mktemp -d)
|
||||
trap 'rm -rf "$TMPDIR"' EXIT
|
||||
|
||||
echo "[DEBUG] tmpdir: $TMPDIR"
|
||||
echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
|
||||
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
|
||||
echo "[DEBUG] GROUP_ID: $GROUP_ID"
|
||||
|
||||
# Receiver (user_two): authenticate then wait for messages
|
||||
echo "[DEBUG] starting receiver (user_two)..."
|
||||
{ echo '{"token":"'"$TOKEN2"'"}'; sleep 5; } \
|
||||
| stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" &
|
||||
RECV_PID=$!
|
||||
echo "[DEBUG] receiver PID: $RECV_PID"
|
||||
sleep 0.5
|
||||
|
||||
# Sender (user_one): authenticate, send message
|
||||
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}'
|
||||
echo "[DEBUG] starting sender (user_one)..."
|
||||
echo "[DEBUG] send payload: $SEND_PAYLOAD"
|
||||
{ echo '{"token":"'"$TOKEN1"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \
|
||||
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" &
|
||||
SEND_PID=$!
|
||||
echo "[DEBUG] sender PID: $SEND_PID"
|
||||
|
||||
echo "[DEBUG] waiting 2s for message delivery..."
|
||||
sleep 2
|
||||
|
||||
echo "[DEBUG] killing connections..."
|
||||
kill $RECV_PID $SEND_PID 2>/dev/null
|
||||
wait $RECV_PID 2>/dev/null
|
||||
RECV_EXIT=$?
|
||||
wait $SEND_PID 2>/dev/null
|
||||
SEND_EXIT=$?
|
||||
echo "[DEBUG] receiver exit: $RECV_EXIT, sender exit: $SEND_EXIT"
|
||||
|
||||
RECV_ERR=$(cat "$TMPDIR/recv_err")
|
||||
SEND_ERR=$(cat "$TMPDIR/send_err")
|
||||
SEND_OUT=$(cat "$TMPDIR/send_out")
|
||||
[[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR"
|
||||
[[ -n "$SEND_ERR" ]] && echo "[DEBUG] sender stderr: $SEND_ERR"
|
||||
[[ -n "$SEND_OUT" ]] && echo "[DEBUG] sender received: $SEND_OUT"
|
||||
|
||||
RESPONSE=$(cat "$TMPDIR/received")
|
||||
echo "user_two received: $RESPONSE"
|
||||
|
||||
if [[ -z "$RESPONSE" ]]; then
|
||||
echo "[DEBUG] EMPTY response - no data received by user_two"
|
||||
echo "FAIL: message not received"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
AUTH_MSG="{\"token\":\"$TOKEN1\"}"
|
||||
CHAT_MSG="{\"subject\":$GROUP_ID,\"content\":\"Hello from user1!\"}"
|
||||
|
||||
echo ""
|
||||
echo "=== Sending message to group $GROUP_ID as user1 ==="
|
||||
echo " Auth payload: $AUTH_MSG"
|
||||
echo " Chat payload: $CHAT_MSG"
|
||||
echo ""
|
||||
|
||||
echo "[*] Connecting to ws://localhost:8080/ws ..."
|
||||
|
||||
# Use a temp file to capture all ws output
|
||||
WS_OUT=$(mktemp)
|
||||
trap "rm -f $WS_OUT" EXIT
|
||||
|
||||
(
|
||||
echo "[>] Sending auth message..." >&2
|
||||
echo "$AUTH_MSG"
|
||||
sleep 2
|
||||
echo "[>] Auth sent. Sending chat message..." >&2
|
||||
echo "$CHAT_MSG"
|
||||
sleep 2
|
||||
echo "[>] Chat message sent. Closing..." >&2
|
||||
) | websocat -v ws://localhost:8080/ws 2>&1 | tee "$WS_OUT" &
|
||||
|
||||
WS_PID=$!
|
||||
|
||||
# Wait for websocat to finish or timeout after 10s
|
||||
WAITED=0
|
||||
while kill -0 $WS_PID 2>/dev/null && [[ $WAITED -lt 10 ]]; do
|
||||
sleep 1
|
||||
WAITED=$((WAITED + 1))
|
||||
done
|
||||
|
||||
if kill -0 $WS_PID 2>/dev/null; then
|
||||
echo "[!] websocat still running after ${WAITED}s, killing..."
|
||||
kill $WS_PID 2>/dev/null
|
||||
wait $WS_PID 2>/dev/null
|
||||
if echo "$RESPONSE" | grep -q "$MESSAGE"; then
|
||||
echo "PASS"
|
||||
else
|
||||
echo "FAIL: message not received"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "=== WebSocket output ==="
|
||||
cat "$WS_OUT"
|
||||
echo ""
|
||||
echo "=== Done ==="
|
||||
|
||||
Reference in New Issue
Block a user