diff --git a/webpush.go b/webpush.go index abbf868..7bd7e7a 100644 --- a/webpush.go +++ b/webpush.go @@ -34,21 +34,20 @@ var saltFunc = func() ([]byte, error) { return salt, nil } -// httpClient is an interface for sending the notification HTTP request / testing -type httpClient interface { +// HTTPClient is an interface for sending the notification HTTP request / testing +type HTTPClient interface { Do(*http.Request) (*http.Response, error) } // Options are config and extra params needed to send a notification type Options struct { - httpClient httpClient - - Subscriber string // Sub in VAPID JWT token - Topic string // Set the Topic header to collapse a pending messages (Optional) - TTL int // Set the TTL on the endpoint POST request - Urgency Urgency // Set the Urgency header to change a message priority (Optional) - VAPIDPublicKey string // VAPID public key, passed in VAPID Authorization header - VAPIDPrivateKey string // VAPID private key, used to sign VAPID JWT token + HTTPClient HTTPClient // Will replace with *http.Client by default if not included + Subscriber string // Sub in VAPID JWT token + Topic string // Set the Topic header to collapse a pending messages (Optional) + TTL int // Set the TTL on the endpoint POST request + Urgency Urgency // Set the Urgency header to change a message priority (Optional) + VAPIDPublicKey string // VAPID public key, passed in VAPID Authorization header + VAPIDPrivateKey string // VAPID private key, used to sign VAPID JWT token } // Keys are the base64 encoded values from PushSubscription.getKey() @@ -201,11 +200,14 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http. req.Header.Set("Authorization", vapidAuthHeader) // Send the request - if options.httpClient == nil { - options.httpClient = &http.Client{} + var client HTTPClient + if options.HTTPClient != nil { + client = options.HTTPClient + } else { + client = &http.Client{} } - return options.httpClient.Do(req) + return client.Do(req) } // decodeSubscriptionKey decodes a base64 subscription key. diff --git a/webpush_test.go b/webpush_test.go index 5b9cddf..720177c 100644 --- a/webpush_test.go +++ b/webpush_test.go @@ -33,7 +33,7 @@ func getStandardEncodedTestSubscription() *Subscription { func TestSendNotificationToURLEncodedSubscription(t *testing.T) { resp, err := SendNotification([]byte("Test"), getURLEncodedTestSubscription(), &Options{ - httpClient: &testHTTPClient{}, + HTTPClient: &testHTTPClient{}, Subscriber: "mailto:", Topic: "test_topic", TTL: 0, @@ -56,7 +56,7 @@ func TestSendNotificationToURLEncodedSubscription(t *testing.T) { func TestSendNotificationToStandardEncodedSubscription(t *testing.T) { resp, err := SendNotification([]byte("Test"), getStandardEncodedTestSubscription(), &Options{ - httpClient: &testHTTPClient{}, + HTTPClient: &testHTTPClient{}, Subscriber: "mailto:", Topic: "test_topic", TTL: 0,