diff --git a/server/server.go b/server/server.go index 035821d..a9799c6 100644 --- a/server/server.go +++ b/server/server.go @@ -238,9 +238,10 @@ func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message { return m } if len(s) > fcmMessageLimitReal { - over := len(s) - fcmMessageLimitReal + over := len(s) - fcmMessageLimitReal + 16 // = len("truncated":"1",), sigh ... message, ok := m.Data["message"] if ok && len(message) > over { + m.Data["truncated"] = "1" m.Data["message"] = message[:len(message)-over] } } diff --git a/server/server_test.go b/server/server_test.go index b9bcf8b..92952a3 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -618,9 +618,37 @@ func TestServer_MaybeTruncateFCMMessage(t *testing.T) { truncatedMessageLength := len(truncatedFCMMessage.Data["message"]) serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage) require.Equal(t, fcmMessageLimitReal, len(serializedTruncatedFCMMessage)) + require.Equal(t, "1", truncatedFCMMessage.Data["truncated"]) require.NotEqual(t, origMessageLength, truncatedMessageLength) } +func TestServer_MaybeTruncateFCMMessage_NotTooLong(t *testing.T) { + origMessage := "not really a long string" + origFCMMessage := &messaging.Message{ + Topic: "mytopic", + Data: map[string]string{ + "id": "abcdefg", + "time": "1641324761", + "event": "message", + "topic": "mytopic", + "priority": "0", + "tags": "", + "title": "", + "message": origMessage, + }, + } + origMessageLength := len(origFCMMessage.Data["message"]) + serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage) + require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimitReal) // Pre-condition + + notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage) + notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"]) + serializedNotTruncatedFCMMessage, _ := json.Marshal(notTruncatedFCMMessage) + require.Equal(t, origMessageLength, notTruncatedMessageLength) + require.Equal(t, len(serializedOrigFCMMessage), len(serializedNotTruncatedFCMMessage)) + require.Equal(t, "", notTruncatedFCMMessage.Data["truncated"]) +} + func newTestConfig(t *testing.T) *Config { conf := NewConfig() conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")