-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
64 lines (51 loc) · 1.28 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
package apns2_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/sger/go-apns2"
)
func TestPush(t *testing.T) {
deviceToken := "c7800a79efffe8ffc01b280717a936937cb69f8ca307545eb6983c60f12e167a"
payload := apns2.Payload{
Alert: apns2.Alert{
Body: "Hello World"},
}
apnsID := "674EB1D5-7E7C-3DC9-B0F5-32A55E54960E"
handler := http.NewServeMux()
server := httptest.NewServer(handler)
handler.HandleFunc("/3/device/", func(w http.ResponseWriter, r *http.Request) {
expectURL := fmt.Sprintf("/3/device/%s", deviceToken)
if r.URL.String() != expectURL {
t.Errorf("Expected url %v, got %v", expectURL, r.URL)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
b, err := json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(body, b) {
t.Errorf("Expected body %v, got %v", payload, body)
}
w.Header().Set("apns-id", apnsID)
})
client := apns2.Client{
HTTPClient: http.DefaultClient,
Host: server.URL,
}
resp, err := client.SendPush(payload, deviceToken, &apns2.Headers{})
if err != nil {
t.Error(err)
}
remoteApnsID := resp.ApnsID
if remoteApnsID != apnsID {
t.Errorf("Expected apns-id %q, but got %q ", apnsID, remoteApnsID)
}
}