Skip to content

Commit

Permalink
Document and test
Browse files Browse the repository at this point in the history
  • Loading branch information
bergusman committed Aug 17, 2021
1 parent d001d58 commit 8f8a784
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ var (
ErrClientTokenNil = errors.New("client: token is nil")
)

// Token-based client for sending remote notifications.
type Client struct {
Token *Token
HTTPClient *http.Client
}

// NewClient creates client with token and http.Client client,
// pass nil for client to use http.DefaultClient.
func NewClient(token *Token, httpClient *http.Client) *Client {
return &Client{
Token: token,
HTTPClient: httpClient,
}
}

// Push sends remote notification.
func (c *Client) Push(n *Notification) (*Response, error) {
return c.PushWithContext(context.Background(), n)
}

// Push sends remote notification with context.
func (c *Client) PushWithContext(ctx context.Context, n *Notification) (*Response, error) {
if n == nil {
return nil, ErrClientNotificationNil
Expand Down
25 changes: 25 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package apns

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -101,4 +104,26 @@ func TestClientPushErrors(t *testing.T) {
} else {
t.Error("err must be not nil")
}

key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
t.Fatal(err)
}
client.Token = &Token{
Key: key,
}
_, err = client.Push(n)
if err != nil {
if err != ErrJWTKeyNotECDSAP256 {
t.Errorf("got: %v, want: ErrJWTKeyNotECDSAP256", err)
}
} else {
t.Error("err must be not nil")
}

n.Host = ":::::"
_, err = client.Push(n)
if err == nil {
t.Error("err must be not nil")
}
}
99 changes: 98 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package apns_test

import (
"encoding/json"
"fmt"
"log"
"os"
"time"

"github.com/bergusman/apns-go"
Expand Down Expand Up @@ -48,11 +50,106 @@ func Example() {
}

func ExampleNotification() {
key, err := apns.AuthKeyFromFile("testdata/AuthKey_5MDQ4KLTY7.p8")
if err != nil {
log.Fatal(err)
}
token := apns.NewToken(key, "5MDQ4KLTY7", "SUPERTEEM1")

n := &apns.Notification{
DeviceToken: "7c968c83f6fd6de5843c309150ed1a706bc64fcdc42310f66054c0271e67219e",
Topic: "com.example.app",
ID: "EC1BF194-B3B2-424A-89A9-5A918A6E6B5D",
Host: apns.HostDevelopment,

Payload: apns.BuildPayload(&apns.APS{
Alert: apns.Alert{
Title: "Hello",
},
Sound: apns.SoundDefault,
}, nil),
}

req, err := n.BuildRequest()
if err != nil {
log.Fatal(err)
}

token.SetAuthorization(req.Header)
// res, err := http.DefaultClient.Do(req)
}

func ExampleNotification_payload() {
enc := json.NewEncoder(os.Stdout)

n := &apns.Notification{
Payload: `{"aps":{"alert":"Hello"}}`,
}
err := enc.Encode(n)
if err != nil {
log.Fatal(err)
}

n = &apns.Notification{
Payload: []byte(`{"aps":{"alert":"Hello"}}`),
}
err = enc.Encode(n)
if err != nil {
log.Fatal(err)
}

n = &apns.Notification{
Payload: apns.BuildPayload(&apns.APS{
Alert: "Hello",
}, nil),
}
err = enc.Encode(n)
if err != nil {
log.Fatal(err)
}

// Output:
// {"aps":{"alert":"Hello"}}
// {"aps":{"alert":"Hello"}}
// {"aps":{"alert":"Hello"}}
}

func ExampleBuildPayload() {
enc := json.NewEncoder(os.Stdout)

payload := apns.BuildPayload(&apns.APS{
Alert: "Hello",
}, nil)
err := enc.Encode(payload)
if err != nil {
log.Fatal(err)
}

payload = apns.BuildPayload(&apns.APS{
Alert: "Hello",
}, map[string]interface{}{
"type": "hello",
})
err = enc.Encode(payload)
if err != nil {
log.Fatal(err)
}

payload = apns.BuildPayload(nil, map[string]interface{}{
"aps": map[string]interface{}{
"alert": "Hello",
},
"type": "hello",
})
err = enc.Encode(payload)
if err != nil {
log.Fatal(err)
}

// Output:
// {"aps":{"alert":"Hello"}}
// {"aps":{"alert":"Hello"},"type":"hello"}
// {"aps":{"alert":"Hello"},"type":"hello"}
}

func ExampleGenerateBearer() {
Expand All @@ -75,6 +172,6 @@ func ExampleGenerateBearer() {
log.Fatal(err)
}

req.Header.Set("authorization", "bearer "+token)
apns.SetBearer(req.Header, token)
// res, err := http.DefaultClient.Do(req)
}
1 change: 1 addition & 0 deletions notification.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// APNS package for sending remote notifications for iOS by token-based connection.
package apns

import (
Expand Down

0 comments on commit 8f8a784

Please sign in to comment.