-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
73 lines (58 loc) · 1.54 KB
/
response_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
package fcm
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestResponse_GetInvalidTokens(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != "key=test" {
t.Fatalf("expected: key=test\ngot: %s", req.Header.Get("Authorization"))
}
rw.WriteHeader(http.StatusOK)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprint(rw, `{
"success": 1,
"failure":0,
"results": [{
"message_id":"1y2t4i224uy2b",
"registration_id": "jfey12fugyuy12oijd",
"error": ""
}, {"error": "InvalidToken"}, {"error": "InvalidToken"}]
}`)
}))
defer server.Close()
data := map[string]string{
"body": "Test",
}
// Init client
client := NewClient("test")
client.ApiFCM = server.URL
client.PushMultiple([]string{"token 1"}, data)
invalidTokens := []string{"token 2", "token 3"}
client.AppendRegistrationIds(invalidTokens)
status, err := client.Send()
if err != nil {
t.Errorf("unexpected error %v", err)
}
if status.StatusCode != http.StatusOK {
t.Errorf("expected 200 got %d", status.StatusCode)
}
if status.Success != 1 {
t.Errorf("expected 1 got %d", status.Success)
}
if status.Failure != 0 {
t.Errorf("expected 0 got %d", status.Failure)
}
badTokens := status.GetInvalidTokens()
if len(badTokens) != 2 {
t.Errorf("expected 2, got %d", len(badTokens))
}
for _, val := range badTokens {
if val != "InvalidToken" {
t.Errorf("expected InvalidToken, got %s", val)
}
}
}