From b6b1762ab3924d7f7e94bcaaea68ff0ec4fe04bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= <3lvis@users.noreply.github.com> Date: Thu, 19 Jul 2018 13:01:57 +0200 Subject: [PATCH] Fix failing tests (#232) --- Sources/Networking+HTTPRequests.swift | 12 ++++---- Sources/Networking+Private.swift | 43 ++++++++++++++++----------- Tests/DownloadTests.swift | 2 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Sources/Networking+HTTPRequests.swift b/Sources/Networking+HTTPRequests.swift index e0a3fc2..23fc62a 100644 --- a/Sources/Networking+HTTPRequests.swift +++ b/Sources/Networking+HTTPRequests.swift @@ -13,7 +13,7 @@ public extension Networking { public func get(_ path: String, parameters: Any? = nil, cachingLevel: CachingLevel = .none, completion: @escaping (_ result: JSONResult) -> Void) -> String { let parameterType: ParameterType = parameters != nil ? .formURLEncoded : .none - return handleJSONRequest(.get, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: cachingLevel, completion: completion) + return handleJSONRequest(.get, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: cachingLevel, completion: completion) } /// Registers a fake GET request for the specified path. After registering this, every GET request to the path, will return the registered response. @@ -57,7 +57,7 @@ public extension Networking { /// - Returns: The request identifier. @discardableResult public func patch(_ path: String, parameterType: ParameterType = .json, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String { - return handleJSONRequest(.patch, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) + return handleJSONRequest(.patch, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) } /// Registers a fake PATCH request for the specified path. After registering this, every PATCH request to the path, will return the registered response. @@ -101,7 +101,7 @@ public extension Networking { /// - Returns: The request identifier. @discardableResult public func put(_ path: String, parameterType: ParameterType = .json, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String { - return handleJSONRequest(.put, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) + return handleJSONRequest(.put, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) } /// Registers a fake PUT request for the specified path. After registering this, every PUT request to the path, will return the registered response. @@ -145,7 +145,7 @@ public extension Networking { /// - Returns: The request identifier. @discardableResult public func post(_ path: String, parameterType: ParameterType = .json, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String { - return handleJSONRequest(.post, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) + return handleJSONRequest(.post, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) } /// POST request to the specified path, using the provided parameters. @@ -158,7 +158,7 @@ public extension Networking { /// - Returns: The request identifier. @discardableResult public func post(_ path: String, parameters: Any? = nil, parts: [FormDataPart], completion: @escaping (_ result: JSONResult) -> Void) -> String { - return handleJSONRequest(.post, path: path, parameterType: .multipartFormData, parameters: parameters, parts: parts, responseType: .json, cachingLevel: .none, completion: completion) + return handleJSONRequest(.post, path: path, cacheName: nil, parameterType: .multipartFormData, parameters: parameters, parts: parts, responseType: .json, cachingLevel: .none, completion: completion) } /// Registers a fake POST request for the specified path. After registering this, every POST request to the path, will return the registered response. @@ -202,7 +202,7 @@ public extension Networking { @discardableResult public func delete(_ path: String, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String { let parameterType: ParameterType = parameters != nil ? .formURLEncoded : .none - return handleJSONRequest(.delete, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) + return handleJSONRequest(.delete, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, responseType: .json, cachingLevel: .none, completion: completion) } /// Registers a fake DELETE request for the specified path. After registering this, every DELETE request to the path, will return the registered response. diff --git a/Sources/Networking+Private.swift b/Sources/Networking+Private.swift index b128f03..ea9eca7 100644 --- a/Sources/Networking+Private.swift +++ b/Sources/Networking+Private.swift @@ -57,7 +57,7 @@ extension Networking { fakeRequests[requestType] = requests } - func handleFakeRequest(_ fakeRequest: FakeRequest, path: String, cachingLevel: CachingLevel, completion: @escaping (_ body: Any?, _ response: HTTPURLResponse, _ error: NSError?) -> Void) -> String { + func handleFakeRequest(_ fakeRequest: FakeRequest, path: String, cacheName: String?, cachingLevel: CachingLevel, completion: @escaping (_ body: Any?, _ response: HTTPURLResponse, _ error: NSError?) -> Void) -> String { var error: NSError? let url = try! composedURL(with: path) let response = HTTPURLResponse(url: url, statusCode: fakeRequest.statusCode) @@ -71,11 +71,14 @@ extension Networking { error = NSError(fakeRequest: fakeRequest) } - guard let destinationURL = try? self.destinationURL(for: path, cacheName: nil) else { - fatalError("Couldn't get destination URL for path: \(path)") + switch fakeRequest.responseType { + case .image: + let _ = cacheOrPurgeImage(data: fakeRequest.response as? Data, path: path, cacheName: cacheName, cachingLevel: cachingLevel) + case .data: + cacheOrPurgeData(data: fakeRequest.response as? Data, path: path, cacheName: cacheName, cachingLevel: cachingLevel) + case .json: + try! cacheOrPurgeJSON(object: fakeRequest.response, path: path, cacheName: cacheName, cachingLevel: cachingLevel) } - - cacheOrPurgeObject(object: fakeRequest.response, cachingLevel: cachingLevel, destinationURL: destinationURL) completion(fakeRequest.response, response, error) } @@ -83,10 +86,10 @@ extension Networking { return requestID } - func handleJSONRequest(_ requestType: RequestType, path: String, parameterType: ParameterType?, parameters: Any?, parts: [FormDataPart]? = nil, responseType: ResponseType, cachingLevel: CachingLevel, completion: @escaping (_ result: JSONResult) -> Void) -> String { + func handleJSONRequest(_ requestType: RequestType, path: String, cacheName: String?, parameterType: ParameterType?, parameters: Any?, parts: [FormDataPart]? = nil, responseType: ResponseType, cachingLevel: CachingLevel, completion: @escaping (_ result: JSONResult) -> Void) -> String { switch cachingLevel { - case .none: + case .memory, .memoryAndFile: if let object = objectFromCache(for: path, cacheName: nil, cachingLevel: cachingLevel, responseType: responseType) { TestCheck.testBlock(isSynchronous) { let url = try! self.composedURL(with: path) @@ -98,7 +101,7 @@ extension Networking { } if let fakeRequest = FakeRequest.find(ofType: requestType, forPath: path, in: fakeRequests) { - return handleFakeRequest(fakeRequest, path: path, cachingLevel: cachingLevel) { _, response, error in + return handleFakeRequest(fakeRequest, path: path, cacheName: cacheName, cachingLevel: cachingLevel) { _, response, error in completion(JSONResult(body: fakeRequest.response, response: response, error: error)) } } else { @@ -112,7 +115,7 @@ extension Networking { func handleDataRequest(_ requestType: RequestType, path: String, cacheName: String?, cachingLevel: CachingLevel, responseType: ResponseType, completion: @escaping (_ result: DataResult) -> Void) -> String { if let fakeRequests = fakeRequests[requestType], let fakeRequest = fakeRequests[path] { - return handleFakeRequest(fakeRequest, path: path, cachingLevel: cachingLevel) { _, response, error in + return handleFakeRequest(fakeRequest, path: path, cacheName: cacheName, cachingLevel: cachingLevel) { _, response, error in completion(DataResult(body: fakeRequest.response, response: response, error: error)) } } else { @@ -127,7 +130,7 @@ extension Networking { return requestID } else { return requestData(requestType, path: path, cachingLevel: cachingLevel, parameterType: nil, parameters: nil, parts: nil, responseType: responseType) { data, response, error in - self.cacheOrPurgeData(data: data, cachingLevel: cachingLevel, path: path, cacheName: cacheName) + self.cacheOrPurgeData(data: data, path: path, cacheName: cacheName, cachingLevel: cachingLevel) TestCheck.testBlock(self.isSynchronous) { completion(DataResult(body: data, response: response, error: error)) @@ -139,7 +142,7 @@ extension Networking { func handleImageRequest(_ requestType: RequestType, path: String, cacheName: String?, cachingLevel: CachingLevel, responseType: ResponseType, completion: @escaping (_ result: ImageResult) -> Void) -> String { if let fakeRequests = fakeRequests[requestType], let fakeRequest = fakeRequests[path] { - return handleFakeRequest(fakeRequest, path: path, cachingLevel: cachingLevel) { _, response, error in + return handleFakeRequest(fakeRequest, path: path, cacheName: cacheName, cachingLevel: cachingLevel) { _, response, error in completion(ImageResult(body: fakeRequest.response, response: response, error: error)) } } else { @@ -155,7 +158,7 @@ extension Networking { return requestID } else { return requestData(requestType, path: path, cachingLevel: cachingLevel, parameterType: nil, parameters: nil, parts: nil, responseType: responseType) { data, response, error in - let returnedImage = self.cacheOrPurgeImage(data: data, cachingLevel: cachingLevel, path: path, cacheName: cacheName) + let returnedImage = self.cacheOrPurgeImage(data: data, path: path, cacheName: cacheName, cachingLevel: cachingLevel) TestCheck.testBlock(self.isSynchronous) { completion(ImageResult(body: returnedImage, response: response, error: error)) @@ -266,7 +269,7 @@ extension Networking { } } - self.cacheOrPurgeData(data: data, cachingLevel: cachingLevel, path: path, cacheName: nil) + self.cacheOrPurgeData(data: data, path: path, cacheName: nil, cachingLevel: cachingLevel) if TestCheck.isTesting && self.isSynchronous == false { semaphore.signal() @@ -419,15 +422,19 @@ extension Networking { print(" ") } - func cacheOrPurgeObject(object: Any?, cachingLevel: CachingLevel, destinationURL: URL) { + func cacheOrPurgeJSON(object: Any?, path: String, cacheName: String?, cachingLevel: CachingLevel) throws { + guard let destinationURL = try? self.destinationURL(for: path, cacheName: cacheName) else { + fatalError("Couldn't get destination URL for path: \(path) and cacheName: \(String(describing: cacheName))") + } + if let unwrappedObject = object { switch cachingLevel { case .memory: self.cache.setObject(unwrappedObject as AnyObject, forKey: destinationURL.absoluteString as AnyObject) case .memoryAndFile: - let convertedData = try! JSONSerialization.data(withJSONObject: unwrappedObject, options: []) - _ = try? convertedData.write(to: destinationURL, options: [.atomic]) + let convertedData = try JSONSerialization.data(withJSONObject: unwrappedObject, options: []) + _ = try convertedData.write(to: destinationURL, options: [.atomic]) self.cache.setObject(unwrappedObject as AnyObject, forKey: destinationURL.absoluteString as AnyObject) case .none: break @@ -437,7 +444,7 @@ extension Networking { } } - func cacheOrPurgeData(data: Data?, cachingLevel: CachingLevel, path: String, cacheName: String?) { + func cacheOrPurgeData(data: Data?, path: String, cacheName: String?, cachingLevel: CachingLevel) { guard let destinationURL = try? self.destinationURL(for: path, cacheName: cacheName) else { fatalError("Couldn't get destination URL for path: \(path) and cacheName: \(String(describing: cacheName))") } @@ -457,7 +464,7 @@ extension Networking { } } - func cacheOrPurgeImage(data: Data?, cachingLevel: CachingLevel, path: String, cacheName: String?) -> Image? { + func cacheOrPurgeImage(data: Data?, path: String, cacheName: String?, cachingLevel: CachingLevel) -> Image? { guard let destinationURL = try? self.destinationURL(for: path, cacheName: cacheName) else { fatalError("Couldn't get destination URL for path: \(path) and cacheName: \(String(describing: cacheName))") } diff --git a/Tests/DownloadTests.swift b/Tests/DownloadTests.swift index b3b1a91..e26bbfa 100644 --- a/Tests/DownloadTests.swift +++ b/Tests/DownloadTests.swift @@ -238,7 +238,7 @@ class DownloadTests: XCTestCase { guard let destinationURL = try? networking.destinationURL(for: path, cacheName: cacheName) else { XCTFail(); return } let absoluteString = destinationURL.absoluteString cache.removeObject(forKey: absoluteString as AnyObject) - guard let image = networking.imageFromCache(path) else { XCTFail(); return } + guard let image = networking.imageFromCache(path, cacheName: cacheName) else { XCTFail(); return } let pigImage = Image.find(named: "pig.png", inBundle: Bundle(for: DownloadTests.self)) let pigImageData = pigImage.pngData() let imageData = image.pngData()