mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-05-07 20:38:46 -05:00
Add the core WebSocket infrastructure: - Message type definitions for the wire protocol (subscribe, unsubscribe, auth, error, push events) - In-memory connection hub that tracks per-user connections and routes messages to subscribed clients - Connection wrapper with auth-after-connect flow: connections start unauthenticated, client sends JWT as first message, only then can subscribe to event topics Includes auth timeout (30s), shared cancellation context for read/write loops, hub map cleanup on last connection removal, and proper error delivery before closing on auth failure.
86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
// Vikunja is a to-do list application to facilitate your life.
|
|
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package websocket
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestHubRegisterUnregister(t *testing.T) {
|
|
h := NewHub()
|
|
conn := &Connection{
|
|
userID: 1,
|
|
subscriptions: make(map[string]bool),
|
|
send: make(chan OutgoingMessage, 16),
|
|
}
|
|
h.Register(conn)
|
|
assert.Len(t, h.connections[1], 1)
|
|
|
|
h.Unregister(conn)
|
|
assert.Empty(t, h.connections[1])
|
|
_, exists := h.connections[1]
|
|
assert.False(t, exists, "map entry should be deleted when last connection is removed")
|
|
}
|
|
|
|
func TestHubPublishToSubscribedConnection(t *testing.T) {
|
|
h := NewHub()
|
|
conn := &Connection{
|
|
userID: 1,
|
|
subscriptions: make(map[string]bool),
|
|
send: make(chan OutgoingMessage, 16),
|
|
}
|
|
h.Register(conn)
|
|
conn.subscriptions["notification.created"] = true
|
|
|
|
h.PublishForUser(1, "notification.created", map[string]string{"id": "1"})
|
|
|
|
msg := <-conn.send
|
|
assert.Equal(t, "notification.created", msg.Event)
|
|
}
|
|
|
|
func TestHubPublishSkipsUnsubscribedConnection(t *testing.T) {
|
|
h := NewHub()
|
|
conn := &Connection{
|
|
userID: 1,
|
|
subscriptions: make(map[string]bool),
|
|
send: make(chan OutgoingMessage, 16),
|
|
}
|
|
h.Register(conn)
|
|
// Not subscribed to "notification.created"
|
|
|
|
h.PublishForUser(1, "notification.created", map[string]string{"id": "1"})
|
|
|
|
assert.Empty(t, conn.send)
|
|
}
|
|
|
|
func TestHubPublishSkipsOtherUsers(t *testing.T) {
|
|
h := NewHub()
|
|
conn := &Connection{
|
|
userID: 2,
|
|
subscriptions: make(map[string]bool),
|
|
send: make(chan OutgoingMessage, 16),
|
|
}
|
|
h.Register(conn)
|
|
conn.subscriptions["notification.created"] = true
|
|
|
|
h.PublishForUser(1, "notification.created", map[string]string{"id": "1"})
|
|
|
|
assert.Empty(t, conn.send)
|
|
}
|