Mock URLProtocol with strict swift 6 concurrency

I generally recommend that you introduce an abstraction layer between your request-response code and your network transport. You can then:

  • Test the request-response code with an in-memory transport

  • Test the network transport against a server

That raises the question of what that abstraction layer looks like. Rather than try to mock URLSession itself, with its large and complex API, I recommend that you create a much smaller interface that’s focused on your product’s specific needs. For many apps that interface can be as simple as a throwing async function that takes a URLRequest and returns an HTTPURLResponse.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

6 Likes