Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS7 NSURLSessionDataTask support #31

Closed
johanforssell opened this issue Sep 18, 2013 · 8 comments
Closed

iOS7 NSURLSessionDataTask support #31

johanforssell opened this issue Sep 18, 2013 · 8 comments

Comments

@johanforssell
Copy link

The library is not working with AFNetworking 2.0.0 RC3, which uses the new NSURLSessionDataTask. Are there any plans on getting the library to work with the new NSURLSession-style library?

Here's an example which is not working

#import <XCTest/XCTest.h>
#import <OHHTTPStubs/OHHTTPStubs.h>
#import <AFNetworking/AFHTTPSessionManager.h>

// A couple of macros for dealing with async blocks
#define TestNeedsToWaitForBlock() __block BOOL blockFinished = NO
#define BlockFinished() blockFinished = YES
#define WaitForBlock() while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)


@interface NSURLSessionDataTaskStubTest : XCTestCase
@end


@implementation NSURLSessionDataTaskStubTest

- (void)testFailingExample
{
    // Setup stub
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES;
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {

        NSString * json_str = @"{ \"suggestions\" : \"schmugestion\" }";

        NSData* stubData = [json_str dataUsingEncoding:NSUTF8StringEncoding];
        return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:nil];
    }];


    TestNeedsToWaitForBlock();

    // Make the http call
    AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:9494"]];
    NSMutableURLRequest *request = [client.requestSerializer requestWithMethod:@"GET" URLString:@"/public/suggestions/search" parameters:nil];

    NSURLSessionDataTask *task = [client dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        XCTAssertNil(error, @"Should be no error if the stubbing worked");
        BlockFinished();
    }];

    // Start getting data and wait for the block to be called
    [task resume];
    WaitForBlock();

    [OHHTTPStubs removeAllStubs];
}

@end
@AliSoftware
Copy link
Owner

Thanks for the feature request and the test 👍
This is definitely in the road map to make it work with AFN 2.0 🍻

For now my work is focused on finishing my big refactoring in the inputstrem branch (see my 2.2.x-RC tags / versions, but it seems you are using them already as you use the new removeAllStubs API 😃) which uses NSInputStream objects instead of NSData for a big memory and performance improvement (lazy loading of stubs, etc) but did a lot of internal changes so I have to make sure everything is fine with it first.

I am quite confident in those RC versions, but the only thing I miss in this branch is adding a bunch of missing Unit Tests to be sure my test coverage is good enough and that this refactoring is good to be merged in the master.

As soon as my big inputstrem (yeah I know I missed an "a" in the branch name ^^) contains the missing tests and then merged into master, I'll have to work on making OHHTTPStubs compatible with AFN 2.0 right away 👍
(Actually, feel free to propose some Pull Requests to help me add those missing Unit Tests here and be done with this refactoring so I can hit version 3.0 sooner 😉)

@jkrall
Copy link

jkrall commented Oct 4, 2013

+1 for getting OHHTTPStubs working with NSURLSessionDataTask and AFNetworking 2.0 ... unfortunately, we can't use this library on our project until that lands.

How can I help?

@jkrall
Copy link

jkrall commented Oct 4, 2013

BTW... just found this fork: ndonald2@107faaa

Looks like it works great. (though it does require that you inject the sessionConfiguration into the creating of the AFHTTPSessionManager, or the underlying NSURLSession... see the test coverage on that fork's commit)

@AliSoftware
Copy link
Owner

@jkrall It's still in my roadmap 😉

Any help appreciated, you could for example at least propose a Pull Request containing at least the Unit Test quoted by @johanforssell — but using my AsyncSenTestCase subclass and SenTestKit instead of XCTest and his macros, to be consistent with my other Unit Tests on OHHTTPStubs.

I haven't started investigating yet (I hope I'll have time this weekend), if anyone has pointers on why this fails, it could save me some time too 👍 (Haven't even had time to look at iOS7's NSURLSession new mechanism 😟 )

@AliSoftware
Copy link
Owner

Thx for the pointer on this fork! Will definitely have a look at @ndonald2 's work

@ndonald2
Copy link
Contributor

ndonald2 commented Oct 4, 2013

I will try to get a pull request in sometime today.

As @jkrall mentioned there are a few caveats. NSURLSession handles NSURLProtocol via its NSURLSessionConfiguration, which must be configured before initializing the session, regardless of whether using AFNetworking or vanilla NSURLSession - it won't work if you change the configuration later.

Also, the static library for OHHTTPStubs is targeting 5.0, so the new stuff I added won't even be compiled unless that is changed to 7.0 for a particular project (I wrapped it in an #ifdef for min required > 7.0). I debated whether to do that or not, but I figured that for the few months people may still be building against the 6.0 SDK. That's how AFNetworking handles the NSURLSession additions, too, so it seems to be standard practice.

@AliSoftware
Copy link
Owner

Thanks @ndonald2 it helps a lot!

See #34 for some other comments and the continuation of this discussion

@AliSoftware
Copy link
Owner

Done.

Version automatically-compatible with NSURLSession (with the caveat mentioned by @ndonald2 above thanks to swizzling magic, so not even the need to configure your NSURLSessionConfiguration for that, your existing code should work as is) published as 2.4.0.

Only caveat (but unsolvable) is that OHHTTPStubs won't work with background session, due to the intrisic nature of those sessions and the fact that they don't support custom NSURLProtocols. See README for more details)


Version 3.0.0 released as well — and available thru CocoaPods too, don't forget to update your Podfiles — which is the same as 2.4.0 but with all old & deprecated methods removed (at last!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants