pmusolino / Wormholy

iOS network debugging, like a wizard 🧙‍♂️
MIT License
2.31k stars 184 forks source link

Responses are not cached in CustomHTTPProtocol #135

Open Jyothi-Narayan opened 1 year ago

Jyothi-Narayan commented 1 year ago

Cache storage policy is not set as per the caching policy in URLRequest received. When the url request cache policy is NSURLRequestReturnCacheDataElseLoad , as per the below code storage policy is set to URLCache.StoragePolicy.notAllowed

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        let policy = URLCache.StoragePolicy(rawValue: request.cachePolicy.rawValue) ?? .notAllowed
        client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: policy)
        currentRequest?.initResponse(response: response)
        completionHandler(.allow)
    }

And it doesn't skip the api call when the url request policy is set to load from cache

override public class func canInit(with request: URLRequest) -> Bool {
        guard CustomHTTPProtocol.shouldHandleRequest(request) else { return false }

        if CustomHTTPProtocol.property(forKey: Constants.RequestHandledKey, in: request) != nil {
            return false
        }
        return true
    }

the above canInit func only ignores the request which are from ignored hosts, doesn't ignore the requests with cache policy set to NSURLRequestReturnCacheDataElseLoad/ NSURLRequestReturnCacheDataDontLoad