This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
message_test.go
112 lines (89 loc) · 2.75 KB
/
message_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
package incus
import "encoding/json"
import "testing"
import "github.com/alexjlockwood/gcm"
import apns "github.com/anachronistic/apns"
import mock "github.com/stretchr/testify/mock"
func TestAPNS(t *testing.T) {
msg := new(CommandMsg)
json.Unmarshal([]byte(`{
"command": {
"command": "push",
"push_type": "ios",
"build": "store",
"device_token": "123456"
},
"message": {
"event": "foobaz",
"data": {
"message_text": "foobar"
},
"time": 1234
}
}`), &msg)
mockAPNS := &apns.MockClient{}
mockAPNS.On("Send", mock.AnythingOfType("*apns.PushNotification")).Return(&apns.PushNotificationResponse{
Success: true,
AppleResponse: "Hello from California!",
Error: nil,
})
server := &Server{
Stats: &DiscardStats{},
apnsProvider: func(build string) apns.APNSClient { return mockAPNS },
}
msg.FromRedis(server)
mockAPNS.AssertCalled(t, "Send", mock.AnythingOfType("*apns.PushNotification"))
pushNotification := mockAPNS.Calls[0].Arguments[0].(*apns.PushNotification)
if pushNotification.DeviceToken != "123456" {
t.Fatalf("Expected device token to be 123456, instead %s in %+v", pushNotification.DeviceToken, pushNotification)
}
apsPayload := pushNotification.Get("aps").(*apns.Payload)
if apsPayload.Alert != "foobar" {
t.Fatalf("Expected push alert to be \"foobar\", instead %s in %+v", apsPayload.Alert, pushNotification)
}
}
type MockGCMClient struct {
mock.Mock
}
func (m *MockGCMClient) Send(msg *gcm.Message, retries int) (resp *gcm.Response, err error) {
args := m.Called(msg, retries)
return args.Get(0).(*gcm.Response), args.Error(1)
}
func TestGCM(t *testing.T) {
msg := new(CommandMsg)
json.Unmarshal([]byte(`{
"command": {
"command": "push",
"push_type": "android",
"registration_ids": "123456,654321"
},
"message": {
"event": "foobaz",
"data": {
"foobar": "foo"
},
"time": 1234
}
}`), &msg)
mockGCM := &MockGCMClient{}
mockGCM.On("Send", mock.AnythingOfType("*gcm.Message"), mock.AnythingOfType("int")).Return(&gcm.Response{
MulticastID: 1234,
Success: 2,
Failure: 0,
CanonicalIDs: 0,
Results: []gcm.Result{
gcm.Result{MessageID: "abcd", RegistrationID: "123456", Error: ""},
gcm.Result{MessageID: "bcde", RegistrationID: "654321", Error: ""},
},
}, nil)
server := &Server{
Stats: &DiscardStats{},
gcmProvider: func() GCMClient { return mockGCM },
}
msg.FromRedis(server)
mockGCM.AssertCalled(t, "Send", mock.AnythingOfType("*gcm.Message"), mock.AnythingOfType("int"))
message := mockGCM.Calls[0].Arguments[0].(*gcm.Message)
if message.RegistrationIDs == nil || len(message.RegistrationIDs) != 2 {
t.Fatalf("Expected there to be two registration IDs, instead %+v in %+v", message.RegistrationIDs, message)
}
}