Implements pollUpdates
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -159,11 +160,6 @@ func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
|
||||
// Stub: return 205 (Nothing to report)
|
||||
w.WriteHeader(http.StatusResetContent)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
// Decode request body as SendMessageRequest
|
||||
var sendMessageReq SendMessageRequest
|
||||
@@ -196,6 +192,43 @@ func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Reques
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: This should block if we don't have updates for that seq yet.
|
||||
|
||||
seq := -1
|
||||
seqString := r.URL.Query().Get("seq")
|
||||
if len(seqString) > 0 {
|
||||
var err error
|
||||
seq, err = strconv.Atoi(seqString)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("FetchUpdates: Error parsing seq")
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch updates (blocking)
|
||||
updates := m.Server.FetchUpdatesBlocking(seq)
|
||||
|
||||
if len(updates) == 0 {
|
||||
// return 205 (Nothing to report)
|
||||
w.WriteHeader(http.StatusResetContent)
|
||||
log.Info().Msg("FetchUpdates: Nothing to report")
|
||||
} else {
|
||||
// Encode updates as JSON
|
||||
jsonData, err := json.Marshal(updates)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error marshalling updates")
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Write JSON to response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jsonData)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m.logRequest(r, r.URL.Query().Encode())
|
||||
m.mux.ServeHTTP(w, r)
|
||||
|
||||
@@ -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