-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathresponse_test.go
91 lines (82 loc) · 1.87 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
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("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")
}
})
}