Private
Public Access
1
0

server: implements /updates websocket

This commit is contained in:
2023-08-13 00:33:46 -07:00
parent e9a9d1c064
commit b92d1a2892
4 changed files with 122 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
@@ -14,6 +15,7 @@ import (
"code.severnaya.net/kordophone-mock/v2/model"
"code.severnaya.net/kordophone-mock/v2/server"
"code.severnaya.net/kordophone-mock/v2/web"
"golang.org/x/net/websocket"
)
func TestVersion(t *testing.T) {
@@ -301,6 +303,98 @@ func TestUpdates(t *testing.T) {
}
}
type MessageUpdateError struct {
Message string
}
func (e MessageUpdateError) Error() string {
return e.Message
}
func TestUpdatesWebsocket(t *testing.T) {
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
httpServer := httptest.NewServer(s)
// Mock conversation
guid := "1234567890"
conversation := model.Conversation{
Date: time.Now(),
Participants: []string{"Alice"},
UnreadCount: 0,
Guid: guid,
}
s.Server.AddConversation(conversation)
// Receive a message
message := model.Message{
Text: "This is a test.",
Sender: &conversation.Participants[0],
Date: time.Now(),
}
// Open websocket connection
wsURL := "ws" + strings.TrimPrefix(httpServer.URL, "http") + "/updates"
ws, err := websocket.Dial(wsURL, "ws", httpServer.URL)
if err != nil {
t.Fatalf("Error opening websocket: %s", err)
}
// Await messages on the websocket
messageReceived := make(chan bool)
errorEncountered := make(chan error)
go func() {
// Read from websocket
var updates []model.UpdateItem
err := websocket.JSON.Receive(ws, &updates)
if err != nil {
errorEncountered <- err
return
} else {
if len(updates) != 1 {
errorEncountered <- MessageUpdateError{
fmt.Sprintf("Unexpected num updates: %d (expected %d)", len(updates), 1),
}
return
}
update := updates[0]
if update.Conversation.Guid != conversation.Guid {
errorEncountered <- MessageUpdateError{
fmt.Sprintf("Unexpected conversation guid: %s (expected %s)", update.Conversation.Guid, conversation.Guid),
}
return
}
if update.Message.Text != message.Text {
errorEncountered <- MessageUpdateError{
fmt.Sprintf("Unexpected message text: %s (expected %s)", update.Message.Text, message.Text),
}
return
}
messageReceived <- true
}
}()
// sleep for a bit to allow the websocket to connect
time.Sleep(100 * time.Millisecond)
// This should enqueue an update item
s.Server.ReceiveMessage(&conversation, message)
// Await expectation
select {
case <-messageReceived:
// COOL
case err := <-errorEncountered:
t.Fatalf("Error encountered reading from websocket: %s", err)
case <-time.After(1 * time.Second):
t.Fatalf("Timed out waiting for websocket message")
}
}
func TestMarkConversation(t *testing.T) {
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
httpServer := httptest.NewServer(s)