Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to use alternative port (2197) #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apns2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -39,6 +40,10 @@ func main() {
client.Production()
}

if *useAltPort {
client.UseAlternativePort()
}

scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
Expand Down
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down