WeTransfer/Mocker

RequestError: doesn't mock the error that's passed in and always return sessionTaskFailed

iOSDeveloperL opened this issue · 9 comments

I want to test our error handling and was hoping to use Mocker for it. We use Alamofire (but the bug is reproducible with URLSession).

Without Alamofire (pseudo code)

                    var url: URL!
                    var urlSession: URLSession!

                    beforeEach {
                        let configuration = URLSessionConfiguration.default
                        configuration.protocolClasses = [MockingURLProtocol.self]
                        urlSession = URLSession(configuration: configuration)

                        let error = ResponseError.testError
                        url = URL(string: "https://test.mycompany.com/thisisvalid?key=hello")!
                                                
                        mock = Mock(url: url, ignoreQuery: true, dataType: .json, statusCode: 404, data: [.get: Data()], requestError: error)
                        mock.register()
                    }

                    it("should call completion with correct error") {
                        waitUntil(timeout: .seconds(2)) { done in
                            urlSession.dataTask(with: url) { (data, response, error) in
                                debugPrint("this error is: \(error as Optional)")

// assertions on actions based on error
                                done()
                            }.resume()
                        }
                    }

po error

ResponseError Code=0 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <xxxx>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <xxxx>.<1>}

^ this should have been the error I passed but instead I received a .sessionTaskFailed error.

I did spend some time looking into this and the finishRequest inside MockingURLProtocol does get the mock.requestError properly but from there i'm not sure where it goes.

In the actual project i'm using Alamofire but there too i'm getting similar error. The requestError in that case was let error: AFError = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)).

the returned error is

▿ Optional<Error>
  - some : Error Domain=Alamofire.AFError Code=9 "(null)" UserInfo={_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <xxxx>
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <xxxx>

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.

this is still an issue.

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.

unfortunately this is still an issue.

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.

still an issue.

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.

this is still an issue.

@iOSDeveloperL I'm seeing the same behavior. Did you ever find a solution to this issue?