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

Request from NSURLSessionDataTask not passing stub's pass-everything pass block #48

Closed
brennon opened this issue Jan 9, 2014 · 4 comments

Comments

@brennon
Copy link

brennon commented Jan 9, 2014

Here's a fun one. I'm creating a stub to catch an upload request that I create using AFNetworking's -[NSURLSessionManager uploadTaskWithRequest:fromData:progress:completionHandler:] method. Here's my stub:

[OHHTTPStubs
 stubRequestsPassingTest:^BOOL(NSURLRequest *request)
 {
     return [request.URL.lastPathComponent isEqualToString:@"addSplash.php"];
 }
 withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
     NSDictionary *standardHeaders = @{ @"Content-Type":@"text/html"};
     return [OHHTTPStubsResponse responseWithData:nil statusCode:200 headers:standardHeaders];
 }];

Here's my test:

it(@"should set isUploading to YES", ^{
    client.isUploading = NO;
    [client sendNewSplashData:[NSData data] withLabel:@"SplashString" forLocation:[[CLLocation alloc] initWithLatitude:37 longitude:-80]];
    expect(client.isUploading).to.beTruthy();
});

And here's that -sendNewSplashData... method:

- (void)sendNewSplashData:(NSData *)splashData withLabel:(NSString *)label forLocation:(CLLocation *)location {

    self.isUploading = YES;

    long clientID = [[NSUserDefaults standardUserDefaults] integerForKey:@"clientID"];

    // Build request body
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSMutableData *body = [NSMutableData data];

    // Add body data
    // <EXCERPTED>

    // Build request with body
    NSURL *url = [self.baseURL URLByAppendingPathComponent:@"addSplash.php"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSProgress *progress;
    NSURLSessionDataTask *task = [self uploadTaskWithRequest:request fromData:body progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        self.isUploading = NO;
    }];
    [task resume];
    [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
}

When I run the test, an exception is thrown in -[OHHTTPStubs startLoading] at this assertion:

NSAssert(stub, @"At the time startLoading is called, canInitRequest should have assured that stub is != nil beforehand");

Back in the stub, if I simply return YES in the request test block so that all requests pass, this exception is still thrown. If I place a breakpoint in this block and look at request, I see that two requests hit the block--the one I expect, and a nil request. Strangely, when I place a breakpoint, run the test, hit the breakpoint, and continue in the debugger through each request, the test passes and no exception is thrown.

@brennon
Copy link
Author

brennon commented Jan 9, 2014

I see now that if I remove the stub after all tests, instead of after each test (even though there is only one test!), I have no problems running this test.

@AliSoftware
Copy link
Owner

That's indeed a strange one! I'll try to find some time next week to investigate a bit why you have a nil request (even if I'm not sure I have any leverage on this, as this is the responsibility of the OS itself).

Anyway, please note that as explained here in the README, OHHTTPStubs uses NSURLProtocolClient to catch the network requests and this Apple class does not allow us to catch upload requests, so unfortunately those can't be stubbed.

@AliSoftware
Copy link
Owner

This last issue is maybe related to #51 (as everything causing this assertion to be thrown is likely to be caused by a thread race-condition)?

@AliSoftware
Copy link
Owner

I'll assume this is a duplicate of #51 and close this one. Please continue any related discussion on issue #51 :)

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

2 participants