user2782745
user2782745

Reputation: 41

Cocoa Error 3840 on AFNetworking

I am writing a unit test case with AFNetworking 2.0 and OHHTTPStubs, and the test always failed with these errors:

Response error:The operation couldn’t be completed. (Cocoa error 3840.).

Thanks a lot!

Below is the simple test Json (User.json): { "userId": "abc", "email": "[email protected]", "username": "usera" }

and the test case codes:

- (void)testGet
{
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES; // Stub ALL requests without any condition
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"User.json",nil)
            statusCode:200 headers:@{@"Content-Type":@"application/json"}];
    }];

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    AFHTTPSessionManager* requestManager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
    AFJSONResponseSerializer* responseSerializer = [[[AFJSONResponseSerializer alloc]init] autorelease];
    responseSerializer.readingOptions = NSJSONReadingAllowFragments;
    responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 2)];
    requestManager.responseSerializer = responseSerializer;

    AFJSONRequestSerializer* requestSerializer = [[AFJSONRequestSerializer alloc] init];
    requestSerializer.writingOptions = NSJSONWritingPrettyPrinted;
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    requestManager.requestSerializer = requestSerializer;

    _done = NO;
    [requestManager GET:@"stream" parameters:nil success:^(NSURLSessionDataTask * task, id JSON)
    {
        NSLog(@"Response data:%@", JSON);
        XCTAssert(JSON != nil, @"null response");
        dispatch_semaphore_signal(semaphore);

    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        NSLog(@"Response error:%@", [error localizedDescription]);
        XCTFail(@"fail to get response");
        dispatch_semaphore_signal(semaphore);
    }];

    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    }
}

Upvotes: 4

Views: 1726

Answers (1)

haik.ampardjian
haik.ampardjian

Reputation: 864

Try to use AFHTTPResponseSerializer instead. So basically, replace this code:

    AFJSONResponseSerializer* responseSerializer = [[[AFJSONResponseSerializer alloc]init] autorelease];
    responseSerializer.readingOptions = NSJSONReadingAllowFragments;
    responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 2)];
    requestManager.responseSerializer = responseSerializer;

to this one:

    requestManager.responseSerializer = [AFHTTPResponseSerializer serializer];

Because you're using acceptableContentType of @"text/html", I assume you don't need a JSON serializer on response call. So this should be fine for your case.

Upvotes: 0

Related Questions