From 16daea258180b92bb77f8a33d3a1770aca9f5d9f Mon Sep 17 00:00:00 2001 From: Antoine van der Lee Date: Fri, 26 Feb 2021 09:56:25 +0100 Subject: [PATCH] Fix tests and make sure the new opt-in mode is working with existing logic. (#86) --- MockerTests/MockerTests.swift | 38 +++++++++++++++++++++++++------- Sources/Mocker.swift | 16 ++++++++------ Sources/MockingURLProtocol.swift | 3 +-- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/MockerTests/MockerTests.swift b/MockerTests/MockerTests.swift index 739133f..0293d80 100644 --- a/MockerTests/MockerTests.swift +++ b/MockerTests/MockerTests.swift @@ -20,6 +20,17 @@ final class MockerTests: XCTestCase { } } + override func setUp() { + super.setUp() + Mocker.mode = .optout + } + + override func tearDown() { + Mocker.removeAll() + Mocker.mode = .optout + super.tearDown() + } + /// It should returned the register mocked image data as response. func testImageURLDataRequest() { let expectation = self.expectation(description: "Data request should succeed") @@ -77,7 +88,10 @@ final class MockerTests: XCTestCase { URLSession.shared.dataTask(with: originalURL) { (data, _, error) in XCTAssert(error == nil) - let image: UIImage = UIImage(data: data!)! + guard let data = data, let image: UIImage = UIImage(data: data) else { + XCTFail("Invalid data") + return + } let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)! XCTAssert(image.size == sampleImage.size, "Image should be returned mocked") @@ -101,7 +115,10 @@ final class MockerTests: XCTestCase { URLSession.shared.dataTask(with: customURL) { (data, _, error) in XCTAssert(error == nil) - let image: UIImage = UIImage(data: data!)! + guard let data = data, let image: UIImage = UIImage(data: data) else { + XCTFail("Invalid data") + return + } let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)! XCTAssert(image.size == sampleImage.size, "Image should be returned mocked") @@ -188,7 +205,10 @@ final class MockerTests: XCTestCase { urlSession.dataTask(with: originalURL!) { (data, _, error) in XCTAssert(error == nil) - let image: UIImage = UIImage(data: data!)! + guard let data = data, let image: UIImage = UIImage(data: data) else { + XCTFail("Invalid data") + return + } let sampleImage: UIImage = UIImage(contentsOfFile: MockedData.botAvatarImageFileUrl.path)! XCTAssert(image.size == sampleImage.size, "Image should be returned mocked") @@ -389,22 +409,24 @@ final class MockerTests: XCTestCase { let expectation = self.expectation(description: "Data request should succeed") let originalURL = URL(string: "https://www.wetransfer.com/example.json")! - enum TestExampleError: Error { + enum TestExampleError: Error, LocalizedError { case example + + var errorDescription: String { "example" } } Mock(url: originalURL, dataType: .json, statusCode: 500, data: [.get: Data()], requestError: TestExampleError.example).register() - URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, err) in + URLSession.shared.dataTask(with: originalURL) { (data, urlresponse, error) in XCTAssertNil(data) XCTAssertNil(urlresponse) - XCTAssertNotNil(err) - if let err = err { + XCTAssertNotNil(error) + if let error = error { // there's not a particularly elegant way to verify an instance // of an error, but this is a convenient workaround for testing // purposes - XCTAssertEqual("example", String(describing: err)) + XCTAssertTrue(String(describing: error).contains("TestExampleError")) } expectation.fulfill() diff --git a/Sources/Mocker.swift b/Sources/Mocker.swift index 96b2976..91d2f64 100644 --- a/Sources/Mocker.swift +++ b/Sources/Mocker.swift @@ -102,14 +102,15 @@ public struct Mocker { /// /// - Parameter url: The URL to check for. /// - Returns: `true` if it should be mocked, `false` if the URL is registered as ignored. - public static func shouldHandle(_ url: URL) -> Bool { - shared.queue.sync { - switch mode { - case .optout: - return !shared.ignoredRules.contains(where: { $0.shouldIgnore(url) }) - case .optin: - return shared.mocks.contains(where: { $0.url == url }) + public static func shouldHandle(_ request: URLRequest) -> Bool { + switch mode { + case .optout: + guard let url = request.url else { return false } + return shared.queue.sync { + !shared.ignoredRules.contains(where: { $0.shouldIgnore(url) }) } + case .optin: + return mock(for: request) != nil } } @@ -117,6 +118,7 @@ public struct Mocker { public static func removeAll() { shared.queue.sync(flags: .barrier) { shared.mocks.removeAll() + shared.ignoredRules.removeAll() } } diff --git a/Sources/MockingURLProtocol.swift b/Sources/MockingURLProtocol.swift index 68883fe..b68943c 100644 --- a/Sources/MockingURLProtocol.swift +++ b/Sources/MockingURLProtocol.swift @@ -88,8 +88,7 @@ public final class MockingURLProtocol: URLProtocol { /// Overrides needed to define a valid inheritance of URLProtocol. override public class func canInit(with request: URLRequest) -> Bool { - guard let url = request.url else { return false } - return Mocker.shouldHandle(url) + return Mocker.shouldHandle(request) } }