WeTransfer / Mocker

Mock Alamofire and URLSession requests without touching your code implementation
MIT License
1.12k stars 96 forks source link

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

Closed iOSDeveloperL closed 2 years ago

iOSDeveloperL commented 3 years ago

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>
github-actions[bot] commented 2 years ago

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.

iOSDeveloperL commented 2 years ago

this is still an issue.

github-actions[bot] commented 2 years ago

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.

iOSDeveloperL commented 2 years ago

unfortunately this is still an issue.

github-actions[bot] commented 2 years ago

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.

iOSDeveloperL commented 2 years ago

still an issue.

github-actions[bot] commented 2 years ago

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.

iOSDeveloperL commented 2 years ago

this is still an issue.

scottymack commented 1 year ago

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