-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
107 lines (94 loc) · 3.17 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
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
package hmspush
import (
"fmt"
"testing"
"golang.org/x/net/context"
)
const (
testClientID = "your-client-id"
testClientSecret = "your-client-secret"
testDeviceToken = "you-device-token"
)
var testDeviceTokenList = []string{
"you-device-token",
"you-device-token",
}
func init() {
Init("clientID", "clientSecret")
}
var defaultClient *HuaweiPushClient
func Init(clientID, clientSecret string) {
defaultClient = &HuaweiPushClient{
clientID: clientID,
clientSecret: clientSecret,
}
}
func Test_RequestAccess(t *testing.T) {
token, err := RequestAccess(testClientID, testClientSecret)
if err != nil {
t.Errorf("err=%v\n", err)
return
}
if token.Error != 0 {
t.Errorf("token=%+v\n", token)
return
}
fmt.Printf("Test_RequestAccess - Got Token: %+v\n", token)
}
func Test_SingleSend(t *testing.T) {
testClient := NewClient(testClientID, testClientSecret)
message := "This is a silent notification to a single device."
notif := NewSingleNotification(testDeviceToken, message).SetTimeToLive(5)
result, err := testClient.SendPush(context.Background(), notif)
if err != nil {
t.Errorf("err=%v\n\n", err)
return
}
fmt.Printf("Test_SingleSend - result=%+v\n", result)
// Query Msg Result
// But it seems that the query result is aways empty, this may a bug of Huawei
qresult, qerr := defaultClient.QueryMsgResult(context.TODO(), result.RequestID, testDeviceToken)
if qerr != nil {
t.Errorf("TestHuaweiPushClient_QueryResult err=%v\n\n", qerr)
return
}
fmt.Printf("TestHuaweiPushClient_QueryResult - qresult=%+v\n\n", qresult)
t.Logf("qresult=%#v\n", qresult)
}
func Test_BatchSend(t *testing.T) {
testClient := NewClient(testClientID, testClientSecret)
message := "This is a silent notification to a list of devices."
notif := NewBatchNotification(testDeviceTokenList, message).SetTimeToLive(5)
result, err := testClient.SendPush(context.Background(), notif)
if err != nil {
t.Errorf("err=%v\n\n", err)
return
}
fmt.Printf("Test_BatchSend - result=%+v\n\n", result)
}
func Test_PsSingleSend(t *testing.T) {
testClient := NewClient(testClientID, testClientSecret)
message := NewAndroidMessage("notificationTitle", "This is a PS notification to a single device.")
// PsSingleNotification uses a old version of time format, with a minutes-precision
// So you can only set a TimeToLive greater than 60 (sec)
notif := NewPsSingleNotification(testDeviceToken, message).SetTimeToLive(70)
result, err := testClient.SendPush(context.Background(), notif)
if err != nil {
t.Errorf("err=%v\n\n", err)
return
}
fmt.Printf("Test_PsSingleSend - result=%+v\n\n", result)
}
func Test_PsBatchSend(t *testing.T) {
testClient := NewClient(testClientID, testClientSecret)
message := NewAndroidMessage("notificationTitle", "This is a PS notification to a list of devices.")
// PsBatchNotification uses a old version of time format, with a minutes-precision
// So you can only set a TimeToLive greater than 60 (sec)
notif := NewPsBatchNotification(testDeviceTokenList, message).SetTimeToLive(70)
result, err := testClient.SendPush(context.Background(), notif)
if err != nil {
t.Errorf("err=%v\n\n", err)
return
}
fmt.Printf("Test_PsBatchSend - result=%+v\n\n", result)
}