How I Implemented a Swift HTTP Request Helper Without a Working Server

Hint: Test doubles is the key

HTTP request helper is one of the most essential parts of most modern-day apps. It takes care of all the processing works before and after calling a remote API.

Ideally, the implementation of the HTTP request helper should start after the remote APIs are ready. However, in most cases, due to tight project schedules, mobile developers will not be given the luxury to start their development work after the remote APIs are ready.

In this kind of situation, some developers might rework their development plan to work on UI related features first while waiting for the server to get ready. Some others might set up a temporary local server to test out their networking module.

In this article, I will show you my way of dealing with this kind of situation — using test doubles to replicate the output from the remote APIs.

If you are not familiar with the concept of test doubles in Swift, feel free to check out this article that explains in detail the concept of dummy, fake, stub and mock.

🔗 Test Doubles in Swift: Dummy, Fake, Stub, Mock

The best way to demonstrate how stubbing and mocking a remote API can be done is by using a real-life example.

Without further ado, let’s get started.

Disclaimer: The following example might not be fully compliant with the RESTful API Designing guidelines and best practices. This is intended in order to reduce the complexity of the example.