-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpayload_test.go
76 lines (67 loc) · 1.63 KB
/
payload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package apns2_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/sger/go-apns2"
)
func TestPayload(t *testing.T) {
var tests = []struct {
input apns2.Payload
expected []byte
}{
{
apns2.Payload{
Alert: apns2.Alert{Body: "Hello World"},
},
[]byte(`{"aps":{"alert":"Hello World"}}`),
},
{
apns2.Payload{
Alert: apns2.Alert{
Title: "My Title",
Body: "Hello APNS 2"},
},
[]byte(`{"aps":{"alert":{"title":"My Title","body":"Hello APNS 2"}}}`),
},
{
apns2.Payload{
Alert: apns2.Alert{
Title: "My Title",
Body: "Hello APNS 2",
LocKey: "GAME_PLAY_REQUEST_FORMAT",
LocArgs: []string{"Jenna", "Frank"},
},
},
[]byte(`{"aps":{"alert":{"title":"My Title","body":"Hello APNS 2","loc-key":"GAME_PLAY_REQUEST_FORMAT","loc-args":["Jenna","Frank"]}}}`),
},
{
apns2.Payload{
Alert: apns2.Alert{
Title: "My Title",
Body: "Hello APNS 2",
LocKey: "GAME_PLAY_REQUEST_FORMAT",
LocArgs: []string{"Jenna", "Frank"},
},
Badge: 2,
},
[]byte(`{"aps":{"alert":{"title":"My Title","body":"Hello APNS 2","loc-key":"GAME_PLAY_REQUEST_FORMAT","loc-args":["Jenna","Frank"]},"badge":2}}`),
},
}
for _, tt := range tests {
testPayload(t, tt.input, tt.expected)
}
}
func testPayload(t *testing.T, p interface{}, expected []byte) {
payloadSize := 256
b, err := json.Marshal(p)
if err != nil {
t.Fatal("Error", err)
}
if len(b) > payloadSize {
t.Errorf("Expected payload to be less than %v instead sent %v", payloadSize, len(b))
}
if !reflect.DeepEqual(b, expected) {
t.Errorf("Expected %s, got %s", expected, b)
}
}