From 1362568003d705138c067c5712fecd849ad26325 Mon Sep 17 00:00:00 2001 From: mtzfederico Date: Wed, 3 Aug 2022 20:05:07 +0200 Subject: [PATCH 1/2] Adds function to use alternative port --- client.go | 7 +++++++ client_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/client.go b/client.go index cd98dd42..92b47b81 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import ( "context" "crypto/tls" "encoding/json" + "fmt" "io" "net" "net/http" @@ -145,6 +146,12 @@ func (c *Client) Production() *Client { return c } +// UseAlternativePort sets the Client to use APNs alternative endpoint port (2197). +func (c *Client) UseAlternativePort() *Client { + c.Host = fmt.Sprintf("%s:2197", c.Host) + return c +} + // Push sends a Notification to the APNs gateway. If the underlying http.Client // is not currently connected, this method will attempt to reconnect // transparently before sending the notification. It will return a Response diff --git a/client_test.go b/client_test.go index 83061ddb..e0c87cb7 100644 --- a/client_test.go +++ b/client_test.go @@ -63,31 +63,61 @@ func TestClientDefaultHost(t *testing.T) { assert.Equal(t, "https://api.sandbox.push.apple.com", client.Host) } +func TestClientDefaultHostWithAlternativePort(t *testing.T) { + client := apns.NewClient(mockCert()).UseAlternativePort() + assert.Equal(t, "https://api.sandbox.push.apple.com:2197", client.Host) +} + func TestTokenDefaultHost(t *testing.T) { client := apns.NewTokenClient(mockToken()).Development() assert.Equal(t, "https://api.sandbox.push.apple.com", client.Host) } +func TestTokenDefaultHostWithAlternativePort(t *testing.T) { + client := apns.NewTokenClient(mockToken()).Development().UseAlternativePort() + assert.Equal(t, "https://api.sandbox.push.apple.com:2197", client.Host) +} + func TestClientDevelopmentHost(t *testing.T) { client := apns.NewClient(mockCert()).Development() assert.Equal(t, "https://api.sandbox.push.apple.com", client.Host) } +func TestClientDevelopmentHostWithAlternativePort(t *testing.T) { + client := apns.NewClient(mockCert()).Development().UseAlternativePort() + assert.Equal(t, "https://api.sandbox.push.apple.com:2197", client.Host) +} + func TestTokenClientDevelopmentHost(t *testing.T) { client := apns.NewTokenClient(mockToken()).Development() assert.Equal(t, "https://api.sandbox.push.apple.com", client.Host) } +func TestTokenClientDevelopmentHostWithAlternativePort(t *testing.T) { + client := apns.NewTokenClient(mockToken()).Development().UseAlternativePort() + assert.Equal(t, "https://api.sandbox.push.apple.com:2197", client.Host) +} + func TestClientProductionHost(t *testing.T) { client := apns.NewClient(mockCert()).Production() assert.Equal(t, "https://api.push.apple.com", client.Host) } +func TestClientProductionHostWithAlternativePort(t *testing.T) { + client := apns.NewClient(mockCert()).Production().UseAlternativePort() + assert.Equal(t, "https://api.push.apple.com:2197", client.Host) +} + func TestTokenClientProductionHost(t *testing.T) { client := apns.NewTokenClient(mockToken()).Production() assert.Equal(t, "https://api.push.apple.com", client.Host) } +func TestTokenClientProductionHostWithAlternativePort(t *testing.T) { + client := apns.NewTokenClient(mockToken()).Production().UseAlternativePort() + assert.Equal(t, "https://api.push.apple.com:2197", client.Host) +} + func TestClientBadUrlError(t *testing.T) { n := mockNotification() res, err := mockClient("badurl://badurl.com").Push(n) From 206d0e00dc74a70eec40a86cf92e1d74d3e59709 Mon Sep 17 00:00:00 2001 From: mtzfederico Date: Wed, 3 Aug 2022 20:46:25 +0200 Subject: [PATCH 2/2] Adds option to use alternative port --- apns2/main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apns2/main.go b/apns2/main.go index 826634db..2a85540f 100644 --- a/apns2/main.go +++ b/apns2/main.go @@ -16,6 +16,7 @@ var ( certificatePath = kingpin.Flag("certificate-path", "Path to certificate file.").Required().Short('c').String() topic = kingpin.Flag("topic", "The topic of the remote notification, which is typically the bundle ID for your app").Required().Short('t').String() mode = kingpin.Flag("mode", "APNS server to send notifications to. `production` or `development`. Defaults to `production`").Default("production").Short('m').String() + useAltPort = kingpin.Flag("alt-port", "Use APNS's alternative port (2197). Default is port 443.").Default("false").Bool() ) func main() { @@ -39,6 +40,10 @@ func main() { client.Production() } + if *useAltPort { + client.UseAlternativePort() + } + scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() {