mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-28 12:45:33 +08:00
Normalize the path pattern instead of throwing an assertion
This commit is contained in:
@@ -36,10 +36,11 @@
|
||||
Creates and returns a new `RKResponseDescriptor` object.
|
||||
|
||||
@param mapping The mapping for the response descriptor.
|
||||
@param pathPattern A path pattern that matches against URLs for which the mapping should be used.
|
||||
@param pathPattern A path pattern that matches against URLs for which the mapping should be used. The path pattern will be normalized on input via `RKPathNormalize`.
|
||||
@param keyPath A key path specifying the subset of the parsed response for which the mapping is to be used.
|
||||
@param statusCodes A set of HTTP status codes for which the mapping is to be used.
|
||||
@return A new `RKResponseDescriptor` object.
|
||||
@see `RKPathNormalize`
|
||||
*/
|
||||
+ (RKResponseDescriptor *)responseDescriptorWithMapping:(RKMapping *)mapping
|
||||
pathPattern:(NSString *)pathPattern
|
||||
@@ -58,7 +59,7 @@
|
||||
/**
|
||||
The path pattern to match against the request URL. If nil, the response descriptor matches any URL.
|
||||
|
||||
@see RKPathMatcher
|
||||
@see `RKPathMatcher`
|
||||
*/
|
||||
@property (nonatomic, copy, readonly) NSString *pathPattern;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#import "RKPathMatcher.h"
|
||||
#import "RKResponseDescriptor.h"
|
||||
#import "RKPathUtilities.h"
|
||||
|
||||
// Cloned from AFStringFromIndexSet -- method should be non-static for reuse
|
||||
static NSString *RKStringFromIndexSet(NSIndexSet *indexSet) {
|
||||
@@ -69,11 +70,6 @@ static BOOL RKURLIsRelativeToURL(NSURL *sourceURL, NSURL *baseURL)
|
||||
return [[sourceURL absoluteString] hasPrefix:[baseURL absoluteString]];
|
||||
}
|
||||
|
||||
static BOOL RKPathPatternIsNilOrAbsolute(NSString *pathPattern)
|
||||
{
|
||||
return pathPattern == nil || [pathPattern characterAtIndex:0] == '/';
|
||||
}
|
||||
|
||||
@interface RKResponseDescriptor ()
|
||||
@property (nonatomic, strong, readwrite) RKMapping *mapping;
|
||||
@property (nonatomic, copy, readwrite) NSString *pathPattern;
|
||||
@@ -89,10 +85,9 @@ static BOOL RKPathPatternIsNilOrAbsolute(NSString *pathPattern)
|
||||
statusCodes:(NSIndexSet *)statusCodes
|
||||
{
|
||||
NSParameterAssert(mapping);
|
||||
NSAssert(RKPathPatternIsNilOrAbsolute(pathPattern), @"The given path pattern must be absolute as it will be evaluated against a complete path segment.");
|
||||
RKResponseDescriptor *mappingDescriptor = [self new];
|
||||
mappingDescriptor.mapping = mapping;
|
||||
mappingDescriptor.pathPattern = pathPattern;
|
||||
mappingDescriptor.pathPattern = pathPattern ? RKPathNormalize(pathPattern) : nil;
|
||||
mappingDescriptor.keyPath = keyPath;
|
||||
mappingDescriptor.statusCodes = statusCodes;
|
||||
|
||||
|
||||
@@ -125,6 +125,6 @@ NSString *RKPathNormalize(NSString *path)
|
||||
path = [path stringByReplacingOccurrencesOfString:@"//" withString:@"/"];
|
||||
NSUInteger length = [path length];
|
||||
if ([path characterAtIndex:length - 1] == '/') path = [path substringToIndex:length - 1];
|
||||
if ([path characterAtIndex:0] != '/') path = [@"/" stringByAppendingString:path];
|
||||
if ([path characterAtIndex:0] != '/') path = [NSString stringWithFormat:@"/%@", path];
|
||||
return path;
|
||||
}
|
||||
|
||||
@@ -14,11 +14,10 @@ SPEC_BEGIN(RKResponseDescriptorSpec)
|
||||
|
||||
describe(@"init", ^{
|
||||
context(@"when given a relative path pattern", ^{
|
||||
it(@"raises an exception", ^{
|
||||
[[theBlock(^{
|
||||
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
[RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"monkeys" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
|
||||
}) should] raiseWithName:NSInternalInconsistencyException reason:@"The given path pattern must be absolute as it will be evaluated against a complete path segment."];
|
||||
it(@"normalizes the path pattern", ^{
|
||||
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"monkeys" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
|
||||
[[responseDescriptor.pathPattern should] equal:@"/monkeys"];
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -261,6 +260,24 @@ describe(@"matchesResponse:", ^{
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context(@"when the baseURL is 'http://domain.com/domain/api/v1/'", ^{
|
||||
context(@"and the path pattern is '/recommendation/'", ^{
|
||||
context(@"then given the URL 'http://domain.com/domain/api/v1/recommendation'", ^{
|
||||
beforeEach(^{
|
||||
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:@"/recommendation/" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
|
||||
responseDescriptor.baseURL = [NSURL URLWithString:@"http://domain.com/domain/api/v1/"];
|
||||
});
|
||||
|
||||
it(@"returns YES", ^{
|
||||
NSURL *URL = [NSURL URLWithString:@"http://domain.com/domain/api/v1/recommendation/"];
|
||||
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:URL statusCode:200 HTTPVersion:@"1.1" headerFields:nil];
|
||||
[[@([responseDescriptor matchesResponse:response]) should] beYes];
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
SPEC_END
|
||||
|
||||
Reference in New Issue
Block a user