Skip to content

Commit

Permalink
Fix failing tests (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis authored Jul 19, 2018
1 parent 706df6d commit b6b1762
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
12 changes: 6 additions & 6 deletions Sources/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
43 changes: 25 additions & 18 deletions Sources/Networking+Private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -71,22 +71,25 @@ 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)
}

let requestID = UUID().uuidString
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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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))")
}
Expand All @@ -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))")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/DownloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b6b1762

Please sign in to comment.