Skip to content

Commit

Permalink
Make Payload an interface for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshow committed Feb 23, 2016
1 parent 8ce5dae commit e8c8af4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
14 changes: 9 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ func (c *Client) Production() *Client {
// indicating whether the notification was accepted or rejected by the APNs
// gateway, or an error if something goes wrong.
func (c *Client) Push(n *Notification) (*Response, error) {
jsonBytes, err := json.Marshal(n.Payload)
if err != nil {
return nil, err
}

url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(n.Payload))
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
setHeaders(req, n)
httpRes, httpErr := c.HTTPClient.Do(req)

if httpErr != nil {
return nil, httpErr
httpRes, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer httpRes.Body.Close()

Expand Down
11 changes: 10 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apns2_test

import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -124,16 +125,24 @@ func TestHeaders(t *testing.T) {

func TestPayload(t *testing.T) {
n := mockNotification()
bytes, _ := json.Marshal(n.Payload)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, n.Payload, body)
assert.Equal(t, bytes, body)
}))
defer server.Close()
_, err := mockClient(server.URL).Push(n)
assert.NoError(t, err)
}

func TestBadPayload(t *testing.T) {
n := mockNotification()
n.Payload = func() {}
_, err := mockClient("").Push(n)
assert.Error(t, err)
}

func Test200SuccessResponse(t *testing.T) {
n := mockNotification()
var apnsID = "02ABC856-EF8D-4E49-8F15-7B8A61D978D6"
Expand Down
2 changes: 1 addition & 1 deletion notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ type Notification struct {
// A byte array containing the JSON-encoded payload of this push notification.
// Refer to "The Remote Notification Payload" section in the Apple Local and
// Remote Notification Programming Guide for more info.
Payload []byte
Payload interface{}
}

0 comments on commit e8c8af4

Please sign in to comment.