OHHTTPStubs
OHHTTPStubs copied to clipboard
User-Agent is missing from `NSURLRequest` in `stubRequestsPassingTest:...` when using `NSURLSessionConfiguraiton`?
I am setting up the userAgent string using NSURLSessionConfiguraiton:
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = @{ @"User-Agent": @"Test User Agent" };
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
The User-Agent is being sent over the network - I can see it using Charles Proxy. However, it is not coming though in the stub checker:
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
NSDictionary* headers = request.allHTTPHeaders;
// `headers` does not contain the user agent entry
return YES;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
return nil;
}];
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.
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 :disappointed:
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 :disappointed:
If you have any suggestion on how to hack into that, I'm interested.
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.
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)
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.
Hi @rsaunders100
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…)