1
1
package apns2_test
2
2
3
3
import (
4
+ "crypto/ecdsa"
5
+ "crypto/elliptic"
6
+ "crypto/rand"
4
7
"crypto/tls"
5
8
"fmt"
6
9
"io/ioutil"
@@ -15,6 +18,7 @@ import (
15
18
16
19
apns "github.com/sideshow/apns2"
17
20
"github.com/sideshow/apns2/certificate"
21
+ "github.com/sideshow/apns2/token"
18
22
"github.com/stretchr/testify/assert"
19
23
)
20
24
@@ -27,6 +31,12 @@ func mockNotification() *apns.Notification {
27
31
return n
28
32
}
29
33
34
+ func mockToken () * token.Token {
35
+ pubkeyCurve := elliptic .P256 ()
36
+ authKey , _ := ecdsa .GenerateKey (pubkeyCurve , rand .Reader )
37
+ return & token.Token {AuthKey : authKey }
38
+ }
39
+
30
40
func mockCert () tls.Certificate {
31
41
return tls.Certificate {}
32
42
}
@@ -42,16 +52,31 @@ func TestClientDefaultHost(t *testing.T) {
42
52
assert .Equal (t , "https://api.development.push.apple.com" , client .Host )
43
53
}
44
54
55
+ func TestTokenDefaultHost (t * testing.T ) {
56
+ client := apns .NewTokenClient (mockToken ()).Development ()
57
+ assert .Equal (t , "https://api.development.push.apple.com" , client .Host )
58
+ }
59
+
45
60
func TestClientDevelopmentHost (t * testing.T ) {
46
61
client := apns .NewClient (mockCert ()).Development ()
47
62
assert .Equal (t , "https://api.development.push.apple.com" , client .Host )
48
63
}
49
64
65
+ func TestTokenClientDevelopmentHost (t * testing.T ) {
66
+ client := apns .NewTokenClient (mockToken ()).Development ()
67
+ assert .Equal (t , "https://api.development.push.apple.com" , client .Host )
68
+ }
69
+
50
70
func TestClientProductionHost (t * testing.T ) {
51
71
client := apns .NewClient (mockCert ()).Production ()
52
72
assert .Equal (t , "https://api.push.apple.com" , client .Host )
53
73
}
54
74
75
+ func TestTokenClientProductionHost (t * testing.T ) {
76
+ client := apns .NewTokenClient (mockToken ()).Production ()
77
+ assert .Equal (t , "https://api.push.apple.com" , client .Host )
78
+ }
79
+
55
80
func TestClientBadUrlError (t * testing.T ) {
56
81
n := mockNotification ()
57
82
res , err := mockClient ("badurl://badurl.com" ).Push (n )
@@ -150,6 +175,21 @@ func TestHeaders(t *testing.T) {
150
175
assert .NoError (t , err )
151
176
}
152
177
178
+ func TestAuthorizationHeader (t * testing.T ) {
179
+ n := mockNotification ()
180
+ token := mockToken ()
181
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
182
+ assert .Equal (t , "application/json; charset=utf-8" , r .Header .Get ("Content-Type" ))
183
+ assert .Equal (t , fmt .Sprintf ("bearer %v" , token .Bearer ), r .Header .Get ("authorization" ))
184
+ }))
185
+ defer server .Close ()
186
+
187
+ client := mockClient (server .URL )
188
+ client .Token = token
189
+ _ , err := client .Push (n )
190
+ assert .NoError (t , err )
191
+ }
192
+
153
193
func TestPayload (t * testing.T ) {
154
194
n := mockNotification ()
155
195
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
0 commit comments