@@ -26,16 +26,9 @@ import Dispatch
2626
2727struct NetworkServiceMockCallback {
2828 let onErrorCallback : ( ( NetworkError ) -> Void ) ?
29- let onSuccess : ( ( Data , HTTPURLResponse ) -> Void ) ?
3029 let onTypedSuccess : ( ( Any , HTTPURLResponse ) -> Void ) ?
3130
3231 init < Result> ( resource: Resource < Result > , onCompletionWithResponse: @escaping ( Result , HTTPURLResponse ) -> Void , onError: @escaping ( NetworkError ) -> Void ) {
33- onSuccess = { data, response in
34- guard let result = try ? resource. parse ( data) else {
35- fatalError ( " Could not parse data into matching result type " )
36- }
37- onCompletionWithResponse ( result, response)
38- }
3932 onTypedSuccess = { anyResult, response in
4033 guard let typedResult = anyResult as? Result else {
4134 fatalError ( " Extected type of \( Result . self) but got \( anyResult. self) " )
@@ -104,10 +97,20 @@ struct NetworkServiceMockCallback {
10497 - seealso: `NetworkService`
10598 */
10699public final class NetworkServiceMock : NetworkService {
100+
107101 /// Count of all started requests
108- public var requestCount : Int = 0
102+ public var requestCount : Int {
103+ return lastRequests. count
104+ }
105+
109106 /// Last executed request
110- public var lastRequest : URLRequestConvertible ?
107+ public var lastRequest : URLRequest ? {
108+ return lastRequests. last
109+ }
110+
111+ /// All executed requests.
112+ public private( set) var lastRequests : [ URLRequest ] = [ ]
113+
111114 /// Set this to hava a custom networktask returned by the mock
112115 public var nextNetworkTask : NetworkTask ?
113116
@@ -144,9 +147,7 @@ public final class NetworkServiceMock: NetworkService {
144147 @discardableResult
145148 public func request< Result> ( queue: DispatchQueue , resource: Resource < Result > , onCompletionWithResponse: @escaping ( Result , HTTPURLResponse ) -> Void ,
146149 onError: @escaping ( NetworkError ) -> Void ) -> NetworkTask {
147-
148- lastRequest = resource. request
149- requestCount += 1
150+ lastRequests. append ( resource. request)
150151 callbacks. append ( NetworkServiceMockCallback (
151152 resource: resource,
152153 onCompletionWithResponse: onCompletionWithResponse,
@@ -164,42 +165,6 @@ public final class NetworkServiceMock: NetworkService {
164165 callbacks. removeFirst ( ) . onErrorCallback ? ( error)
165166 }
166167
167- /// Will return an error to the current waiting request.
168- ///
169- /// - Parameters:
170- /// - error: the error which gets passed to the caller
171- /// - count: the count, how often the error occours.
172- @available ( * , deprecated, message: " Use returnError without count parameter instead. Multiple calls can be done manually. " )
173- public func returnError( with error: NetworkError , count: Int ) {
174- ( 0 ..< count) . forEach { _ in
175- precondition ( !callbacks. isEmpty, " There is no request left to return an error for. " )
176- returnError ( with: error)
177- }
178- }
179-
180- /// Will return a successful request, by using the given data as a server response.
181- ///
182- /// - Parameters:
183- /// - data: the mock response from the server.
184- /// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
185- public func returnSuccess( with data: Data , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) ) {
186- callbacks. removeFirst ( ) . onSuccess ? ( data, httpResponse)
187- }
188-
189- /// Will return a successful request, by using the given data as a server response.
190- ///
191- /// - Parameters:
192- /// - data: the mock response from the server. `Data()` by default
193- /// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
194- /// - count: the count how often the response gets triggerd.
195- @available ( * , deprecated, message: " Use returnSuccess without count parameter instead. Multiple calls can be done manually. " )
196- public func returnSuccess( with data: Data = Data ( ) , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) , count: Int = 1 ) {
197- ( 0 ..< count) . forEach { _ in
198- precondition ( !callbacks. isEmpty, " There is no request left to return a success for. " )
199- returnSuccess ( with: data, httpResponse: httpResponse)
200- }
201- }
202-
203168 /// Will return a successful request, by using the given type `T` as serialized result of a request.
204169 ///
205170 /// **Warning:** This will crash if type `T` does not match your expected ResponseType of your current request
@@ -210,21 +175,5 @@ public final class NetworkServiceMock: NetworkService {
210175 public func returnSuccess< T> ( with serializedResponse: T , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) ) {
211176 callbacks. removeFirst ( ) . onTypedSuccess ? ( serializedResponse, httpResponse)
212177 }
213-
214- /// Will return a successful request, by using the given type `T` as serialized result of a request.
215- ///
216- /// **Warning:** This will crash if type `T` does not match your expected ResponseType of your current request
217- ///
218- /// - Parameters:
219- /// - data: the mock response from the server. `Data()` by default
220- /// - httpResponse: the mock `HTTPURLResponse` from the server. `HTTPURLResponse()` by default
221- /// - count: the count how often the response gets triggerd.
222- @available ( * , deprecated, message: " Use returnSuccess without count parameter instead. Multiple calls can be done manually. " )
223- public func returnSuccess< T> ( with serializedResponse: T , httpResponse: HTTPURLResponse = HTTPURLResponse ( ) , count: Int ) {
224- ( 0 ..< count) . forEach { _ in
225- precondition ( !callbacks. isEmpty, " There is no request left to return a typed success for. " )
226- returnSuccess ( with: serializedResponse, httpResponse: httpResponse)
227- }
228- }
229-
178+
230179}
0 commit comments