mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-04-06 06:27:40 +03:00
Merge branch 'main' into attachments
This commit is contained in:
commit
24eb27d41c
8 changed files with 194 additions and 20 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"firebase.google.com/go/messaging"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/http"
|
||||
|
@ -582,6 +583,63 @@ func TestServer_UnifiedPushDiscovery(t *testing.T) {
|
|||
require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String())
|
||||
}
|
||||
|
||||
func TestServer_MaybeTruncateFCMMessage(t *testing.T) {
|
||||
origMessage := strings.Repeat("this is a long string", 300)
|
||||
origFCMMessage := &messaging.Message{
|
||||
Topic: "mytopic",
|
||||
Data: map[string]string{
|
||||
"id": "abcdefg",
|
||||
"time": "1641324761",
|
||||
"event": "message",
|
||||
"topic": "mytopic",
|
||||
"priority": "0",
|
||||
"tags": "",
|
||||
"title": "",
|
||||
"message": origMessage,
|
||||
},
|
||||
Android: &messaging.AndroidConfig{
|
||||
Priority: "high",
|
||||
},
|
||||
}
|
||||
origMessageLength := len(origFCMMessage.Data["message"])
|
||||
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
|
||||
require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
|
||||
|
||||
truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
|
||||
truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
|
||||
serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
|
||||
require.Equal(t, fcmMessageLimit, 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), fcmMessageLimit) // 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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue