Skip to content

Commit c438199

Browse files
Export HTTP client interface (SherClockHolmes#19)
1 parent 2a62cae commit c438199

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

webpush.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ var saltFunc = func() ([]byte, error) {
3434
return salt, nil
3535
}
3636

37-
// httpClient is an interface for sending the notification HTTP request / testing
38-
type httpClient interface {
37+
// HTTPClient is an interface for sending the notification HTTP request / testing
38+
type HTTPClient interface {
3939
Do(*http.Request) (*http.Response, error)
4040
}
4141

4242
// Options are config and extra params needed to send a notification
4343
type Options struct {
44-
httpClient httpClient
45-
46-
Subscriber string // Sub in VAPID JWT token
47-
Topic string // Set the Topic header to collapse a pending messages (Optional)
48-
TTL int // Set the TTL on the endpoint POST request
49-
Urgency Urgency // Set the Urgency header to change a message priority (Optional)
50-
VAPIDPublicKey string // VAPID public key, passed in VAPID Authorization header
51-
VAPIDPrivateKey string // VAPID private key, used to sign VAPID JWT token
44+
HTTPClient HTTPClient // Will replace with *http.Client by default if not included
45+
Subscriber string // Sub in VAPID JWT token
46+
Topic string // Set the Topic header to collapse a pending messages (Optional)
47+
TTL int // Set the TTL on the endpoint POST request
48+
Urgency Urgency // Set the Urgency header to change a message priority (Optional)
49+
VAPIDPublicKey string // VAPID public key, passed in VAPID Authorization header
50+
VAPIDPrivateKey string // VAPID private key, used to sign VAPID JWT token
5251
}
5352

5453
// Keys are the base64 encoded values from PushSubscription.getKey()
@@ -201,11 +200,14 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
201200
req.Header.Set("Authorization", vapidAuthHeader)
202201

203202
// Send the request
204-
if options.httpClient == nil {
205-
options.httpClient = &http.Client{}
203+
var client HTTPClient
204+
if options.HTTPClient != nil {
205+
client = options.HTTPClient
206+
} else {
207+
client = &http.Client{}
206208
}
207209

208-
return options.httpClient.Do(req)
210+
return client.Do(req)
209211
}
210212

211213
// decodeSubscriptionKey decodes a base64 subscription key.

webpush_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func getStandardEncodedTestSubscription() *Subscription {
3333

3434
func TestSendNotificationToURLEncodedSubscription(t *testing.T) {
3535
resp, err := SendNotification([]byte("Test"), getURLEncodedTestSubscription(), &Options{
36-
httpClient: &testHTTPClient{},
36+
HTTPClient: &testHTTPClient{},
3737
Subscriber: "mailto:<EMAIL@EXAMPLE.COM>",
3838
Topic: "test_topic",
3939
TTL: 0,
@@ -56,7 +56,7 @@ func TestSendNotificationToURLEncodedSubscription(t *testing.T) {
5656

5757
func TestSendNotificationToStandardEncodedSubscription(t *testing.T) {
5858
resp, err := SendNotification([]byte("Test"), getStandardEncodedTestSubscription(), &Options{
59-
httpClient: &testHTTPClient{},
59+
HTTPClient: &testHTTPClient{},
6060
Subscriber: "mailto:<EMAIL@EXAMPLE.COM>",
6161
Topic: "test_topic",
6262
TTL: 0,

0 commit comments

Comments
 (0)