iOS: Mock server behaviors and responses in Swift using Alamofire + Kakapo

Raul Peña Alonso
Tiendeo Tech
Published in
3 min readDec 7, 2018

In Tiendeo Mobile department we’re always looking for new ways to make our app more testable and our tests better.

Let me introduce basic concepts about our data layer. If you are familiar with Alamofire and AlamofireObjectMapper, our app data layer uses a custom Alamofire SessionManager class, called AuthenticationSessionManager, that implements RequestAdapter and RequestRetrier protocols.

Our AuthenticationSessionManager has its own request method to check if user token is valid before call the Alamofire request method:

For further information about how we handle the authentication flow, have a look to other of my post. 😉

In purpose to test the refresh token flow we started using Kakapo library to dynamically mock server behaviors and responses.

Basically, Kakapo library provides you a Router class that registers endpoints to be intercepted in order to get a custom response. You have a good documentation in its readme file in GitHub. So, let’s start testing! 💪

Let’s start with this test case scenario:

  • Expired token
  • Refresh token API call succeeded
  • Fake API call succeeded

Following the steps indicated in the next diagram:

First we create a Unit Testing Test Class called AuthenticationSessionManagerTest and implement one method that returns an invalid token and another two methods to setup the interceptors, with Kakapo’s Router class, for the two succeeded API calls:

Then we need to configure our custom SessionManager to use Kakapo protocol. We’re going to create a method to create a custom URLSessionConfiguration:

And… we’re done with the //Given part of the test:

Let’s sum up: we have already a sessionManager configured with Kakapo and an invalid token. We also have two API call ready to be intercepted. 💪

Now we have to implement the //When & //Then part of the test.

We’re going to use our sessionManager to make the request to “_api/Fake”, which is intercepted. sessionManager, as we saw at the beginning of the post, will check the token first, but the token won’t be valid… sessionManager automatically will request a new fresh token to “_api/TokenNew”, which is also intercepted. When it gets the new token, finally it’ll call to “_api/Fake”, whose response will be success. 🎊

Let’s see the code:

Here it is how the test looks! We’re using expectation to check that the value of the response is the same value that we set to Kakapo’s router.

From here on

With this test as a sample, we could create multiple test scenarios to test our SessionManager like:

  • Valid token
  • Fake API call succeeded

or:

  • No token
  • Refresh token API call succeeded
  • Fake API call succeeded

or:

  • Valid token
  • Fake API call response 401

etc.
Just changing the //Given conditions and adding new responses to Kakapo’s router.

Hope you found this post interesting and useful for your projects. Any question or comment will be welcome!

Thanks and good luck!!!

--

--