Avoid panic when push messages are empty or nil (#13751)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3049378506
Коммит
682a1d5d15
@@ -5,6 +5,7 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
@@ -83,16 +84,26 @@ func (me *PushNotification) SetDeviceIdAndPlatform(deviceId string) {
|
||||
}
|
||||
}
|
||||
|
||||
func PushNotificationFromJson(data io.Reader) *PushNotification {
|
||||
func PushNotificationFromJson(data io.Reader) (*PushNotification, error) {
|
||||
if data == nil {
|
||||
return nil, errors.New("push notification data can't be nil")
|
||||
}
|
||||
var me *PushNotification
|
||||
json.NewDecoder(data).Decode(&me)
|
||||
return me
|
||||
if err := json.NewDecoder(data).Decode(&me); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return me, nil
|
||||
}
|
||||
|
||||
func PushNotificationAckFromJson(data io.Reader) *PushNotificationAck {
|
||||
func PushNotificationAckFromJson(data io.Reader) (*PushNotificationAck, error) {
|
||||
if data == nil {
|
||||
return nil, errors.New("push notification data can't be nil")
|
||||
}
|
||||
var ack *PushNotificationAck
|
||||
json.NewDecoder(data).Decode(&ack)
|
||||
return ack
|
||||
if err := json.NewDecoder(data).Decode(&ack); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ack, nil
|
||||
}
|
||||
|
||||
func (ack *PushNotificationAck) ToJson() string {
|
||||
|
||||
@@ -11,11 +11,49 @@ import (
|
||||
)
|
||||
|
||||
func TestPushNotification(t *testing.T) {
|
||||
msg := PushNotification{Platform: "test"}
|
||||
json := msg.ToJson()
|
||||
result := PushNotificationFromJson(strings.NewReader(json))
|
||||
t.Run("should build a push notification from JSON", func(t *testing.T) {
|
||||
msg := PushNotification{Platform: "test"}
|
||||
json := msg.ToJson()
|
||||
result, err := PushNotificationFromJson(strings.NewReader(json))
|
||||
|
||||
require.Equal(t, msg.Platform, result.Platform, "Ids do not match")
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, msg.Platform, result.Platform, "ids do not match")
|
||||
})
|
||||
|
||||
t.Run("should throw an error when the message is nil", func(t *testing.T) {
|
||||
_, err := PushNotificationFromJson(nil)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "push notification data can't be nil", err.Error())
|
||||
})
|
||||
|
||||
t.Run("should throw an error when the message parsing fails", func(t *testing.T) {
|
||||
_, err := PushNotificationFromJson(strings.NewReader(""))
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "EOF", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestPushNotificationAck(t *testing.T) {
|
||||
t.Run("should build a push notification ack from JSON", func(t *testing.T) {
|
||||
msg := PushNotificationAck{ClientPlatform: "test"}
|
||||
json := msg.ToJson()
|
||||
result, err := PushNotificationAckFromJson(strings.NewReader(json))
|
||||
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, msg.ClientPlatform, result.ClientPlatform, "ids do not match")
|
||||
})
|
||||
|
||||
t.Run("should throw an error when the message is nil", func(t *testing.T) {
|
||||
_, err := PushNotificationAckFromJson(nil)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "push notification data can't be nil", err.Error())
|
||||
})
|
||||
|
||||
t.Run("should throw an error when the message parsing fails", func(t *testing.T) {
|
||||
_, err := PushNotificationAckFromJson(strings.NewReader(""))
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "EOF", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
func TestPushNotificationDeviceId(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user