Update sendMessage to return inserted guid for new protocol
This commit is contained in:
5
web/response_types.go
Normal file
5
web/response_types.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package web
|
||||
|
||||
type SendMessageResponse struct {
|
||||
Guid string `json:"guid"`
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user