mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-28 20:55:32 +08:00
Fix bug with status code handling when statusCodes is nil for any response descriptor.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user