From 416949095c09eef4c46b48b0ea2c828acfa9e5b1 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sun, 10 Dec 2023 17:45:40 -0800 Subject: [PATCH] Update sendMessage to return inserted guid for new protocol --- web/response_types.go | 5 ++++ web/server.go | 22 +++++++++++++- web/server_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 web/response_types.go diff --git a/web/response_types.go b/web/response_types.go new file mode 100644 index 0000000..2072ff1 --- /dev/null +++ b/web/response_types.go @@ -0,0 +1,5 @@ +package web + +type SendMessageResponse struct { + Guid string `json:"guid"` +} diff --git a/web/server.go b/web/server.go index a3f6b25..7b5619f 100644 --- a/web/server.go +++ b/web/server.go @@ -224,7 +224,22 @@ func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Reques // Send message m.Server.SendMessage(conversation, message) - w.WriteHeader(http.StatusOK) + // Formulate response + sendMessageResp := SendMessageResponse{ + Guid: message.Guid, + } + + // Encode response as JSON + jsonData, err := json.Marshal(sendMessageResp) + if err != nil { + log.Error().Err(err).Msg("Error marshalling response") + 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) handlePollUpdates(w http.ResponseWriter, r *http.Request) { @@ -315,6 +330,11 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer { authEnabled: config.AuthEnabled, } + // Redirect /api/* to /* + this.mux.Handle("/api/", http.StripPrefix("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, r.URL.Path, http.StatusMovedPermanently) + }))) + this.mux.Handle("/version", http.HandlerFunc(this.handleVersion)) this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations)) this.mux.Handle("/status", http.HandlerFunc(this.handleStatus)) diff --git a/web/server_test.go b/web/server_test.go index 5ce72d7..20cb6ea 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -534,3 +534,71 @@ func TestMessageQueries(t *testing.T) { } } + +func TestSendMessage(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) + + request := web.SendMessageRequest{ + ConversationGUID: "1234567890", + Body: "This is a test.", + TransferGUIDs: []string{}, + } + + // Encode as json + requestJSON, err := json.Marshal(request) + if err != nil { + t.Fatalf("Error marshalling JSON: %s", err) + } + + // Send request + resp, err := http.Post(httpServer.URL+"/sendMessage", "application/json", io.NopCloser(bytes.NewReader(requestJSON))) + if err != nil { + t.Fatalf("TestSendMessage error: %s", err) + } + + if resp.StatusCode != http.StatusOK { + t.Fatalf("Unexpected status code: %d (expected %d)", resp.StatusCode, http.StatusOK) + } + + // Decode response + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("Error decoding body: %s", body) + } + + var response web.SendMessageResponse + err = json.Unmarshal(body, &response) + if err != nil { + t.Fatalf("Error unmarshalling JSON: %s", err) + } + + if response.Guid == "" { + t.Fatalf("Unexpected empty guid") + } + + // Make sure message is present + messages := s.Server.MessagesForConversation(&conversation) + found := false + for _, message := range messages { + if message.Guid == response.Guid { + found = true + break + } + } + + if found != true { + t.Fatalf("Message not found in conversation") + } +}