Fix bug with status code handling when statusCodes is nil for any response descriptor.

This commit is contained in:
Blake Watters
2013-01-16 13:57:24 -05:00
parent c26739ce6b
commit a8f3887b26
2 changed files with 37 additions and 2 deletions

View File

@@ -60,12 +60,14 @@ static inline NSString *RKDescriptionForRequest(NSURLRequest *request)
static NSIndexSet *RKAcceptableStatusCodesFromResponseDescriptors(NSArray *responseDescriptors)
{
// If there are no response descriptors or any descriptor matches any status code (expressed by `statusCodes` == `nil`) then we want to accept anything
if ([responseDescriptors count] == 0 || [[responseDescriptors valueForKey:@"statusCodes"] containsObject:[NSNull null]]) return nil;
NSMutableIndexSet *acceptableStatusCodes = [NSMutableIndexSet indexSet];
[responseDescriptors enumerateObjectsUsingBlock:^(RKResponseDescriptor *responseDescriptor, NSUInteger idx, BOOL *stop) {
[acceptableStatusCodes addIndexes:responseDescriptor.statusCodes];
}];
// If there are no indexes specified in the response descriptors, then we want to aceept anything
return [acceptableStatusCodes count] ? acceptableStatusCodes : nil;
return acceptableStatusCodes;
}
static NSString *RKStringForStateOfObjectRequestOperation(RKObjectRequestOperation *operation)

View File

@@ -617,4 +617,37 @@
[mockOperation verify];
}
- (void)testMappingErrorsFromFiveHundredStatusCodeRange
{
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError);
RKObjectMapping *errorResponseMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorResponseMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorResponseMapping pathPattern:nil keyPath:@"errors" statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/fail" relativeToURL:[RKTestFactory baseURL]]];
RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[requestOperation start];
[requestOperation waitUntilFinished];
expect(requestOperation.error).willNot.beNil();
expect([requestOperation.error localizedDescription]).to.equal(@"error1, error2");
}
- (void)testMappingErrorsWithNilStatusCodesAndTwoHundredDescriptorRegistered
{
RKObjectMapping *errorResponseMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorResponseMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorResponseMapping pathPattern:nil keyPath:@"errors" statusCodes:nil];
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTestComplexUser class]];
RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:nil keyPath:@"user" statusCodes:[NSIndexSet indexSetWithIndex:200]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/fail" relativeToURL:[RKTestFactory baseURL]]];
RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ userDescriptor, errorDescriptor ]];
[requestOperation start];
[requestOperation waitUntilFinished];
expect(requestOperation.error).willNot.beNil();
expect([requestOperation.error localizedDescription]).to.equal(@"error1, error2");
}
@end