-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_helper_test.go
62 lines (50 loc) · 1.33 KB
/
send_helper_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
package fcm
import (
"context"
"net/http/httptest"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/messaging"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)
type MockTokenSource struct {
AccessToken string
}
func (ts *MockTokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{AccessToken: ts.AccessToken}, nil
}
const testMessageID = "projects/test-project/messages/msg_id"
func defaultFirebaseConfig() *firebase.Config {
return &firebase.Config{
ProjectID: "test-project",
}
}
func defaultFirebaseOption() []option.ClientOption {
return []option.ClientOption{
option.WithTokenSource(&MockTokenSource{AccessToken: "test-token"}),
}
}
func newMessagingClient(ctx context.Context, ts *httptest.Server, opts ...option.ClientOption) (c *messaging.Client, err error) {
var url string
if ts != nil {
url = ts.URL
}
app, err := firebase.NewApp(
ctx,
defaultFirebaseConfig(),
append(defaultFirebaseOption(), append(opts, option.WithEndpoint(url))...)...,
)
if err != nil {
return
}
c, err = app.Messaging(ctx)
return
}
func newClient(ctx context.Context, ts *httptest.Server, opts ...option.ClientOption) (c *client, err error) {
msgClient, err := newMessagingClient(ctx, ts, opts...)
if err != nil {
return
}
c = (&client{Client: msgClient}).Assign(new(Option).Init())
return
}