Coder Social home page Coder Social logo

User-Agent is missing from `NSURLRequest` in `stubRequestsPassingTest:...` when using `NSURLSessionConfiguraiton`? about ohhttpstubs HOT 6 OPEN

alisoftware avatar alisoftware commented on May 17, 2024
User-Agent is missing from `NSURLRequest` in `stubRequestsPassingTest:...` when using `NSURLSessionConfiguraiton`?

from ohhttpstubs.

Comments (6)

AliSoftware avatar AliSoftware commented on May 17, 2024

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.

AliSoftware avatar AliSoftware commented on May 17, 2024

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.

AliSoftware avatar AliSoftware commented on May 17, 2024

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.

AliSoftware avatar AliSoftware commented on May 17, 2024

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.

rsaunders100 avatar rsaunders100 commented on May 17, 2024

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.

AliSoftware avatar AliSoftware commented on May 17, 2024

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…)

from ohhttpstubs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.