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.04k stars 601 forks source link

Unable to stub WKWebview? #179

Closed trilorez closed 8 years ago

trilorez commented 8 years ago

I'm having issues attempting to stub WKWebview. Here's a simple test project. I added a catch-all stub in the AppDelegate. The stub test block gets called but the response block is never called. Digging a little deeper, I see that OHHTTPStubsProtocol initWithRequest:cachedResponse:client: is never called, even though canInitWithRequest: returns true. Does WKWebview not support stubbing or am I missing something?

AliSoftware commented 8 years ago

Thanks for filing an issue and providing a sample test project to reproduce the issue! 👌

That's a good question actually. I can already confirm that I reproduce the issue and can't stub the requests sent by the WKWebView 😢

I'll investigate a bit more, but at first glance my theory is that, since WKWebView is a bit special as it runs in a separate process different than the one from the application (e.g. you have less control over it for stuff like cookies, etc), that might be related. Will keep you posted.

AliSoftware commented 8 years ago

Ok after a little digging, I can confirm that it is indeed a limitation of WKWebView.

See for example this answer in SO but also this radar as well as this one. As OHHTTPStubs uses an NSURLProtocol behind the scenes to catch the outgoing requests transparently and return the stubs before they hit the network, then WKWebView not supporting custom NSURLProtocol classes means we'll never be able to catch those requests from the application.

This is all because WKWebView uses an out-of-process engine to send requests, execute JS and render the page, and is totally isolated from the app process. So we have very little control over WKWebView for this sort of things (they have the same problem about cookies and similar features btw)

I'm going to close this issue now, as this is actually an Apple Bug and I won't be able to do anything about it, but feel free to open a bug report to Apple (by duplicating the radars I mentioned above) to make this possible one day… hopefully!

trilorez commented 8 years ago

Ah, I was afraid of that. Thanks for the quick response and the great library!