Hacking Launch URLs to Enable Dynamic API Testing
by Scott Olson, on Aug 26, 2015 11:35:46 AM
This post belongs to our Mobile Dev Series
Disclaimer: If you don’t enjoy coding and have zero interest in learning, the following might not be for you. However, the Modus tech team has built awesome apps on all platforms and made it super easy for you to create your own sales app.
Your API engineers are ready to test and deploy a new change, and they need to test the mobile apps against the change. They come over to your desk and ask you for a build of the apps that point to their test endpoint. You spend 10 minutes switching back to the latest stable tag, changing the API base URL, building, and deploying to a test iPad. Two weeks later the same engineers show up with the iPad at your desk, asking why it's not pointing to the right API endpoint. You slowly open your drawer and reach for a pen to gouge your eyes out...
Sound familiar? Cooking up test versions of our applications for one-off testing is a cross we don't need to bear as mobile developers. We have come up with a scheme for allowing the engineers and QA to change the API endpoints themselves, without any hidden UI elements that make it into the app store builds.
The trick here is to use a specially formatted launch URL scheme that allows the API endpoint to be passed in via a query string parameter. This value is parsed by the launch handling logic and overwrites the baked in API endpoint for the duration of that app session. Each mobile platform has slightly different ways of accomplishing this.
Let's take a look at how to test stage API on iOS for an example:
In this code block, we parse out the override URL and set it in the user defaults. When we initialize our service later, we can check for this override default and use that as our base URL. Once this code goes live, your testers can now choose any URL they wish and override the API endpoint simply by launching the app from a link containing the override URL. Your days of pulling out your hair creating builds for each API test are over!
Sometimes testers don't close the application and relaunch between sessions, so it is helpful to make an obvious UI change when the API endpoint is being overridden. I like to change the navigation bar or tab bar background color to an awful color, like Misty Rose.
This way, the fact that the app is using an override URL for the API endpoint is painfully obvious to anyone using the application.