Open d-a-n opened 6 years ago
Hey
Thanks for the issue report
The problem you have is that you're using the shared URLSessionConfiguration
, which is kind of special (see Apple docs) as it uses the global URLProtocolClasses.
I think you're not even supposed to reuse the .configuration
of the shared
session in practice? (I'll have to check the Apple docs to confirm).
That's because the shared session has kind of a special status compared to any other custom session. E.g. it uses the same global URLProtocolClasses
as the old NSURLConnection
, which this intercept the outgoing requests differently, compared to any custom URLSession which has its own URLProtocolClasses and its own interception of stubs.
So instead what you're supposed to do is:
1) either make a mutable copy of the URLSession.shared.configuration
, then explicitly enable the stubs on that configuration (OHHTTPStubs.enable(true, for: yourMutableCopy)
, iirc) before using it to create the new URLSession with it
2) or just simply use URLSessionConfiguration.default
(Or a copy of it if you wanted to customize some parameters of that configuration first)
The problem here is more how Apple made the shared Configuration a special case. The shared config is read only so I can't insert the stubs magically into it anyway, but if you use any other configuration, or if you explicitly enable the stubs manually on a copy of that shared config you should be good to go. As said, even outside OHHTTPStubs, I'm not even sure it's recommended / meant to be to reuse the shared session config.
New Issue Checklist
OHHTTPStubs
for your project sectionEnvironment
Issue Description
OHHTTPStubs does not intercept custom URLSessions. When using
URLSession.shared.dataTask
everything is working as expected. However when creating an own instance or URLSession the stub does not work anymore.Update
It is working with
Shouldn't
let session = URLSession(configuration: URLSessionConfiguration.default)
andlet session = URLSession(configuration: URLSession.shared.configuration)
behave the same way?