-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
99 lines (91 loc) · 2.43 KB
/
example_test.go
File metadata and controls
99 lines (91 loc) · 2.43 KB
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
package webpush
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type exampleHTTPClient struct{}
func (exampleHTTPClient) Do(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 201,
Status: "201 Created",
Body: io.NopCloser(strings.NewReader("")),
}, nil
}
func exampleSubscription() *Subscription {
const subJSON = `{
"endpoint": "https://updates.push.services.mozilla.com/wpush/v2/gAAAAA",
"keys": {
"p256dh": "BNNL5ZaTfK81qhXOx23-wewhigUeFb632jN6LvRWCFH1ubQr77FE_9qV1FuojuRmHP42zmf34rXgW80OvUVDgTk",
"auth": "zqbxT6JKstKSY9JKibZLSQ"
}
}`
sub := new(Subscription)
if err := json.Unmarshal([]byte(subJSON), sub); err != nil {
panic(err)
}
return sub
}
func ExampleGenerateVAPIDKeys() {
keys, err := GenerateVAPIDKeys()
if err != nil {
panic(err)
}
fmt.Println(len(keys.PublicKeyString()) == 87)
// Output: true
}
func ExampleClient_Send() {
keys, err := GenerateVAPIDKeys()
if err != nil {
panic(err)
}
client := NewClient(Config{HTTPClient: exampleHTTPClient{}})
result, err := client.Send(context.Background(), []byte("Hello from Go!"), exampleSubscription(), SendOptions{
Subject: "user@example.com",
VAPIDKeys: keys,
TTL: 60,
})
if err != nil {
panic(err)
}
defer result.Response.Body.Close()
fmt.Println(result.StatusCode, result.RecordCount, result.NoPayload)
// Output: 201 1 false
}
func ExampleClient_Send_noPayload() {
keys, err := GenerateVAPIDKeys()
if err != nil {
panic(err)
}
client := NewClient(Config{HTTPClient: exampleHTTPClient{}})
result, err := client.Send(context.Background(), nil, exampleSubscription(), SendOptions{
Subject: "user@example.com",
VAPIDKeys: keys,
RequestReceipt: true,
ReceiptSubscription: "https://app.example/receipts",
})
if err != nil {
panic(err)
}
defer result.Response.Body.Close()
fmt.Println(result.StatusCode, result.NoPayload)
// Output: 201 true
}
func ExampleClient_SendBatch() {
keys, err := GenerateVAPIDKeys()
if err != nil {
panic(err)
}
client := NewClient(Config{HTTPClient: exampleHTTPClient{}})
subs := []*Subscription{exampleSubscription(), exampleSubscription()}
attempts := client.SendBatch(context.Background(), []byte("Hello from Go!"), subs, SendOptions{
Subject: "user@example.com",
VAPIDKeys: keys,
TTL: 60,
})
fmt.Println(len(attempts), attempts[0].Err == nil, attempts[1].Err == nil)
// Output: 2 true true
}