Skip to content

Commit 74e3f4e

Browse files
AchoArnoldCopilot
andcommitted
fix(tests): pass message_id from FCM payload to outstanding endpoint
The emulator was calling GET /v1/messages/outstanding without the required message_id query parameter, causing a 422 validation error. Also fixed response parsing since the API returns a single message object, not an array. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fc6aea4 commit 74e3f4e

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

tests/emulator/events.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
)
1212

1313
// ProcessOutstandingMessages fetches outstanding messages and fires SENT/DELIVERED events.
14-
func (e *Emulator) ProcessOutstandingMessages() {
14+
func (e *Emulator) ProcessOutstandingMessages(messageID string) {
1515
time.Sleep(500 * time.Millisecond)
1616

17-
messages := e.fetchOutstandingMessages()
17+
messages := e.fetchOutstandingMessages(messageID)
1818
for _, msg := range messages {
1919
msgID, ok := msg["id"].(string)
2020
if !ok {
@@ -32,9 +32,9 @@ func (e *Emulator) ProcessOutstandingMessages() {
3232
}
3333
}
3434

35-
// fetchOutstandingMessages calls GET /v1/messages/outstanding.
36-
func (e *Emulator) fetchOutstandingMessages() []map[string]interface{} {
37-
url := fmt.Sprintf("%s/v1/messages/outstanding", e.apiBaseURL)
35+
// fetchOutstandingMessages calls GET /v1/messages/outstanding?message_id=<id>.
36+
func (e *Emulator) fetchOutstandingMessages(messageID string) []map[string]interface{} {
37+
url := fmt.Sprintf("%s/v1/messages/outstanding?message_id=%s", e.apiBaseURL, messageID)
3838

3939
req, err := http.NewRequest(http.MethodGet, url, nil)
4040
if err != nil {
@@ -67,21 +67,15 @@ func (e *Emulator) fetchOutstandingMessages() []map[string]interface{} {
6767
return nil
6868
}
6969

70-
data, ok := result["data"].([]interface{})
70+
// The API returns a single message object in "data", not an array
71+
data, ok := result["data"].(map[string]interface{})
7172
if !ok {
72-
log.Printf("[EVENTS] no data array in response")
73+
log.Printf("[EVENTS] no data object in response")
7374
return nil
7475
}
7576

76-
var messages []map[string]interface{}
77-
for _, item := range data {
78-
if msg, ok := item.(map[string]interface{}); ok {
79-
messages = append(messages, msg)
80-
}
81-
}
82-
83-
log.Printf("[EVENTS] found %d outstanding messages", len(messages))
84-
return messages
77+
log.Printf("[EVENTS] found outstanding message")
78+
return []map[string]interface{}{data}
8579
}
8680

8781
// fireEvent posts a message event (SENT or DELIVERED).

tests/emulator/fcm_handler.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"log"
56

67
"github.com/gofiber/fiber/v3"
78
)
89

10+
// fcmRequest represents the FCM push notification payload from the API.
11+
type fcmRequest struct {
12+
Message struct {
13+
Data map[string]string `json:"data"`
14+
} `json:"message"`
15+
}
16+
917
// HandleFCM receives FCM push notification requests from the API.
1018
// After receiving a push, it triggers SENT and DELIVERED events asynchronously.
1119
func (e *Emulator) HandleFCM(c fiber.Ctx) error {
1220
log.Printf("[FCM] %s %s", c.Method(), c.Path())
1321
log.Printf("[FCM] body: %s", string(c.Body()))
1422

23+
var req fcmRequest
24+
if err := json.Unmarshal(c.Body(), &req); err != nil {
25+
log.Printf("[FCM] error parsing body: %v", err)
26+
return c.Status(400).JSON(fiber.Map{"error": "invalid request"})
27+
}
28+
29+
messageID := req.Message.Data["KEY_MESSAGE_ID"]
30+
if messageID == "" {
31+
log.Printf("[FCM] no KEY_MESSAGE_ID in data payload")
32+
return c.Status(400).JSON(fiber.Map{"error": "missing KEY_MESSAGE_ID"})
33+
}
34+
1535
// Respond with a fake FCM success response
1636
err := c.JSON(fiber.Map{
1737
"name": "projects/httpsms-test/messages/fake-message-id",
1838
})
1939

2040
// Fire events asynchronously (mimics phone receiving push and responding)
21-
go e.ProcessOutstandingMessages()
41+
go e.ProcessOutstandingMessages(messageID)
2242

2343
return err
2444
}

0 commit comments

Comments
 (0)