Using URLProtocol to Mock and Test
When writing unit tests for Swift networking code that uses URLSession we don’t want to perform real network requests. We want to mock responses so that our unit tests remain fast, predictable and independent. Mocking responses from URLSession can be done by wrapping it in a protocol and creating a mock session when testing. But an alternative way is using URLProtocol. What is URLProtocol? URLProtocol is a class that allows you to intercept and handle network requests, giving you the ability to implement features such as caching, redirects, or authentication....