Stub not working for shared configuration
d-a-n opened this issue · comments
New Issue Checklist
- I have tried with the latest version of OHHTTPStubs
- I have read the README
- I have read the Using the right Swift Version of
OHHTTPStubs
for your project section - I have searched in the existing issues
- I have read the OHHTTPStubs wiki to see if there wasn't a detailed page talking about my issue
Environment
- version of OHHTTPStubs: [LIB VERSION HERE]
- integration method you are using:
- Cocoapods
- Carthage
- submodule
- other
- version of the tool you use: 6.1.0
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.
func testExample() {
let expectation = self.expectation(description: "")
stub(condition: isMethodGET()) { _ in
return OHHTTPStubsResponse(
jsonObject: [],
statusCode: 333,
headers: [ "Content-Type": "application/json" ]
)
}
// intercepted by OHHTTPStubs
URLSession.shared.dataTask(with: URL(string: "https://www.google.com/")!) { (data, response, error) in
print((response as! HTTPURLResponse).statusCode) // returns 333 as expected
}.resume()
// not intercepted by OHHTTPStubs
let session = URLSession(configuration: URLSession.shared.configuration)
session.dataTask(with: URL(string: "https://www.google.com/")!) { (data, response, error) in
print((response as! HTTPURLResponse).statusCode) // returns 200
}.resume()
waitForExpectations(timeout: 3)
}
Update
It is working with
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: URL(string: "https://www.google.com/")!) { (data, response, error) in
print((response as! HTTPURLResponse).statusCode) // returns 333
}.resume()
Shouldn't let session = URLSession(configuration: URLSessionConfiguration.default)
and let session = URLSession(configuration: URLSession.shared.configuration)
behave the same way?
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:
-
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 -
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.