From 7bfa79488719eac961f3efdceeb3991de8ba6c72 Mon Sep 17 00:00:00 2001 From: Nathan Youngman Date: Fri, 8 Jan 2016 13:35:42 -0700 Subject: [PATCH] add tests and documentation for headers these tests use an unexported method --- README.md | 17 ++++++++++++++- push/header_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ push/service.go | 14 ++++++------ 3 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 push/header_test.go diff --git a/README.md b/README.md index 2768a52..599c710 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) } diff --git a/push/header_test.go b/push/header_test.go new file mode 100644 index 0000000..0a3859a --- /dev/null +++ b/push/header_test.go @@ -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) + } +} diff --git a/push/service.go b/push/service.go index 3fa99a2..0087b1f 100644 --- a/push/service.go +++ b/push/service.go @@ -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 { @@ -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) } }