Implements pollUpdates
This commit is contained in:
@@ -3,6 +3,7 @@ package web_test
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -231,3 +232,70 @@ func TestAuthentication(t *testing.T) {
|
||||
t.Fatalf("Unexpected body: %s (expected %s)", body, "OK")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdates(t *testing.T) {
|
||||
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
|
||||
httpServer := httptest.NewServer(s)
|
||||
|
||||
messageSeq := 0
|
||||
|
||||
// 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(),
|
||||
}
|
||||
|
||||
// This should enqueue an update item
|
||||
s.Server.ReceiveMessage(&conversation, message)
|
||||
|
||||
resp, err := http.Get(httpServer.URL + fmt.Sprintf("/pollUpdates?seq=%d", messageSeq))
|
||||
if err != nil {
|
||||
t.Fatalf("TestUpdates error: %s", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("Unexpected status code: %d (expected %d)", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Error decoding body: %s", body)
|
||||
}
|
||||
|
||||
var updates []model.UpdateItem
|
||||
err = json.Unmarshal(body, &updates)
|
||||
if err != nil {
|
||||
t.Fatalf("Error unmarshalling JSON: %s", err)
|
||||
}
|
||||
|
||||
if len(updates) != 1 {
|
||||
t.Fatalf("Unexpected num updates: %d (expected %d)", len(updates), 1)
|
||||
}
|
||||
|
||||
update := updates[0]
|
||||
|
||||
// Message seq should be >= messageSeq
|
||||
messageSeq = update.MessageSequenceNumber
|
||||
if messageSeq != 1 {
|
||||
t.Fatalf("Unexpected message seq: %d (expected >= 0)", messageSeq)
|
||||
}
|
||||
|
||||
if update.Conversation.Guid != conversation.Guid {
|
||||
t.Fatalf("Unexpected conversation guid: %s (expected %s)", update.Conversation.Guid, conversation.Guid)
|
||||
}
|
||||
|
||||
if update.Message.Text != message.Text {
|
||||
t.Fatalf("Unexpected message text: %s (expected %s)", update.Message.Text, message.Text)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user