forked from SherClockHolmes/webpush-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpush_test.go
136 lines (124 loc) · 3.34 KB
/
webpush_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package webpush
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
)
type testHTTPClient struct{}
func (*testHTTPClient) Do(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 201}, nil
}
func getURLEncodedTestSubscription() *Subscription {
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 getStandardEncodedTestSubscription() *Subscription {
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 TestSendNotificationToURLEncodedSubscription(t *testing.T) {
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
resp, err := SendNotification(context.Background(), []byte("Test"), getURLEncodedTestSubscription(), &Options{
HTTPClient: &testHTTPClient{},
RecordSize: 3070,
Subscriber: "<EMAIL@EXAMPLE.COM>",
Topic: "test_topic",
TTL: 0,
Urgency: "low",
VAPIDKeys: vapidKeys,
})
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 201 {
t.Fatalf(
"Incorrect status code, expected=%d, got=%d",
resp.StatusCode,
201,
)
}
}
func TestSendNotificationToStandardEncodedSubscription(t *testing.T) {
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
t.Fatal(err)
}
resp, err := SendNotification(context.Background(), []byte("Test"), getStandardEncodedTestSubscription(), &Options{
HTTPClient: &testHTTPClient{},
Subscriber: "<EMAIL@EXAMPLE.COM>",
Topic: "test_topic",
TTL: 0,
Urgency: "low",
VAPIDKeys: vapidKeys,
})
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 201 {
t.Fatalf(
"Incorreect status code, expected=%d, got=%d",
resp.StatusCode,
201,
)
}
}
func TestSendTooLargeNotification(t *testing.T) {
_, err := SendNotification(context.Background(), []byte(strings.Repeat("Test", int(MaxRecordSize))), getStandardEncodedTestSubscription(), &Options{
HTTPClient: &testHTTPClient{},
Subscriber: "<EMAIL@EXAMPLE.COM>",
Topic: "test_topic",
TTL: 0,
Urgency: "low",
})
if err == nil {
t.Fatalf("Error is nil, expected=%s", ErrRecordSizeTooSmall)
}
}
func BenchmarkWebPush(b *testing.B) {
vapidKeys, err := GenerateVAPIDKeys()
if err != nil {
b.Fatal(err)
}
ctx := context.Background()
message := []byte("@time=2024-12-26T19:36:21.923Z;account=shivaram;msgid=56g9v3b92q6q4wtq43uhyqzegw :shivaram!~u@kca7nfgniet7q.irc PRIVMSG #redacted :[redacted message contents]")
sub := getStandardEncodedTestSubscription()
options := Options{
HTTPClient: &testHTTPClient{},
RecordSize: 2048,
Subscriber: "https://example.com",
TTL: 60 * 60 * 24,
Urgency: UrgencyHigh,
VAPIDKeys: vapidKeys,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := SendNotification(ctx, message, sub, &options); err != nil {
b.Fatal(err)
}
}
}