Private
Public Access
1
0

messages: Implements /messages API

This commit is contained in:
2023-06-19 18:31:05 -07:00
parent cdf3d922f7
commit 2d415a1170
6 changed files with 183 additions and 6 deletions

View File

@@ -82,6 +82,35 @@ func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Requ
w.Write(jsonData)
}
func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) {
guid := r.URL.Query().Get("guid")
if len(guid) == 0 {
log.Println("handleMessage: Got empty guid parameter")
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
return
}
conversation, err := m.Server.ConversationForGUID(guid)
if err != nil {
log.Printf("handleMessage: Error getting conversation (%s): %s", guid, err)
http.Error(w, "conversation not found", http.StatusBadRequest)
return
}
messages := m.Server.MessagesForConversation(*conversation)
jsonData, err := json.Marshal(messages)
if err != nil {
log.Printf("Error marshalling messages: %s", err)
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) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
// Decode request body as AuthenticationRequest
var authReq AuthenticationRequest
@@ -130,6 +159,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
return &this
}

View File

@@ -107,6 +107,60 @@ func TestConversations(t *testing.T) {
}
}
func TestMessages(t *testing.T) {
server := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{})
httpServer := httptest.NewServer(server)
const sender = "Alice"
const text = "This is a test."
conversation := model.Conversation{
Date: time.Now(),
Participants: []string{sender},
UnreadCount: 1,
Guid: "1234567890",
}
server.Server.AddConversation(conversation)
message := model.Message{
Text: text,
Sender: &conversation.Participants[0],
Date: time.Now(),
}
server.Server.AppendMessageToConversation(&conversation, message)
resp, err := http.Get(httpServer.URL + "/messages?guid=" + *&conversation.Guid)
if err != nil {
t.Fatalf("TestMessages error: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error decoding body: %s", body)
}
var messages []model.Message
err = json.Unmarshal(body, &messages)
if err != nil {
t.Fatalf("Error unmarshalling JSON: %s", err)
}
if len(messages) != 1 {
t.Fatalf("Unexpected num messages: %d (expected %d)", len(messages), 1)
}
fetchedMessage := messages[0]
if fetchedMessage.Text != message.Text {
t.Fatalf("Unexpected message text: %s (expected %s)", fetchedMessage.Text, message.Text)
}
if *fetchedMessage.Sender != *message.Sender {
t.Fatalf("Unexpected message sender: %s (expected %s)", *fetchedMessage.Sender, *message.Sender)
}
}
func TestAuthentication(t *testing.T) {
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
httpServer := httptest.NewServer(s)