-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
129 lines (116 loc) · 3.38 KB
/
client_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
package apns
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestClientPush(t *testing.T) {
key, err := AuthKeyFromFile("testdata/AuthKey_5MDQ4KLTY7.p8")
if err != nil {
t.Fatal(err)
}
token := NewToken(key, "5JZB9P77A7", "SUPERTEEM1")
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
payload, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if r.Method != http.MethodPost {
t.Errorf(":method: %v; want: %v", r.Method, http.MethodPost)
}
if r.URL.Path != "/3/device/7c968c83f6fd6de5843c309150ed1a706bc64fcdc42310f66054c0271e67219e" {
t.Errorf(":path: %v, want: %v", r.URL.Path, "/3/device/7c968c83f6fd6de5843c309150ed1a706bc64fcdc42310f66054c0271e67219e")
}
if r.Header.Get("authorization") != "bearer "+token.Bearer {
t.Errorf("authorization: %v; want: %v", r.Header.Get("authorization"), "bearer "+token.Bearer)
}
if r.Header.Get("apns-id") != "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D" {
t.Errorf("apns-id: %v; want: %v", r.Header.Get("apns-id"), "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D")
}
if r.Header.Get("apns-push-type") != "alert" {
t.Errorf("apns-push-type: %v; want: %v", r.Header.Get("apns-push-type"), "alert")
}
if string(payload) != `{"aps":{"alert":{"title":"Hello"}}}` {
t.Errorf("payload: %v; want: %v", string(payload), `{"aps":{"alert":{"title":"Hello"}}}`)
}
w.Header().Set("apns-id", "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D")
w.WriteHeader(410)
w.Write([]byte(`{"reason":"Unregistered","timestamp":1629000000}`))
}))
ts.EnableHTTP2 = true
ts.StartTLS()
defer ts.Close()
client := NewClient(token, ts.Client())
n := &Notification{
DeviceToken: "7c968c83f6fd6de5843c309150ed1a706bc64fcdc42310f66054c0271e67219e",
Host: ts.URL,
ID: "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D",
PushType: PushTypeAlert,
Payload: BuildPayload(&APS{
Alert: Alert{
Title: "Hello",
},
}, nil),
}
res, err := client.Push(n)
if err != nil {
t.Fatal(err)
}
if res.ID != "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D" {
t.Errorf("res.ID: %v; want: %v", res.ID, "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D")
}
if res.Status != Status410 {
t.Errorf("res.Status: %v; want: %v", res.Status, Status410)
}
if res.Reason != ReasonUnregistered {
t.Errorf("res.Reason: %v; want: %v", res.Reason, ReasonUnregistered)
}
if res.Timestamp != 1629000000 {
t.Errorf("res.Timestamp: %v; want: %v", res.Timestamp, 1629000000)
}
}
func TestClientPushErrors(t *testing.T) {
client := NewClient(nil, nil)
_, err := client.Push(nil)
if err != nil {
if err != ErrClientNotificationNil {
t.Errorf("got: %v, want: ErrClientNotificationNil", err)
}
} else {
t.Error("err must be not nil")
}
n := &Notification{}
_, err = client.Push(n)
if err != nil {
if err != ErrClientTokenNil {
t.Errorf("got: %v, want: ErrClientTokenNil", err)
}
} else {
t.Error("err must be not nil")
}
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
t.Fatal(err)
}
client.Token = &Token{
Key: key,
}
_, err = client.Push(n)
if err != nil {
if err != ErrJWTKeyNotECDSAP256 {
t.Errorf("got: %v, want: ErrJWTKeyNotECDSAP256", err)
}
} else {
t.Error("err must be not nil")
}
n.Host = ":::::"
_, err = client.Push(n)
if err == nil {
t.Error("err must be not nil")
}
}