Use path normalization instead of prefix matching to solve issue with failure to match path patterns with trailing slashes. Expand tests

This commit is contained in:
Blake Watters
2012-10-05 13:46:52 -04:00
parent 61a0a05b29
commit fb7f074b23
5 changed files with 79 additions and 3 deletions

View File

@@ -22,6 +22,7 @@
#import "SOCKit.h"
#import "RKLog.h"
#import "RKDictionaryUtilities.h"
#import "RKPathUtilities.h"
static NSString *RKEncodeURLString(NSString *unencodedString);
extern NSDictionary *RKQueryParametersFromStringWithEncoding(NSString *string, NSStringEncoding stringEncoding);
@@ -59,7 +60,6 @@ static NSString *RKEncodeURLString(NSString *unencodedString)
return copy;
}
+ (RKPathMatcher *)pathMatcherWithPattern:(NSString *)patternString
{
NSAssert(patternString != NULL, @"Pattern string must not be empty in order to perform pattern matching.");
@@ -77,11 +77,15 @@ static NSString *RKEncodeURLString(NSString *unencodedString)
return matcher;
}
// Normalize our root path for matching cleanly with SOCKit
- (void)setRootPath:(NSString *)rootPath
{
_rootPath = RKPathNormalize(rootPath);
}
- (BOOL)matches
{
NSAssert((self.socPattern != NULL && self.rootPath != NULL), @"Matcher is insufficiently configured. Before attempting pattern matching, you must provide a path string and a pattern to match it against.");
// If the pattern is an exact prefix of the matched string, we evaluate to YES
if ([self.rootPath hasPrefix:self.patternString]) return YES;
return [self.socPattern stringMatches:self.rootPath];
}

View File

@@ -51,6 +51,15 @@ NSString *RKPathFromPatternWithObject(NSString *pathPattern, id object);
For example, given a string with the path `@"/Users/blake/Documents/monkey.json"` `@"application/json"` would be returned as the MIME Type.
@param path The path to return the MIME Type for.
@return The expected MIME Type of the resource identified by the path or nil if unknown.
*/
NSString *RKMIMETypeFromPathExtension(NSString *path);
/**
Normalizes a given string into a path by ensuring that it includes a leading slash and does not include a trailing slash.
@param path The path to be normalized.
@return The given path, normalized with a leading slash and without a trailing sla
*/
NSString *RKPathNormalize(NSString *path);

View File

@@ -119,3 +119,12 @@ NSString *RKMIMETypeFromPathExtension(NSString *path)
// Consult our internal dictionary of mappings if not found
return [RKDictionaryOfFileExtensionsToMIMETypes() valueForKey:pathExtension];
}
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];
return path;
}