server: implements /updates websocket
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"code.severnaya.net/kordophone-mock/v2/server"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
|
||||
type MockHTTPServerConfiguration struct {
|
||||
@@ -286,6 +287,21 @@ func (m *MockHTTPServer) handleMarkConversation(w http.ResponseWriter, r *http.R
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handleUpdatesWebsocket(c *websocket.Conn) {
|
||||
// Fetch updates continuously
|
||||
for {
|
||||
// Fetch updates (blocking)
|
||||
updates := m.Server.FetchUpdatesBlocking(-1)
|
||||
|
||||
// Send updates to client
|
||||
err := websocket.JSON.Send(c, updates)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("handleUpdatesWebsocket: Error sending updates to client (probably disconnected)")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m.logRequest(r, r.URL.Query().Encode())
|
||||
m.mux.ServeHTTP(w, r)
|
||||
@@ -307,6 +323,12 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
||||
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
|
||||
this.mux.Handle("/markConversation", http.HandlerFunc(this.handleMarkConversation))
|
||||
|
||||
// /updates websocket
|
||||
this.mux.Handle("/updates", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
s := websocket.Server{Handler: websocket.Handler(this.handleUpdatesWebsocket)}
|
||||
s.ServeHTTP(w, r)
|
||||
}))
|
||||
|
||||
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
||||
|
||||
return &this
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user