Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from RobotsAndPencils/headers
Browse files Browse the repository at this point in the history
add tests and documentation for headers
  • Loading branch information
curtisallen committed Jan 8, 2016
2 parents 10aeb8b + 7bfa794 commit e877878
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ func main() {
}
}
```
#### Headers

You can specify an ID, expiration, priority, and other parameters via the Headers struct.

```go
headers := &push.Headers{
ID: "922D9F1F-B82E-B337-EDC9-DB4FC8527676",
Expiration: time.Now().Add(time.Hour),
LowPriority: true,
}

id, err = service.Push(deviceToken, headers, p)
```

If no ID is specified APNS will generate and return a unique ID. When an expiration is specified, APNS will store and retry sending the notification until that time, otherwise APNS will not store or retry the notification. LowPriority should be used when sending a ContentAvailable payload.

#### Custom values

Expand All @@ -107,7 +122,7 @@ The Push method will use json.Marshal to serialize whatever you send it.
Use json.Marshal to serialize your payload once and then send it to multiple device tokens with PushBytes.

```go
b, err := json.Marshal(payload)
b, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
Expand Down
53 changes: 53 additions & 0 deletions push/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package push

import (
"net/http"
"testing"
"time"
)

func TestHeaders(t *testing.T) {
headers := Headers{
ID: "uuid",
Expiration: time.Unix(12622780800, 0),
LowPriority: true,
Topic: "bundle-id",
}

reqHeader := http.Header{}
headers.set(reqHeader)

testHeader(t, reqHeader, "apns-id", "uuid")
testHeader(t, reqHeader, "apns-expiration", "12622780800")
testHeader(t, reqHeader, "apns-priority", "5")
testHeader(t, reqHeader, "apns-topic", "bundle-id")
}

func TestNilHeader(t *testing.T) {
var headers *Headers
reqHeader := http.Header{}
headers.set(reqHeader)

testHeader(t, reqHeader, "apns-id", "")
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
}

func TestEmptyHeaders(t *testing.T) {
headers := Headers{}
reqHeader := http.Header{}
headers.set(reqHeader)

testHeader(t, reqHeader, "apns-id", "")
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
}

func testHeader(t *testing.T, reqHeader http.Header, key, expected string) {
actual := reqHeader.Get(key)
if actual != expected {
t.Errorf("Expected %s %q, got %q.", key, expected, actual)
}
}
14 changes: 7 additions & 7 deletions push/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *Service) PushBytes(deviceToken string, headers *Headers, payload []byte
return "", err
}
req.Header.Set("Content-Type", "application/json")
headers.set(req)
headers.set(req.Header)

resp, err := s.Client.Do(req)
if err != nil {
Expand Down Expand Up @@ -202,26 +202,26 @@ func (s *Service) PushBytes(deviceToken string, headers *Headers, payload []byte
return "", ErrUnknown
}

// set headers on an HTTP request
func (h *Headers) set(req *http.Request) {
// set headers for an HTTP request
func (h *Headers) set(reqHeader http.Header) {
// headers are optional
if h == nil {
return
}

if h.ID != "" {
req.Header.Set("apns-id", h.ID)
reqHeader.Set("apns-id", h.ID)
} // when ommitted, Apple will generate a UUID for you

if !h.Expiration.IsZero() {
req.Header.Set("apns-expiration", strconv.FormatInt(h.Expiration.Unix(), 10))
reqHeader.Set("apns-expiration", strconv.FormatInt(h.Expiration.Unix(), 10))
}

if h.LowPriority {
req.Header.Set("apns-priority", "5")
reqHeader.Set("apns-priority", "5")
} // when ommitted, the default priority is 10

if h.Topic != "" {
req.Header.Set("apns-topic", h.Topic)
reqHeader.Set("apns-topic", h.Topic)
}
}

0 comments on commit e877878

Please sign in to comment.