AliSoftware / OHHTTPStubs

Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
MIT License
5.03k stars 601 forks source link

Possible Upload workaround (based on OCMock) #312

Open ghost opened 5 years ago

ghost commented 5 years ago

I have been able to implement mock tests for my upload requests based on this Stackoverflow answer.

As a result, I have removed every OHHttpStubs dependency from my project, to not mix libraries. Maybe you could change your internal approach to stubbing the web requests and support upload tasks as well, based on OCMock?

If you do not want to depend on an external library, I think you could implement similar behaviour as OCMock with Method Swizzling.

jeffctown commented 4 years ago

Hey @Shanakor ! I'm not exactly sure what you're asking for here. Can you please clarify what you need? This library already uses method swizzling.

ghost commented 4 years ago

I was referring to the "Known Limitations" section in the readme.md (https://github.com/AliSoftware/OHHTTPStubs#known-limitations).

It states that OHHTTPStubs is unable to handle upload requests.

OHHTTPStubs don't simulate data upload. The NSURLProtocolClient @protocol does not provide a way to signal the delegate that data has been sent (only that some has been loaded), so any data in the HTTPBody or HTTPBodyStream of an NSURLRequest, or data provided to -[NSURLSession uploadTaskWithRequest:fromData:]; will be ignored, and more importantly, the -URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: delegate method will never be called when you stub the request using OHHTTPStubs

and

As far as I know, there's nothing we can do about those three limitations. Please let me know if you know a solution that would make that possible anyway.

So, why do you not simply swizzle the methods of classes like NSURLConnection like in the stackoverflow link I posted?

kirhgoff commented 1 year ago

We also faced the issue with using OHHTTPStubs in out project. We failed to mock the upload request (it hags out until it times out and only then returns mocked data). Looking for a solution for that.

If it is possible with OHHTTPStubs, would appreciate some help