forked from timehop/apns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_test.go
229 lines (176 loc) · 6.37 KB
/
notification_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package apns_test
import (
"bytes"
"encoding/binary"
"encoding/json"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/timehop/apns"
)
var _ = Describe("Notifications", func() {
Describe("Alert", func() {
Describe("JSON marshalling", func() {
Context("only body", func() {
It("should just have that field", func() {
a := apns.Alert{Body: "whatup"}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"body\":\"whatup\"}")))
})
})
Context("only loc-key", func() {
It("should just have that field", func() {
a := apns.Alert{LocKey: "localization"}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"loc-key\":\"localization\"}")))
})
})
Context("only loc-args", func() {
It("should just have that field", func() {
a := apns.Alert{LocArgs: []string{"world", "cup"}}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"loc-args\":[\"world\",\"cup\"]}")))
})
})
Context("only action-loc-key", func() {
It("should just have that field", func() {
a := apns.Alert{ActionLocKey: "akshun localization"}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"action-loc-key\":\"akshun localization\"}")))
})
})
Context("only launch image", func() {
It("should just have that field", func() {
a := apns.Alert{LaunchImage: "dee fault"}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"launch-image\":\"dee fault\"}")))
})
})
Context("fully loaded", func() {
It("should serialize", func() {
a := apns.Alert{Body: "USA scores!", LocKey: "game", LocArgs: []string{"USA", "BRA"}, LaunchImage: "scoreboard"}
j, err := json.Marshal(a)
Expect(err).To(BeNil())
Expect(j).To(Equal([]byte("{\"body\":\"USA scores!\",\"loc-key\":\"game\",\"loc-args\":[\"USA\",\"BRA\"],\"launch-image\":\"scoreboard\"}")))
})
})
})
})
Describe("Payload", func() {
Describe("#MarshalJSON", func() {
Context("with only APS", func() {
It("should marshal APS", func() {
p := apns.NewPayload()
p.APS.Alert.Body = "testing"
b, err := json.Marshal(p)
Expect(err).To(BeNil())
Expect(b).To(Equal([]byte("{\"aps\":{\"alert\":{\"body\":\"testing\"}}}")))
})
})
Context("with custom attributes APS", func() {
It("should marshal APS", func() {
p := apns.NewPayload()
p.APS.Alert.Body = "testing"
p.SetCustomValue("email", "come@me.bro")
b, err := json.Marshal(p)
Expect(err).To(BeNil())
Expect(b).To(Equal([]byte("{\"aps\":{\"alert\":{\"body\":\"testing\"}},\"email\":\"come@me.bro\"}")))
})
})
})
})
Describe("Notification", func() {
Describe("#ToBinary", func() {
Context("invalid token format", func() {
n := apns.NewNotification()
n.DeviceToken = "totally not a valid token"
It("should return an error", func() {
_, err := n.ToBinary()
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("convert token to hex error"))
})
})
Context("valid payload", func() {
It("should generate the correct byte payload with expiry", func() {
t := time.Unix(1404102833, 0)
n := apns.NewNotification()
n.Identifier = uint32(123123)
n.DeviceToken = "9999999999999999999999999999999999999999999999999999999999999999"
n.Priority = apns.PriorityImmediate
n.Expiration = &t
b, err := n.ToBinary()
Expect(err).To(BeNil())
buf := bytes.NewBuffer(b)
var expiry uint32
buf.Next(1 + 4 + 1 + 2 + 32 + 1 + 2 + 20 + 1 + 2 + 4 + 1 + 2)
// Expiry
binary.Read(buf, binary.BigEndian, &expiry)
})
It("should generate the correct byte payload", func() {
n := apns.NewNotification()
n.Identifier = uint32(123123)
n.DeviceToken = "9999999999999999999999999999999999999999999999999999999999999999"
n.Priority = apns.PriorityImmediate
b, err := n.ToBinary()
Expect(err).To(BeNil())
buf := bytes.NewBuffer(b)
var command, tokID, payloadID, identifierID, expiryID, priorityID uint8
var tokLen, payloadLen, identifierLen, expiryLen, priorityLen uint16
var frameLen, identifier, expiry uint32
var priority byte
var tok [32]byte
var payload [20]byte
binary.Read(buf, binary.BigEndian, &command)
binary.Read(buf, binary.BigEndian, &frameLen)
// Token
binary.Read(buf, binary.BigEndian, &tokID)
binary.Read(buf, binary.BigEndian, &tokLen)
binary.Read(buf, binary.BigEndian, &tok)
// Payload
binary.Read(buf, binary.BigEndian, &payloadID)
binary.Read(buf, binary.BigEndian, &payloadLen)
binary.Read(buf, binary.BigEndian, &payload)
// Identifier
binary.Read(buf, binary.BigEndian, &identifierID)
binary.Read(buf, binary.BigEndian, &identifierLen)
binary.Read(buf, binary.BigEndian, &identifier)
// Expiry
binary.Read(buf, binary.BigEndian, &expiryID)
binary.Read(buf, binary.BigEndian, &expiryLen)
binary.Read(buf, binary.BigEndian, &expiry)
// Priority
binary.Read(buf, binary.BigEndian, &priorityID)
binary.Read(buf, binary.BigEndian, &priorityLen)
binary.Read(buf, binary.BigEndian, &priority)
Expect(command).To(Equal(uint8(2)))
Expect(frameLen).To(Equal(uint32(76)))
// Token
Expect(tokID).To(Equal(uint8(1)))
Expect(tokLen).To(Equal(uint16(32)))
Expect(tok).To(Equal([32]byte{153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153}))
// Payload
Expect(payloadID).To(Equal(uint8(2)))
Expect(payloadLen).To(Equal(uint16(20)))
Expect(payload).To(Equal([20]byte{123, 34, 97, 112, 115, 34, 58, 123, 34, 97, 108, 101, 114, 116, 34, 58, 123, 125, 125, 125}))
// Identifier
Expect(identifierID).To(Equal(uint8(3)))
Expect(identifierLen).To(Equal(uint16(4)))
Expect(identifier).To(Equal(uint32(123123)))
// Expiry
Expect(expiryID).To(Equal(uint8(4)))
Expect(expiryLen).To(Equal(uint16(4)))
Expect(expiry).To(Equal(uint32(0)))
// Priority
Expect(priorityID).To(Equal(uint8(5)))
Expect(priorityLen).To(Equal(uint16(1)))
Expect(priority).To(Equal(uint8(10)))
})
})
})
})
})