diff --git a/internal/hub/hub.go b/internal/hub/hub.go index 7560696..246b585 100644 --- a/internal/hub/hub.go +++ b/internal/hub/hub.go @@ -154,7 +154,7 @@ func (h *Hub) HandleWebSocket(w http.ResponseWriter, r *http.Request) { return } - room := r.URL.Query().Get("room") + room := r.URL.Path h.register <- client{conn: conn, room: room} diff --git a/internal/hub/hub_integration_test.go b/internal/hub/hub_integration_test.go index 2860517..b0bc8f0 100644 --- a/internal/hub/hub_integration_test.go +++ b/internal/hub/hub_integration_test.go @@ -38,7 +38,7 @@ func dialWS(t *testing.T, server *httptest.Server) *websocket.Conn { // helper: dial a WebSocket connection to a specific room func dialWSWithRoom(t *testing.T, server *httptest.Server, room string) *websocket.Conn { t.Helper() - wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "?room=" + room + wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/" + room conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) if err != nil { t.Fatalf("Failed to dial WebSocket (room=%q): %v", room, err) @@ -483,13 +483,13 @@ func TestIntegration_BroadcastToEmptyRoom(t *testing.T) { // Send directly to broadcast channel targeting a non-existent room. // This should be handled gracefully (no panic, no delivery). - h.broadcast <- broadcastMsg{room: "non-existent", data: []byte("ghost message")} + h.broadcast <- broadcastMsg{room: "/non-existent", data: []byte("ghost message")} // Give the hub time to process time.Sleep(50 * time.Millisecond) // Now send a real message to room-a to confirm hub is still functional - h.broadcast <- broadcastMsg{room: "room-a", data: []byte("real message")} + h.broadcast <- broadcastMsg{room: "/room-a", data: []byte("real message")} connA.SetReadDeadline(time.Now().Add(time.Second)) _, msg, err := connA.ReadMessage() diff --git a/internal/hub/hub_test.go b/internal/hub/hub_test.go index e455e93..a0cd729 100644 --- a/internal/hub/hub_test.go +++ b/internal/hub/hub_test.go @@ -17,14 +17,14 @@ func newTestLogger() *logging.Logger { } // dialTestHub starts an httptest server for the given hub and dials a -// WebSocket connection to it with the given room query parameter. +// WebSocket connection to it with the given room path. // Returns the client-side connection and a cleanup function. func dialTestHub(t *testing.T, h *Hub, room string) *websocket.Conn { t.Helper() srv := httptest.NewServer(http.HandlerFunc(h.HandleWebSocket)) t.Cleanup(srv.Close) - wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "?room=" + room + wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "/" + room conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) if err != nil { t.Fatalf("failed to dial WebSocket: %v", err) @@ -259,8 +259,8 @@ func TestBroadcastRoomIsolation(t *testing.T) { // Allow registers to be processed time.Sleep(50 * time.Millisecond) - // Send message to room-a via broadcast channel - h.broadcast <- broadcastMsg{room: "room-a", data: []byte("for-a-only")} + // Send message to room-a via broadcast channel (room includes leading slash from URL path) + h.broadcast <- broadcastMsg{room: "/room-a", data: []byte("for-a-only")} // Room-a client should receive the message connA.SetReadDeadline(time.Now().Add(time.Second))