From b1e286b448e7788d47cea53e2208ae1082aa9e52 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Wed, 23 Oct 2024 21:17:15 +1300 Subject: [PATCH] [CH-5425] Fix apns-expiration request header logic (#9) (#237) Co-authored-by: Zijin Li --- client.go | 2 +- client_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index d36da1b..832b421 100644 --- a/client.go +++ b/client.go @@ -227,7 +227,7 @@ func setHeaders(r *http.Request, n *Notification) { if n.Priority > 0 { r.Header.Set("apns-priority", strconv.Itoa(n.Priority)) } - if !n.Expiration.IsZero() { + if n.Expiration.After(time.Unix(0, 0)) { r.Header.Set("apns-expiration", strconv.FormatInt(n.Expiration.Unix(), 10)) } if n.PushType != "" { diff --git a/client_test.go b/client_test.go index cfbdb6c..2f2045c 100644 --- a/client_test.go +++ b/client_test.go @@ -245,6 +245,25 @@ func TestHeaders(t *testing.T) { assert.NoError(t, err) } +func TestExpirationHeader(t *testing.T) { + n := mockNotification() + n.ApnsID = "84DB694F-464F-49BD-960A-D6DB028335C9" + n.CollapseID = "game1.start.identifier" + n.Topic = "com.testapp" + n.Priority = 10 + n.Expiration = time.Unix(0, 0) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, n.ApnsID, r.Header.Get("apns-id")) + assert.Equal(t, n.CollapseID, r.Header.Get("apns-collapse-id")) + assert.Equal(t, "10", r.Header.Get("apns-priority")) + assert.Equal(t, n.Topic, r.Header.Get("apns-topic")) + assert.Equal(t, "", r.Header.Get("apns-expiration")) + })) + defer server.Close() + _, err := mockClient(server.URL).Push(n) + assert.NoError(t, err) +} + func TestPushTypeAlertHeader(t *testing.T) { n := mockNotification() n.PushType = apns.PushTypeAlert