Test for Apple Push Notification Test for Apple Push Notification mongoose mongoose

Test for Apple Push Notification


In many situations, while writing tests, it is either impossible or simply too dangerous to verify that an action has really taken place (i.e. a push notification has been delivered). Imagine writing a unit test for the rm command where you would like to ensure that doing rm -rf / succeeds. Obviously, you cannot let this action take place and verify that your root partition is indeed empty!

What you can do, however (and should do, really), is verify that whatever commands, routines or other actions necessary to accomplish the task are being invoked correctly, without actually allowing them to take place.

In your particular situation, you do not need to verify that your push notification has been delivered because your application is not responsible for the notification's delivery. However, you can test that the push notification is being correctly delivered to the push server.

So, instead of testing for successful delivery, you test

  1. Whether the outgoing request is properly formatted (i.e. JSON is valid)
  2. Whether it contains the data you expect it to contain (i.e. a field in JSON is present and contains expected data)
  3. Whether the authentication token required by the server is included
  4. Whether the target server is correct (i.e. you are indeed sending the data to xxx.apple.com and not to localhost)

Ideally, these test requests will not even reach the target server - doing so would mean you are relying on two factors that are not always perfectly stable:

  • network connectivity
  • target server availability and proper functionality

In the past, I dealt with this so that I first manually issued a correct request, captured the response and then mocked the whole communication in the unit test (using i.e. nock. That way, I am completely in control of the whole communication.


As far as I know, there's no way to check if an APNS request has reached its destination or not. Apple tends to have this "everything's fine, and if it's not, then it should be your fault" policy with us developers. If things haven't changed since I started coding, you make an APNS request by sending raw data (JSON payload, you probably know the whole format) through the 2195 port, and you get absolutely no response for that.

Only thing I can think of, if you have a physical iOS device (an iPod, an iPhone or an iPad), you can "automate" a test by launching a PUSH request with a hardcoded token, corresponding to your device and a test app, and if you receive the notification then it works.

Oh, and if it doesn't work, please make sure you have all required ports open if you're operating behind a firewall. It's the first big stone I stepped into when I first dove into this ;) (related: https://support.apple.com/en-us/HT203609)


I would use a request mocking framework like nock to intercept the request to APN. The urls seem to be located in the code here.