From 680d344cd82e55c2e41159ecfc6977b105ef95cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Thu, 5 Jan 2017 19:48:44 +0100 Subject: [PATCH] Fix cache not working properly for URLs with many parameters --- Sources/Networking.swift | 6 ++---- Tests/NetworkingTests.swift | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Sources/Networking.swift b/Sources/Networking.swift index f096959..08b98a1 100644 --- a/Sources/Networking.swift +++ b/Sources/Networking.swift @@ -474,9 +474,7 @@ extension Networking { } } case .data, .image: - let trimmedPath = path.components(separatedBy: "?").first! - - let object = self.objectFromCache(for: trimmedPath, cacheName: cacheName, responseType: responseType) + let object = self.objectFromCache(for: path, cacheName: cacheName, responseType: responseType) if let object = object { TestCheck.testBlock(self.disableTestingMode) { completion(object, [String: Any](), nil) @@ -486,7 +484,7 @@ extension Networking { var returnedResponse: Any? if let data = data, data.count > 0 { - guard let destinationURL = try? self.destinationURL(for: trimmedPath, cacheName: cacheName) else { fatalError("Couldn't get destination URL for path: \(path) and cacheName: \(cacheName)") } + guard let destinationURL = try? self.destinationURL(for: path, cacheName: cacheName) else { fatalError("Couldn't get destination URL for path: \(path) and cacheName: \(cacheName)") } let _ = try? data.write(to: destinationURL, options: [.atomic]) switch responseType { case .data: diff --git a/Tests/NetworkingTests.swift b/Tests/NetworkingTests.swift index 1042066..7e43a19 100644 --- a/Tests/NetworkingTests.swift +++ b/Tests/NetworkingTests.swift @@ -223,8 +223,7 @@ class NetworkingTests: XCTestCase { let path = "/4973/as-images.apple.com/is/image/AppleInc/aos/published/images/i/pa/ipad/pro/ipad-pro-201603-gallery3?wid=4000&%3Bhei=1536&%3Bfmt=jpeg&%3Bqlt=95&%3Bop_sharpen=0&%3BresMode=bicub&%3Bop_usm=0.5%2C0.5%2C0%2C0&%3BiccEmbed=0&%3Blayer=comp&%3B.v=Y7wkx0&hei=3072" networking.downloadData(for: path) { downloadData, error in - let cacheKey = path.components(separatedBy: "?").first! - let cacheData = networking.dataFromCache(for: cacheKey) + let cacheData = networking.dataFromCache(for: path) XCTAssert(downloadData == cacheData!) } }