Posts

Post not yet marked as solved
1 Replies
962 Views
We want to create a custom URLProtocol that returns mock HTTP responses for testing.This technique is explained in the following WWDC video (8:00)https://developer.apple.com/videos/play/wwdc2018/417/The WWDC example does not implement any of the URL Loading Systems caching API.After implementing the caching APIs we've found that they do not work as expected.Specifically, when using the default cache policy .useProtocolCachePolicy the loading system never passes a cachedResponse value to the protocol.We expect the cached response to be passed to the protocol so it can inspect it and signal if it should be used. Otherwise we cannot see how a general URL Loading System, that provides caching, could integrate with custom protocols.Example implementation:public override init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) { super.init(request: request, cachedResponse: cachedResponse, client: client) // All cachedResponses are valid if let cachedResponse == cachedResponse { client?.urlProtocol(self, cachedResponseIsValid: cachedResponse) } }If the cached response is not passed, the custom protocol can only access the shared cache which would exclude any URLSessions that use a per session cache.Have we misunderstood how the API is intended to be used, or is this a bug in the URL Loading System?
Posted Last updated
.