forked from appleboy/go-fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
139 lines (126 loc) · 2.86 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
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
package fcm
import (
"encoding/json"
"reflect"
"testing"
)
func TestUnmarshal(t *testing.T) {
t.Run("unmarshal=success", func(t *testing.T) {
data := []byte(`{
"multicast_id":10,
"success": 0,
"failure":1,
"canonical_ids":10,
"results": [{
"message_id":"q1w2e3r4",
"registration_id": "t5y6u7i8o9",
"error": "NotRegistered"
}]
}`)
var response Response
err := json.Unmarshal(data, &response)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := Response{
MulticastID: 10,
Success: 0,
Failure: 1,
CanonicalIDs: 10,
Results: []Result{{
MessageID: "q1w2e3r4",
RegistrationID: "t5y6u7i8o9",
Error: ErrNotRegistered,
}},
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("expected: %v\ngot: %v", expected, response)
}
if !response.Results[0].Unregistered() {
t.Fatalf("expected: true\ngot: %t", response.Results[0].Unregistered())
}
})
t.Run("Device Group HTTP Response", func(t *testing.T) {
data := []byte(`{
"success":1,
"failure":2,
"failed_registration_ids":[
"regId1",
"regId2"
]
}`)
var response Response
err := json.Unmarshal(data, &response)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := Response{
Success: 1,
Failure: 2,
FailedRegistrationIDs: []string{
"regId1",
"regId2",
},
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("expected: %v\ngot: %v", expected, response)
}
})
t.Run("Topic HTTP response", func(t *testing.T) {
data := []byte(`{
"message_id": 1023456,
"error": "TopicsMessageRateExceeded"
}`)
var response Response
err := json.Unmarshal(data, &response)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := Response{
MessageID: int64(1023456),
Error: errMap["TopicsMessageRateExceeded"],
}
if !reflect.DeepEqual(response, expected) {
t.Fatalf("expected: %v\ngot: %v", expected, response)
}
})
t.Run("unmarshal=success_timeout", func(t *testing.T) {
data := []byte(`{
"multicast_id":10,
"success": 0,
"failure":1,
"canonical_ids":10,
"results": [{
"message_id":"q1w2e3r4",
"registration_id": "t5y6u7i8o9",
"error": "Unavailable"
}]
}`)
var response Response
err := json.Unmarshal(data, &response)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if response.Results[0].Unregistered() {
t.Fatalf("expected: false\ngot: %t", response.Results[0].Unregistered())
}
})
t.Run("unmarshal=failed", func(t *testing.T) {
data := []byte(`{
"multicast_id":10,
"success": 0,
"failure":1,
"canonical_ids":10,
"results": [{
"message_id":["q1w2e3r4"],
"registration_id": "t5y6u7i8o9",
"error": "NotRegistered"
}]
}`)
var response Response
err := json.Unmarshal(data, &response)
if err == nil {
t.Fatal("expected error but got nil")
}
})
}