Skip to content

Commit

Permalink
Added support for automatically decoding plus symbols in query params (
Browse files Browse the repository at this point in the history
…fixes #87)
  • Loading branch information
joeldev committed Mar 23, 2017
1 parent 676278e commit a8d6f7d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
6 changes: 2 additions & 4 deletions JLRoutes/Classes/JLRRouteDefinition.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ - (JLRRouteResponse *)routeResponseForRequest:(JLRRouteRequest *)request decodeP
if (isMatch) {
// if it's a match, set up the param dictionary and create a valid match response
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params addEntriesFromDictionary:request.queryParams];
[params addEntriesFromDictionary:[request queryParamsDecodingPlusSymbols:decodePlusSymbols]];
[params addEntriesFromDictionary:routeParams];
[params addEntriesFromDictionary:[self baseMatchParametersForRequest:request]];
response = [JLRRouteResponse validMatchResponseWithParameters:[params copy]];
Expand Down Expand Up @@ -135,9 +135,7 @@ - (NSString *)variableValueForValue:(NSString *)value decodePlusSymbols:(BOOL)de
var = [var substringToIndex:var.length - 1];
}

if (decodePlusSymbols) {
var = [var stringByReplacingOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, var.length)];
}
var = [JLRRouteRequest variableValueFrom:var decodePlusSymbols:decodePlusSymbols];

return var;
}
Expand Down
4 changes: 4 additions & 0 deletions JLRoutes/Classes/JLRRouteRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ NS_ASSUME_NONNULL_BEGIN

- (instancetype)initWithURL:(NSURL *)URL;

+ (NSString *)variableValueFrom:(NSString *)value decodePlusSymbols:(BOOL)decodePlusSymbols;

- (NSDictionary *)queryParamsDecodingPlusSymbols:(BOOL)decodePlusSymbols;

@end


Expand Down
36 changes: 36 additions & 0 deletions JLRoutes/Classes/JLRRouteRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ - (instancetype)initWithURL:(NSURL *)URL
return self;
}

+ (NSString *)variableValueFrom:(NSString *)value decodePlusSymbols:(BOOL)decodePlusSymbols;
{
if (!decodePlusSymbols) {
return value;
}
return [value stringByReplacingOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, value.length)];
}

- (NSDictionary *)queryParamsDecodingPlusSymbols:(BOOL)decodePlusSymbols;
{
if (!decodePlusSymbols) {
return self.queryParams;
}

NSMutableDictionary *updatedQueryParams = [NSMutableDictionary dictionary];

for (NSString *name in self.queryParams) {
id value = self.queryParams[name];

if ([value isKindOfClass:[NSArray class]]) {
NSMutableArray *variables = [NSMutableArray array];
for (NSString *arrayValue in (NSArray *)value) {
[variables addObject:[[self class] variableValueFrom:arrayValue decodePlusSymbols:decodePlusSymbols]];
}
updatedQueryParams[name] = [variables copy];
} else if ([value isKindOfClass:[NSString class]]) {
NSString *variable = [[self class] variableValueFrom:value decodePlusSymbols:decodePlusSymbols];
updatedQueryParams[name] = variable;
} else {
NSAssert(NO, @"Unexpected query parameter type: %@", NSStringFromClass([value class]));
}
}

return [updatedQueryParams copy];
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ %p> - URL: %@", NSStringFromClass([self class]), self, [self.URL absoluteString]];
Expand Down
31 changes: 31 additions & 0 deletions JLRoutesTests/JLRoutesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ - (void)setUp
{
[super setUp];
testsInstance = self;
[JLRoutes setShouldDecodePlusSymbols:YES];
}

- (void)tearDown
Expand Down Expand Up @@ -606,12 +607,42 @@ - (void)testDecodePlusSymbols
JLValidateParameterCount(1);
JLValidateParameter(@{@"userID": @"joel levin"});

[self route:@"tests://user/view/joel+levin"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(1);
JLValidateParameter(@{@"userID": @"joel levin"});

[self route:@"tests://user/view/test?name=joel+levin"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(2);
JLValidateParameter(@{@"name": @"joel levin"});

[self route:@"tests://user/view/test?people=joel+levin&people=foo+bar"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(2);
JLValidateParameter((@{@"people": @[@"joel levin", @"foo bar"]}));

[JLRoutes setShouldDecodePlusSymbols:NO];

[self route:@"tests://user/view/joel%2Blevin"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(1);
JLValidateParameter(@{@"userID": @"joel+levin"});

[self route:@"tests://user/view/joel+levin"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(1);
JLValidateParameter(@{@"userID": @"joel+levin"});

[self route:@"tests://user/view/test?name=joel+levin"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(2);
JLValidateParameter(@{@"name": @"joel+levin"});

[self route:@"tests://user/view/test?people=joel+levin&people=foo+bar"];
JLValidateAnyRouteMatched();
JLValidateParameterCount(2);
JLValidateParameter((@{@"people": @[@"joel+levin", @"foo+bar"]}));
}

- (void)testVariableEmptyFollowedByWildcard
Expand Down

0 comments on commit a8d6f7d

Please sign in to comment.