Skip to content

Commit

Permalink
Export HTTP client interface (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
SherClockHolmes authored Feb 20, 2019
1 parent 2a62cae commit c438199
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions webpush.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions webpush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:<EMAIL@EXAMPLE.COM>",
Topic: "test_topic",
TTL: 0,
Expand All @@ -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:<EMAIL@EXAMPLE.COM>",
Topic: "test_topic",
TTL: 0,
Expand Down

0 comments on commit c438199

Please sign in to comment.