Skip to content

Commit

Permalink
Added an option to control how URL host is treated (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldev committed Apr 8, 2017
1 parent b2e2d24 commit 1af2a1c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion JLRoutes/Classes/JLRRouteRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) NSArray *pathComponents;
@property (nonatomic, strong, readonly) NSDictionary *queryParams;

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

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

Expand Down
4 changes: 2 additions & 2 deletions JLRoutes/Classes/JLRRouteRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ @interface JLRRouteRequest ()

@implementation JLRRouteRequest

- (instancetype)initWithURL:(NSURL *)URL
- (instancetype)initWithURL:(NSURL *)URL alwaysTreatsHostAsPathComponent:(BOOL)alwaysTreatsHostAsPathComponent
{
if ((self = [super init])) {
self.URL = URL;

NSURLComponents *components = [NSURLComponents componentsWithString:[self.URL absoluteString]];

if (components.host.length > 0 && ![components.host isEqualToString:@"localhost"] && [components.host rangeOfString:@"."].location == NSNotFound) {
if (components.host.length > 0 && (alwaysTreatsHostAsPathComponent || (![components.host isEqualToString:@"localhost"] && [components.host rangeOfString:@"."].location == NSNotFound))) {
// convert the host to "/" so that the host is considered a path component
NSString *host = [components.percentEncodedHost copy];
components.host = @"/";
Expand Down
6 changes: 6 additions & 0 deletions JLRoutes/JLRoutes.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ extern NSString *const JLRoutesGlobalRoutesScheme;
/// Returns if '+' should be replaced with spaces in parsed values. Defaults to YES.
+ (BOOL)shouldDecodePlusSymbols;

/// Configures if URL host is always considered to be a path component. Defaults to NO.
+ (void)setAlwaysTreatsHostAsPathComponent:(BOOL)treatsHostAsPathComponent;

/// Returns if URL host is always considered to be a path component. Defaults to NO.
+ (BOOL)alwaysTreatsHostAsPathComponent;

@end


Expand Down
13 changes: 12 additions & 1 deletion JLRoutes/JLRoutes.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// global options
static BOOL verboseLoggingEnabled = NO;
static BOOL shouldDecodePlusSymbols = YES;
static BOOL alwaysTreatsHostAsPathComponent = NO;


@interface JLRoutes ()
Expand Down Expand Up @@ -258,7 +259,7 @@ - (BOOL)_routeURL:(NSURL *)URL withParameters:(NSDictionary *)parameters execute
[self _verboseLog:@"Trying to route URL %@", URL];

BOOL didRoute = NO;
JLRRouteRequest *request = [[JLRRouteRequest alloc] initWithURL:URL];
JLRRouteRequest *request = [[JLRRouteRequest alloc] initWithURL:URL alwaysTreatsHostAsPathComponent:alwaysTreatsHostAsPathComponent];

for (JLRRouteDefinition *route in [self.mutableRoutes copy]) {
// check each route for a matching response
Expand Down Expand Up @@ -356,6 +357,16 @@ + (BOOL)shouldDecodePlusSymbols
return shouldDecodePlusSymbols;
}

+ (void)setAlwaysTreatsHostAsPathComponent:(BOOL)treatsHostAsPathComponent
{
alwaysTreatsHostAsPathComponent = treatsHostAsPathComponent;
}

+ (BOOL)alwaysTreatsHostAsPathComponent
{
return alwaysTreatsHostAsPathComponent;
}

@end


Expand Down
31 changes: 28 additions & 3 deletions JLRoutesTests/JLRoutesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ - (void)setUp

// reset settings
[JLRoutes setShouldDecodePlusSymbols:YES];
[JLRoutes setAlwaysTreatsHostAsPathComponent:NO];
}

- (void)tearDown
Expand Down Expand Up @@ -775,10 +776,14 @@ - (void)testAddingCustomRouteDefinition
JLValidateScheme(JLRoutesGlobalRoutesScheme);
}

- (void)testShouldTreatHostAsPathComponent
- (void)testTreatsHostAsPathComponent
{
[[JLRoutes globalRoutes] addRoute:@"/sign_in" handler:[[self class] defaultRouteHandler]];
[[JLRoutes globalRoutes] addRoute:@"/path/:pathid" handler:[[self class] defaultRouteHandler]];
id defaultHandler = [[self class] defaultRouteHandler];

[[JLRoutes globalRoutes] addRoute:@"/sign_in" handler:defaultHandler];
[[JLRoutes globalRoutes] addRoute:@"/path/:pathid" handler:defaultHandler];

[JLRoutes setAlwaysTreatsHostAsPathComponent:NO];

[self route:@"https://www.mydomain.com/sign_in"];
JLValidateAnyRouteMatched();
Expand All @@ -788,6 +793,26 @@ - (void)testShouldTreatHostAsPathComponent
JLValidateAnyRouteMatched();
JLValidatePattern(@"/path/:pathid");
JLValidateParameter((@{@"pathid": @"3"}));

[JLRoutes setAlwaysTreatsHostAsPathComponent:YES];

[self route:@"https://www.mydomain2.com/sign_in"];
JLValidateNoLastMatch();

[self route:@"https://www.mydomain2.com/path/3"];
JLValidateNoLastMatch();

[[JLRoutes globalRoutes] addRoute:@"/www.mydomain2.com/sign_in" handler:defaultHandler];
[[JLRoutes globalRoutes] addRoute:@"/www.mydomain2.com/path/:pathid" handler:defaultHandler];

[self route:@"https://www.mydomain2.com/sign_in"];
JLValidateAnyRouteMatched();
JLValidatePattern(@"/www.mydomain2.com/sign_in");

[self route:@"https://www.mydomain2.com/path/3"];
JLValidateAnyRouteMatched();
JLValidatePattern(@"/www.mydomain2.com/path/:pathid");
JLValidateParameter((@{@"pathid": @"3"}));
}

#pragma mark - Convenience Methods
Expand Down

0 comments on commit 1af2a1c

Please sign in to comment.