From e7866bfb110483b79aea8190e4fb6cf479f478fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=B6reth?= Date: Tue, 2 Apr 2024 15:22:38 +0200 Subject: [PATCH 1/3] Added: HTTPURLResponse to: NetworkError.serializationError(let error, let response, let data). --- Source/NetworkError.swift | 18 ++++---- .../NetworkServices/BasicNetworkService.swift | 4 +- .../NetworkServices/NetworkServiceMock.swift | 2 +- Source/Resource+Decodable.swift | 14 ++++++- Source/Resource+Inspect.swift | 8 ++-- Source/Resource+Map.swift | 4 +- Source/Resource+Void.swift | 4 +- Source/Resource.swift | 4 +- ...rceWithError+NetworkErrorConvertible.swift | 2 +- Tests/DecodableResoureTest.swift | 10 ++--- Tests/ModifyRequestNetworkService.swift | 6 +-- Tests/NetworkErrorTest.swift | 15 +++---- Tests/NetworkServiceMockTest.swift | 4 +- Tests/NetworkServiceTest.swift | 2 +- Tests/ResourceInspectTest.swift | 41 ++++++++++++++----- Tests/ResourceTest.swift | 8 ++-- Tests/ResourceWithErrorTest.swift | 12 +++--- Tests/RetryNetworkserviceTest.swift | 3 +- 18 files changed, 96 insertions(+), 65 deletions(-) diff --git a/Source/NetworkError.swift b/Source/NetworkError.swift index 4e341a4..11e5d91 100644 --- a/Source/NetworkError.swift +++ b/Source/NetworkError.swift @@ -36,7 +36,7 @@ public enum NetworkError: Error, Sendable { /// Error on the server (HTTP Error 500...511) case serverError(response: HTTPURLResponse?, data: Data?) /// Parsing the body into expected type failed. - case serializationError(error: Error, data: Data?) + case serializationError(error: Error, response: HTTPURLResponse, data: Data?) /// Complete request failed. case requestError(error: Error) @@ -75,21 +75,21 @@ extension NetworkError: CustomDebugStringConvertible { case .cancelled: return "Request cancelled" case .unauthorized(let response, let data): - return "Authorization error: \(response), response: ".appendingContentsOf(data: data) + return "Authorization error, response headers: \(response), response body: ".appendingContentsOf(data: data) case .clientError(let response, let data): if let response = response { - return "Client error: \((response)), response: ".appendingContentsOf(data: data) + return "Client error, response headers: \((response)), response body: ".appendingContentsOf(data: data) } - return "Client error, response: ".appendingContentsOf(data: data) - case .serializationError(let description, let data): - return "Serialization error: \(description), response: ".appendingContentsOf(data: data) + return "Client error, response headers: nil, response body: ".appendingContentsOf(data: data) + case .serializationError(let error, let response, let data): + return "Serialization error: \(error), response headers: \(response), response body: ".appendingContentsOf(data: data) case .requestError(let error): return "Request error: \(error)" case .serverError(let response, let data): - if let response = response { - return "Server error: \(String(describing: response)), response: ".appendingContentsOf(data: data) + if let response { + return "Server error, response headers: \(String(describing: response)), response body: ".appendingContentsOf(data: data) } else { - return "Server error: nil, response: ".appendingContentsOf(data: data) + return "Server error: nil, response body: ".appendingContentsOf(data: data) } } } diff --git a/Source/NetworkServices/BasicNetworkService.swift b/Source/NetworkServices/BasicNetworkService.swift index ec91b10..fba67b4 100644 --- a/Source/NetworkServices/BasicNetworkService.swift +++ b/Source/NetworkServices/BasicNetworkService.swift @@ -84,9 +84,9 @@ public final class BasicNetworkService: NetworkService { } do { - return .success((try resource.parse(data), response)) + return .success((try resource.parse(response, data), response)) } catch let error { - return .failure(.serializationError(error: error, data: data)) + return .failure(.serializationError(error: error, response: response, data: data)) } } catch let error { if case URLError.cancelled = error { diff --git a/Source/NetworkServices/NetworkServiceMock.swift b/Source/NetworkServices/NetworkServiceMock.swift index 1d8c1b3..1f533ce 100644 --- a/Source/NetworkServices/NetworkServiceMock.swift +++ b/Source/NetworkServices/NetworkServiceMock.swift @@ -182,7 +182,7 @@ public final class NetworkServiceMock: NetworkService, @unchecked Sendable { switch scheduled { case .success((let data, let httpURLResponse)): do { - let result = try resource.parse(data) + let result = try resource.parse(httpURLResponse, data) return .success((result, httpURLResponse)) } catch { fatalError("Not able to parse data. Error: \(error)") diff --git a/Source/Resource+Decodable.swift b/Source/Resource+Decodable.swift index 949fb43..39a689d 100644 --- a/Source/Resource+Decodable.swift +++ b/Source/Resource+Decodable.swift @@ -33,7 +33,11 @@ extension Resource where Model: Decodable { /// - decoder: a decoder which can decode the payload into the model type /// - mapError: a closure which maps to Error public init(request: URLRequest, decoder: JSONDecoder, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { - self.init(request: request, parse: { try decoder.decode(Model.self, from: $0) }, mapError: mapError) + self.init(request: request, parse: { + try decoder.decode(Model.self, from: $1) + }, + mapError: mapError + ) } } @@ -47,6 +51,12 @@ extension Resource where Model: Decodable, E: NetworkErrorConvertible { /// - decoder: a decoder which can decode the payload into the model type /// - mapError: a closure which maps to Error public init(request: URLRequest, decoder: JSONDecoder) { - self.init(request: request, parse: { try decoder.decode(Model.self, from: $0) }, mapError: { E(networkError: $0) }) + self.init(request: request, parse: { + try decoder.decode(Model.self, from: $1) + }, + mapError: { + E(networkError: $0) + } + ) } } diff --git a/Source/Resource+Inspect.swift b/Source/Resource+Inspect.swift index 8c9a56e..3b10b4d 100644 --- a/Source/Resource+Inspect.swift +++ b/Source/Resource+Inspect.swift @@ -36,10 +36,10 @@ extension Resource { - parameter inspector: closure which gets passed the data - returns: a new resource which gets instepcted before parsing */ - public func inspectData(_ inspector: @escaping @Sendable (Data) -> Void) -> Resource { - let parse: @Sendable (Data) throws -> Model = { data in - inspector(data) - return try self.parse(data) + public func inspectData(_ inspector: @escaping @Sendable (HTTPURLResponse, Data) -> Void) -> Resource { + let parse: @Sendable (HTTPURLResponse, Data) throws -> Model = { response, data in + inspector(response, data) + return try self.parse(response, data) } return Resource(request: request, parse: parse, mapError: mapError) } diff --git a/Source/Resource+Map.swift b/Source/Resource+Map.swift index f33df20..662ad53 100644 --- a/Source/Resource+Map.swift +++ b/Source/Resource+Map.swift @@ -30,7 +30,9 @@ extension Resource { public func map(transform: @escaping @Sendable (Model) throws -> T) -> Resource { return Resource( request: request, - parse: { return try transform(try self.parse($0)) }, + parse: { response, data in + return try transform(try self.parse(response, data)) + }, mapError: mapError ) } diff --git a/Source/Resource+Void.swift b/Source/Resource+Void.swift index 06e3ccd..1b6ec9c 100644 --- a/Source/Resource+Void.swift +++ b/Source/Resource+Void.swift @@ -15,7 +15,7 @@ public extension Resource where Model == Void { /// - request: The request to get the remote data payload /// - mapError: a closure which maps to Error init(request: URLRequest, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { - self.init(request: request, parse: { _ in }, mapError: mapError) + self.init(request: request, parse: { _, _ in }, mapError: mapError) } } @@ -26,7 +26,7 @@ public extension Resource where Model == Void, E: NetworkErrorConvertible { /// - Parameters: /// - request: The request to get the remote data payload init(request: URLRequest) { - self.init(request: request, parse: { _ in }, mapError: { E(networkError: $0) }) + self.init(request: request, parse: { _, _ in }, mapError: { E(networkError: $0) }) } } diff --git a/Source/Resource.swift b/Source/Resource.swift index c0074cb..a3c75de 100644 --- a/Source/Resource.swift +++ b/Source/Resource.swift @@ -40,7 +40,7 @@ public struct Resource: Sendable { public let request: URLRequest /// Parses data into given model. - public let parse: @Sendable (_ data: Data) throws -> Model + public let parse: @Sendable (_ response: HTTPURLResponse, _ data: Data) throws -> Model public let mapError: @Sendable (_ networkError: NetworkError) -> E /// Creates a type safe resource, which can be used to fetch it with NetworkService @@ -49,7 +49,7 @@ public struct Resource: Sendable { /// - request: The request to get the remote data payload /// - parse: Parses data fetched with the request into given Model - public init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { + public init(request: URLRequest, parse: @escaping @Sendable (HTTPURLResponse, Data) throws -> Model, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { self.request = request self.parse = parse self.mapError = mapError diff --git a/Source/ResourceWithError+NetworkErrorConvertible.swift b/Source/ResourceWithError+NetworkErrorConvertible.swift index 920f53f..d3f500a 100644 --- a/Source/ResourceWithError+NetworkErrorConvertible.swift +++ b/Source/ResourceWithError+NetworkErrorConvertible.swift @@ -9,7 +9,7 @@ import Foundation public extension Resource where E: NetworkErrorConvertible { - init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model) { + init(request: URLRequest, parse: @escaping @Sendable (HTTPURLResponse, Data) throws -> Model) { self.request = request self.parse = parse self.mapError = { E(networkError: $0) } diff --git a/Tests/DecodableResoureTest.swift b/Tests/DecodableResoureTest.swift index b70be21..ae0df7c 100644 --- a/Tests/DecodableResoureTest.swift +++ b/Tests/DecodableResoureTest.swift @@ -33,8 +33,8 @@ class DecodableResoureTest: XCTestCase { func testResource_withValidData() { //When - let fetchedTrain = try? resource.parse(Train.validJSONData) - + let fetchedTrain = try? resource.parse(HTTPURLResponse.defaultMock, Train.validJSONData) + //Then XCTAssertEqual(fetchedTrain?.name, "ICE") } @@ -42,8 +42,8 @@ class DecodableResoureTest: XCTestCase { func testResource_withMAppedResult() { //When let nameResource = resource.map { $0.name } - let fetchedTrainName = try? nameResource.parse(Train.validJSONData) - + let fetchedTrainName = try? nameResource.parse(HTTPURLResponse.defaultMock, Train.validJSONData) + //Then XCTAssertEqual(fetchedTrainName, "ICE") } @@ -51,7 +51,7 @@ class DecodableResoureTest: XCTestCase { func testResource_WithInvalidData() throws { //When do { - _ = try resource.parse(Train.invalidJSONData) + _ = try resource.parse(HTTPURLResponse.defaultMock, Train.invalidJSONData) XCTFail("Expected method to throws") } catch { } } diff --git a/Tests/ModifyRequestNetworkService.swift b/Tests/ModifyRequestNetworkService.swift index c25dde6..ab11081 100644 --- a/Tests/ModifyRequestNetworkService.swift +++ b/Tests/ModifyRequestNetworkService.swift @@ -35,13 +35,13 @@ class ModifyRequestNetworkServiceTest: XCTestCase { ] let networkService = ModifyRequestNetworkService(networkService: networkServiceMock, requestModifications: modification) let request = URLRequest(path: "/trains", baseURL: .defaultMock) - let resource = Resource(request: request, parse: { _ in return 1 }) - + let resource = Resource(request: request, parse: { _, _ in return 1 }) + //When await networkService.requestResult(for: resource) //Then - let lastRequest = await networkServiceMock.lastRequest + let lastRequest = networkServiceMock.lastRequest let lastRequestURL = try XCTUnwrap(lastRequest?.url) XCTAssert(lastRequestURL.absoluteString.contains("key=1")) } diff --git a/Tests/NetworkErrorTest.swift b/Tests/NetworkErrorTest.swift index 8f0af49..5dc8a75 100644 --- a/Tests/NetworkErrorTest.swift +++ b/Tests/NetworkErrorTest.swift @@ -135,8 +135,8 @@ class NetworkErrorTest: XCTestCase { let debugDescription = error.debugDescription //Then - XCTAssert(debugDescription.hasPrefix("Authorization error: (request: URLRequest.defaultMock, parse: { data in - capuredParsingData = data + let capturedParsingData = Container(nil) + let capturedInspectedData = Container(nil) + + let resource = Resource(request: URLRequest.defaultMock, parse: { response, data in + capturedParsingData.setValue(data) return 1 }) //When - let inspectedResource = resource.inspectData({ data in - capturedInspectedData = data + let inspectedResource = resource.inspectData({ response, data in + capturedInspectedData.setValue(data) }) - let result = try? inspectedResource.parse(data) - + let result = try? inspectedResource.parse(HTTPURLResponse.defaultMock, data) + //Then XCTAssertNotNil(result) - XCTAssertEqual(capuredParsingData, capturedInspectedData) - XCTAssertEqual(data, capturedInspectedData) - XCTAssertEqual(capuredParsingData, data) + XCTAssertEqual(capturedParsingData.getValue(), capturedInspectedData.getValue()) + XCTAssertEqual(data, capturedInspectedData.getValue()) + XCTAssertEqual(capturedParsingData.getValue(), data) + } +} + +private class Container { + private var value: T + + init(_ value: T) { + self.value = value + } + + /// caller should manage concurrent access to its own state + func setValue(_ newValue: T) { + value = newValue + } + + /// caller should manage concurrent access to its own state + func getValue() -> T { + value } } diff --git a/Tests/ResourceTest.swift b/Tests/ResourceTest.swift index ed9fc22..d1ae136 100644 --- a/Tests/ResourceTest.swift +++ b/Tests/ResourceTest.swift @@ -31,11 +31,11 @@ class ResourceTest: XCTestCase { //Given let validData: Data! = "ICE".data(using: .utf8) - let resource = Resource(request: URLRequest.defaultMock, parse: { String(data: $0, encoding: .utf8) }) + let resource = Resource(request: URLRequest.defaultMock, parse: { String(data: $1, encoding: .utf8) }) //When - let name = try resource.parse(validData) - + let name = try resource.parse(HTTPURLResponse.defaultMock, validData) + //Then XCTAssertEqual(name, "ICE") } @@ -45,7 +45,7 @@ class ResourceTest: XCTestCase { let resource = Resource(request: URLRequest.defaultMock) //When - try resource.parse(Data()) + try resource.parse(HTTPURLResponse.defaultMock, Data()) } } diff --git a/Tests/ResourceWithErrorTest.swift b/Tests/ResourceWithErrorTest.swift index ab81d4c..192ef06 100644 --- a/Tests/ResourceWithErrorTest.swift +++ b/Tests/ResourceWithErrorTest.swift @@ -33,13 +33,13 @@ class ResourceWithErrorTest: XCTestCase { let resource = Resource( request: URLRequest.defaultMock, - parse: { String(data: $0, encoding: .utf8) }, + parse: { String(data: $1, encoding: .utf8) }, mapError: { $0 } ) //When - let name = try resource.parse(validData) - + let name = try resource.parse(HTTPURLResponse.defaultMock, validData) + //Then XCTAssertEqual(name, "ICE") } @@ -50,12 +50,12 @@ class ResourceWithErrorTest: XCTestCase { let resource = Resource( request: URLRequest.defaultMock, - parse: { String(data: $0, encoding: .utf8) }, + parse: { String(data: $1, encoding: .utf8) }, mapError: { $0 } ) //When - let numberOfCharacters = try resource.map(transform: { $0?.count }).parse(validData) + let numberOfCharacters = try resource.map(transform: { $0?.count }).parse(HTTPURLResponse.defaultMock, validData) //Then XCTAssertEqual(numberOfCharacters, 3) @@ -86,7 +86,7 @@ class ResourceWithErrorTest: XCTestCase { ) //When - try resource.parse(Data()) + try resource.parse(HTTPURLResponse.defaultMock, Data()) } } diff --git a/Tests/RetryNetworkserviceTest.swift b/Tests/RetryNetworkserviceTest.swift index ad588f1..5e7c9cf 100644 --- a/Tests/RetryNetworkserviceTest.swift +++ b/Tests/RetryNetworkserviceTest.swift @@ -27,12 +27,11 @@ import XCTest class RetryNetworkserviceTest: XCTestCase { var resource: Resource { let request = URLRequest(path: "/train", baseURL: .defaultMock) - return Resource(request: request, parse: { _ in return 1}) + return Resource(request: request, parse: { _, _ in return 1}) } func testRetryRequest_shouldRetry() async throws { //Given - let errorCount = 2 let numberOfRetries = 2 let networkServiceMock = NetworkServiceMock( Result.failure(.unknownError), From a1775fb6587b9f87c4363c19bd2e558f1cc5b20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=B6reth?= Date: Wed, 3 Jul 2024 12:31:20 +0200 Subject: [PATCH 2/3] reverted: HTTPURLResponse does not need to be part of Resource --- Source/NetworkServices/BasicNetworkService.swift | 2 +- Source/NetworkServices/NetworkServiceMock.swift | 2 +- Source/Resource+Decodable.swift | 4 ++-- Source/Resource+Inspect.swift | 8 ++++---- Source/Resource+Map.swift | 4 ++-- Source/Resource+Void.swift | 4 ++-- Source/Resource.swift | 4 ++-- Source/ResourceWithError+NetworkErrorConvertible.swift | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Source/NetworkServices/BasicNetworkService.swift b/Source/NetworkServices/BasicNetworkService.swift index fba67b4..c874653 100644 --- a/Source/NetworkServices/BasicNetworkService.swift +++ b/Source/NetworkServices/BasicNetworkService.swift @@ -84,7 +84,7 @@ public final class BasicNetworkService: NetworkService { } do { - return .success((try resource.parse(response, data), response)) + return .success((try resource.parse(data), response)) } catch let error { return .failure(.serializationError(error: error, response: response, data: data)) } diff --git a/Source/NetworkServices/NetworkServiceMock.swift b/Source/NetworkServices/NetworkServiceMock.swift index 1f533ce..1d8c1b3 100644 --- a/Source/NetworkServices/NetworkServiceMock.swift +++ b/Source/NetworkServices/NetworkServiceMock.swift @@ -182,7 +182,7 @@ public final class NetworkServiceMock: NetworkService, @unchecked Sendable { switch scheduled { case .success((let data, let httpURLResponse)): do { - let result = try resource.parse(httpURLResponse, data) + let result = try resource.parse(data) return .success((result, httpURLResponse)) } catch { fatalError("Not able to parse data. Error: \(error)") diff --git a/Source/Resource+Decodable.swift b/Source/Resource+Decodable.swift index 39a689d..731dfbb 100644 --- a/Source/Resource+Decodable.swift +++ b/Source/Resource+Decodable.swift @@ -34,7 +34,7 @@ extension Resource where Model: Decodable { /// - mapError: a closure which maps to Error public init(request: URLRequest, decoder: JSONDecoder, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { self.init(request: request, parse: { - try decoder.decode(Model.self, from: $1) + try decoder.decode(Model.self, from: $0) }, mapError: mapError ) @@ -52,7 +52,7 @@ extension Resource where Model: Decodable, E: NetworkErrorConvertible { /// - mapError: a closure which maps to Error public init(request: URLRequest, decoder: JSONDecoder) { self.init(request: request, parse: { - try decoder.decode(Model.self, from: $1) + try decoder.decode(Model.self, from: $0) }, mapError: { E(networkError: $0) diff --git a/Source/Resource+Inspect.swift b/Source/Resource+Inspect.swift index 3b10b4d..5ad717c 100644 --- a/Source/Resource+Inspect.swift +++ b/Source/Resource+Inspect.swift @@ -36,10 +36,10 @@ extension Resource { - parameter inspector: closure which gets passed the data - returns: a new resource which gets instepcted before parsing */ - public func inspectData(_ inspector: @escaping @Sendable (HTTPURLResponse, Data) -> Void) -> Resource { - let parse: @Sendable (HTTPURLResponse, Data) throws -> Model = { response, data in - inspector(response, data) - return try self.parse(response, data) + public func inspectData(_ inspector: @escaping @Sendable (Data) -> Void) -> Resource { + let parse: @Sendable (Data) throws -> Model = { data in + inspector(data) + return try self.parse(data) } return Resource(request: request, parse: parse, mapError: mapError) } diff --git a/Source/Resource+Map.swift b/Source/Resource+Map.swift index 662ad53..aefb9a8 100644 --- a/Source/Resource+Map.swift +++ b/Source/Resource+Map.swift @@ -30,8 +30,8 @@ extension Resource { public func map(transform: @escaping @Sendable (Model) throws -> T) -> Resource { return Resource( request: request, - parse: { response, data in - return try transform(try self.parse(response, data)) + parse: { data in + return try transform(try self.parse(data)) }, mapError: mapError ) diff --git a/Source/Resource+Void.swift b/Source/Resource+Void.swift index 1b6ec9c..06e3ccd 100644 --- a/Source/Resource+Void.swift +++ b/Source/Resource+Void.swift @@ -15,7 +15,7 @@ public extension Resource where Model == Void { /// - request: The request to get the remote data payload /// - mapError: a closure which maps to Error init(request: URLRequest, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { - self.init(request: request, parse: { _, _ in }, mapError: mapError) + self.init(request: request, parse: { _ in }, mapError: mapError) } } @@ -26,7 +26,7 @@ public extension Resource where Model == Void, E: NetworkErrorConvertible { /// - Parameters: /// - request: The request to get the remote data payload init(request: URLRequest) { - self.init(request: request, parse: { _, _ in }, mapError: { E(networkError: $0) }) + self.init(request: request, parse: { _ in }, mapError: { E(networkError: $0) }) } } diff --git a/Source/Resource.swift b/Source/Resource.swift index a3c75de..c0074cb 100644 --- a/Source/Resource.swift +++ b/Source/Resource.swift @@ -40,7 +40,7 @@ public struct Resource: Sendable { public let request: URLRequest /// Parses data into given model. - public let parse: @Sendable (_ response: HTTPURLResponse, _ data: Data) throws -> Model + public let parse: @Sendable (_ data: Data) throws -> Model public let mapError: @Sendable (_ networkError: NetworkError) -> E /// Creates a type safe resource, which can be used to fetch it with NetworkService @@ -49,7 +49,7 @@ public struct Resource: Sendable { /// - request: The request to get the remote data payload /// - parse: Parses data fetched with the request into given Model - public init(request: URLRequest, parse: @escaping @Sendable (HTTPURLResponse, Data) throws -> Model, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { + public init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) { self.request = request self.parse = parse self.mapError = mapError diff --git a/Source/ResourceWithError+NetworkErrorConvertible.swift b/Source/ResourceWithError+NetworkErrorConvertible.swift index d3f500a..920f53f 100644 --- a/Source/ResourceWithError+NetworkErrorConvertible.swift +++ b/Source/ResourceWithError+NetworkErrorConvertible.swift @@ -9,7 +9,7 @@ import Foundation public extension Resource where E: NetworkErrorConvertible { - init(request: URLRequest, parse: @escaping @Sendable (HTTPURLResponse, Data) throws -> Model) { + init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model) { self.request = request self.parse = parse self.mapError = { E(networkError: $0) } From 226c1baa236e614c537364023905954dcdc04939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=B6reth?= Date: Wed, 3 Jul 2024 12:38:02 +0200 Subject: [PATCH 3/3] reverted: HTTPURLResponse does not need to be part of Resource --- Tests/DecodableResoureTest.swift | 6 +++--- Tests/ModifyRequestNetworkService.swift | 2 +- Tests/ResourceInspectTest.swift | 6 +++--- Tests/ResourceTest.swift | 6 +++--- Tests/ResourceWithErrorTest.swift | 10 +++++----- Tests/RetryNetworkserviceTest.swift | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Tests/DecodableResoureTest.swift b/Tests/DecodableResoureTest.swift index ae0df7c..2282fbb 100644 --- a/Tests/DecodableResoureTest.swift +++ b/Tests/DecodableResoureTest.swift @@ -33,7 +33,7 @@ class DecodableResoureTest: XCTestCase { func testResource_withValidData() { //When - let fetchedTrain = try? resource.parse(HTTPURLResponse.defaultMock, Train.validJSONData) + let fetchedTrain = try? resource.parse(Train.validJSONData) //Then XCTAssertEqual(fetchedTrain?.name, "ICE") @@ -42,7 +42,7 @@ class DecodableResoureTest: XCTestCase { func testResource_withMAppedResult() { //When let nameResource = resource.map { $0.name } - let fetchedTrainName = try? nameResource.parse(HTTPURLResponse.defaultMock, Train.validJSONData) + let fetchedTrainName = try? nameResource.parse(Train.validJSONData) //Then XCTAssertEqual(fetchedTrainName, "ICE") @@ -51,7 +51,7 @@ class DecodableResoureTest: XCTestCase { func testResource_WithInvalidData() throws { //When do { - _ = try resource.parse(HTTPURLResponse.defaultMock, Train.invalidJSONData) + _ = try resource.parse(Train.invalidJSONData) XCTFail("Expected method to throws") } catch { } } diff --git a/Tests/ModifyRequestNetworkService.swift b/Tests/ModifyRequestNetworkService.swift index ab11081..9133082 100644 --- a/Tests/ModifyRequestNetworkService.swift +++ b/Tests/ModifyRequestNetworkService.swift @@ -35,7 +35,7 @@ class ModifyRequestNetworkServiceTest: XCTestCase { ] let networkService = ModifyRequestNetworkService(networkService: networkServiceMock, requestModifications: modification) let request = URLRequest(path: "/trains", baseURL: .defaultMock) - let resource = Resource(request: request, parse: { _, _ in return 1 }) + let resource = Resource(request: request, parse: { _ in return 1 }) //When await networkService.requestResult(for: resource) diff --git a/Tests/ResourceInspectTest.swift b/Tests/ResourceInspectTest.swift index 812e4a1..b0d615e 100644 --- a/Tests/ResourceInspectTest.swift +++ b/Tests/ResourceInspectTest.swift @@ -30,16 +30,16 @@ final class ResourceInspectTest: XCTestCase { let capturedParsingData = Container(nil) let capturedInspectedData = Container(nil) - let resource = Resource(request: URLRequest.defaultMock, parse: { response, data in + let resource = Resource(request: URLRequest.defaultMock, parse: { data in capturedParsingData.setValue(data) return 1 }) //When - let inspectedResource = resource.inspectData({ response, data in + let inspectedResource = resource.inspectData({ data in capturedInspectedData.setValue(data) }) - let result = try? inspectedResource.parse(HTTPURLResponse.defaultMock, data) + let result = try? inspectedResource.parse(data) //Then XCTAssertNotNil(result) diff --git a/Tests/ResourceTest.swift b/Tests/ResourceTest.swift index d1ae136..c4ecd43 100644 --- a/Tests/ResourceTest.swift +++ b/Tests/ResourceTest.swift @@ -31,10 +31,10 @@ class ResourceTest: XCTestCase { //Given let validData: Data! = "ICE".data(using: .utf8) - let resource = Resource(request: URLRequest.defaultMock, parse: { String(data: $1, encoding: .utf8) }) + let resource = Resource(request: URLRequest.defaultMock, parse: { String(data: $0, encoding: .utf8) }) //When - let name = try resource.parse(HTTPURLResponse.defaultMock, validData) + let name = try resource.parse(validData) //Then XCTAssertEqual(name, "ICE") @@ -45,7 +45,7 @@ class ResourceTest: XCTestCase { let resource = Resource(request: URLRequest.defaultMock) //When - try resource.parse(HTTPURLResponse.defaultMock, Data()) + try resource.parse(Data()) } } diff --git a/Tests/ResourceWithErrorTest.swift b/Tests/ResourceWithErrorTest.swift index 192ef06..c073f9b 100644 --- a/Tests/ResourceWithErrorTest.swift +++ b/Tests/ResourceWithErrorTest.swift @@ -33,12 +33,12 @@ class ResourceWithErrorTest: XCTestCase { let resource = Resource( request: URLRequest.defaultMock, - parse: { String(data: $1, encoding: .utf8) }, + parse: { String(data: $0, encoding: .utf8) }, mapError: { $0 } ) //When - let name = try resource.parse(HTTPURLResponse.defaultMock, validData) + let name = try resource.parse(validData) //Then XCTAssertEqual(name, "ICE") @@ -50,12 +50,12 @@ class ResourceWithErrorTest: XCTestCase { let resource = Resource( request: URLRequest.defaultMock, - parse: { String(data: $1, encoding: .utf8) }, + parse: { String(data: $0, encoding: .utf8) }, mapError: { $0 } ) //When - let numberOfCharacters = try resource.map(transform: { $0?.count }).parse(HTTPURLResponse.defaultMock, validData) + let numberOfCharacters = try resource.map(transform: { $0?.count }).parse(validData) //Then XCTAssertEqual(numberOfCharacters, 3) @@ -86,7 +86,7 @@ class ResourceWithErrorTest: XCTestCase { ) //When - try resource.parse(HTTPURLResponse.defaultMock, Data()) + try resource.parse(Data()) } } diff --git a/Tests/RetryNetworkserviceTest.swift b/Tests/RetryNetworkserviceTest.swift index 5e7c9cf..0c3a70b 100644 --- a/Tests/RetryNetworkserviceTest.swift +++ b/Tests/RetryNetworkserviceTest.swift @@ -27,7 +27,7 @@ import XCTest class RetryNetworkserviceTest: XCTestCase { var resource: Resource { let request = URLRequest(path: "/train", baseURL: .defaultMock) - return Resource(request: request, parse: { _, _ in return 1}) + return Resource(request: request, parse: { _ in return 1}) } func testRetryRequest_shouldRetry() async throws {