Normalize the path pattern instead of throwing an assertion

This commit is contained in:
Blake Watters
2012-10-05 16:29:25 -04:00
parent 635b6257d1
commit 17f9a1af8b
4 changed files with 28 additions and 15 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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