Comments (6)
I believe this is because NSURLSession adds the HTTPAdditionalHeaders at the very end just before sending the request to the network, so you are querying them too soon?
Will try and investigate but I'm not sure it has anything to do with OHHTTPStubs code itself, but more to do with the way Cocoa URL Loading system handles this property and when it uses it to add the headers. Will keep you posted.
from ohhttpstubs.
Unfortunately I just confirmed that it seems to be iOS's fault.
(1) I created a NSURLSessionConfiguration and NSURLSession as in your code:
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = @{ @"User-Agent": @"Test User Agent" };
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
(2) Then sent a request with this session:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"foo://unknownhost:666"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"Completed");
}
(3) I set a breakpoint in my NSURLProtocol
subclass, right in the very beginning when it gets the request passed on, so in the implementation of - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client
method, and dumped request.allHTTPHeaderFields
there.
At that time, OHHTTPStubs
has no control: it's iOS itself (the URL Loading System mechanism handled by iOS and the underlying stack inside Cocoa) that calls this NSURLProtocol
's method and pass it the request before it is sent. And as I have just tested, at that time the request that iOS passed on to me does not contain the HTTPAdditionalHeaders
when dumping allHTTPHeaderFields
.
So unfortunately this issue does not seem to be coming from my own code (where I could have had overridden the headers maybe, in that case I could have had that fixed) but from iOS itself
PS: Probably this HTTPAdditionalHeaders
property is only used privately by the private implementation of the HTTPURLProtocol that is in charge of usually processing HTTP requests to send them to the network, and custom NSURLProtocols
don't have access to such properties (NSURLProtocol
does not have access to the NSURLSession
that handles the NSURLRequest
thus I cannot even recreate/re-implment this and take that -[NSURLSession.configuration HTTPAdditionalHeaders]
property into account
If you have any suggestion on how to hack into that, I'm interested.
from ohhttpstubs.
Hi again,
I've started working on a branch to workaround this issue.
Feel free to test this branch in your code and tell me if it fixes your issue. If so I'll cleanup the code and ensure it's portable (even for those using older SDKs/versions or still using NSURLConnection
etc) and include it in the next release.
from ohhttpstubs.
I just discovered a very strange behavior:
- when testing on iOS7, the additional headers specified in the session configuration are added.
- they are missing only on iOS8!
@rsaunders100 Do you observe the same behavior on your app?
This comforts me in the fact that this is an iOS bug. I encourage you to fill a radar / bug report to Apple.
(I'll probably continue working on the branch with the trick that allows me to access the NSURLSessionConfiguration
so that I can maybe provide a workaround, but this still seems to be an Apple issue that should be addressed by Apple itself)
from ohhttpstubs.
Awesome stuff. I'll do that.
Sent from my iPhone
On 27 Jan 2015, at 21:09, AliSoftware [email protected] wrote:
I just discovered a very strange behavior: when testing on iOS7, the additional headers specified in the session configuration are added. They are missing only on iOS8! (Comforting me in the fact that this is an iOS bug)
I encourage you to fill a radar / bug report to Apple.
β
Reply to this email directly or view it on GitHub.
from ohhttpstubs.
Do you still have the issue with iOS8.1, 8.2 and 8.3?
Or has Apple fixed the issue since then?
[EDIT] Ok nvm, I misread the tests I recently added; the issue is still present in iOS8.3, still trying to find a workaround for this Apple bug (but it's quite hard as we don't have access to the NSURLSessionConfiguration
from the NSURLProtocol
and the NSURLProtocol (NSURLSessionTaskAdditions)
category, even if declared in NSURLProtocol.h
, does not seem to be implemented at all by Appleβ¦)
from ohhttpstubs.
Related Issues (20)
- Unable to import OHHTTPStubsSwift from swift package manager dependency HOT 2
- <!-- Thanks for taking the time to report your issue with OHHTTPStubs! When submitting your issue, please make sure to check the following boxes by putting an x in each appropriate [ ] so we can fully understand the context of your problem and help you better -->
- <!-- Thanks for taking the time to report your issue with OHHTTPStubs! When submitting your issue, please make sure to check the following boxes by putting an x in each appropriate [ ] so we can fully understand the context of your problem and help you better -->
- Xcode 12 - Warnings related to iOS 8 support (Swift Package Manager) HOT 6
- Mocktail support HOT 2
- pod 'AFNetworking', '~> 3.0'
- Incorrect example for onStubActivation on Usage Examples page
- [wiki] OHHTTPStubsResponse to HTTPStubsResponse with Swift HOT 4
- Module 'OHHTTPStubs' has no member named 'removeAllStubs' in 9.0.0 HOT 2
- Xcode Analyze: Logic error
- N HOT 1
- Travis CI has seemed to be moved. HOT 3
- How to use in a UITest HOT 9
- OHHTTPStubs with SPM in Xcode 12.5 HOT 1
- Fluent API for stubbing
- how do i use it HOT 1
- Liscense HOT 1
- 4 da world HOT 1
- - [ ] Submitting a maybeaplle @mtodd []()![image](https://user-images.githubusercontent.com/122186144/230750410-f43c3ffb-f6a7-4bd4-9970-8b065670ea28.jpeg)@craystyle1212
- Won't build with Carthage and Xcode 14.3 HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ohhttpstubs.