forked from huo-ju/Go-Apns
/
payload_test.go
164 lines (152 loc) · 4.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package apns
import (
"encoding/json"
"testing"
)
func TestAlertMarshal(t *testing.T) {
{
alert := Alert{}
alert.LockKey = "GAME_PLAY_REQUEST_FORMAT"
alert.LockArgs = []string{"Jenna", "Frank"}
j, err := json.Marshal(alert)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"loc-key":"GAME_PLAY_REQUEST_FORMAT","loc-args":["Jenna","Frank"]}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
alert := Alert{}
alert.Body = "Bob wants to play poker"
alert.ActionLockKey = "PLAY"
j, err := json.Marshal(alert)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"body":"Bob wants to play poker","action-loc-key":"PLAY"}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
}
func TestApsMarshal(t *testing.T) {
{
aps := Aps{}
aps.AlertStruct = &Alert{
LockKey: "GAME_PLAY_REQUEST_FORMAT",
LockArgs: []string{"Jenna", "Frank"},
}
j, err := json.Marshal(aps)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"alert":{"loc-key":"GAME_PLAY_REQUEST_FORMAT","loc-args":["Jenna","Frank"]}}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
aps := Aps{}
aps.Alert = "Message received from Bob"
j, err := json.Marshal(aps)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"alert":"Message received from Bob"}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
aps := Aps{}
aps.AlertStruct = &Alert{
Body: "Bob wants to play poker",
ActionLockKey: "PLAY",
}
aps.Badge = 5
j, err := json.Marshal(aps)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"alert":{"body":"Bob wants to play poker","action-loc-key":"PLAY"},"badge":5}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
aps := Aps{}
aps.Alert = "You got your emails."
aps.Badge = 9
aps.Sound = "bingbong.aiff"
j, err := json.Marshal(aps)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"alert":"You got your emails.","badge":9,"sound":"bingbong.aiff"}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
aps := Aps{}
j, err := json.Marshal(aps)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
}
func TestPayloadMarshal(t *testing.T) {
{
payload := Payload{}
payload.Aps.Alert = "Message received from Bob"
payload.SetCustom("acme2", []string{"bang", "whiz"})
j, err := json.Marshal(payload)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"acme2":["bang","whiz"],"aps":{"alert":"Message received from Bob"}}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
payload := Payload{}
payload.Aps.Alert = "You got your emails."
payload.Aps.Badge = 9
payload.Aps.Sound = "bingbong.aiff"
payload.SetCustom("acme1", "bar")
payload.SetCustom("acme2", 42)
j, err := json.Marshal(payload)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"acme1":"bar","acme2":42,"aps":{"alert":"You got your emails.","badge":9,"sound":"bingbong.aiff"}}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
payload := Payload{}
payload.SetCustom("acme2", []int{5, 8})
j, err := json.Marshal(payload)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"acme2":[5,8],"aps":{}}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
{
payload := Payload{}
payload.Aps.AlertStruct = &Alert{
LockKey: "GAME_PLAY_REQUEST_FORMAT",
LockArgs: []string{"Jenna", "Frank"},
}
payload.Aps.Sound = "chime"
payload.SetCustom("acme", "foo")
j, err := json.Marshal(payload)
if err != nil {
t.Fatalf("can't marshal to json: %s", err)
}
if got, expect := string(j), `{"acme":"foo","aps":{"alert":{"loc-key":"GAME_PLAY_REQUEST_FORMAT","loc-args":["Jenna","Frank"]},"sound":"chime"}}`; got != expect {
t.Errorf("got: %s, expect: %s", got, expect)
}
}
}