Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to replace a previosly defined route #43

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Criollo/Source/Routing/CRRoute.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,19 @@ - (instancetype)initWithStaticFileAtPath:(NSString *)filePath options:(CRStaticF
}];
return result;
}

- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[CRRoute class]]) {
CRRoute *route = (CRRoute *)object;

return _method == route.method &&
[_path isEqualToString:route.path] &&
_recursive == route.recursive ;
} else {
return false;
}
}



@end
8 changes: 8 additions & 0 deletions Criollo/Source/Routing/CRRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)mount:(NSString *)path fileAtPath:(NSString *)filePath options:(CRStaticFileServingOptions)options fileName:(NSString * _Nullable)fileName contentType:(NSString * _Nullable)contentType contentDisposition:(CRStaticFileContentDisposition)contentDisposition;

/**
Replaces a block for a pathspec, for all HTTP methods, non-recursively. If there is no block defined for the specified path, the new block will be added.

@param path The path specification.
@param block The `CRRouteBlock` to be executed.
*/
- (void)replace:(NSString * _Nullable)path block:(CRRouteBlock)block;

@end

NS_ASSUME_NONNULL_END
12 changes: 12 additions & 0 deletions Criollo/Source/Routing/CRRouter.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ - (void)mount:(NSString *)path fileAtPath:(NSString *)filePath options:(CRStatic
CRRoute* route = [[CRRoute alloc] initWithStaticFileAtPath:filePath options:options fileName:fileName contentType:contentType contentDisposition:contentDisposition path:path];
[self addRoute:route];
}

- (void)replace:(NSString * _Nullable)path block:(CRRouteBlock)block {
CRRoute* route = [[CRRoute alloc] initWithBlock:block method:CRHTTPMethodAll path:path recursive:NO];

NSUInteger index = [self.routes indexOfObject:route];

if (index != NSNotFound) {
[self.routes removeObjectAtIndex:index];
}

[self addRoute:route];
}


#pragma mark - Routing
Expand Down
25 changes: 25 additions & 0 deletions CriolloTests/CRRouterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,30 @@ - (void)testPlaceholderRoutes {
XCTAssertTrue([expectedFoo isEqualToString:foo]);
}
}

- (void)testReplacingRoutes {
CRRouteBlock block = ^(CRRequest * _Nonnull request, CRResponse * _Nonnull response, CRRouteCompletionBlock _Nonnull completionHandler){};
CRRouter *router = [[CRRouter alloc] init];
NSString *testPath = @"/routes/test";
CRRoute *route = [[CRRoute alloc] initWithBlock:block method:CRHTTPMethodAll path:testPath recursive:NO];
[router addRoute:route];

NSArray<CRRouteMatchingResult *> *matches = [router routesForPath:testPath method:CRHTTPMethodGet];

XCTAssertNotNil(matches);
XCTAssertEqual(1, matches.count);
XCTAssertEqual(matches.firstObject.route.block, block);

CRRouteBlock newBlock = ^(CRRequest * _Nonnull request, CRResponse * _Nonnull response, CRRouteCompletionBlock _Nonnull completionHandler){};

[router replace:testPath block:newBlock];

matches = [router routesForPath:testPath method:CRHTTPMethodGet];

XCTAssertNotNil(matches);
XCTAssertEqual(1, matches.count);
XCTAssertNotEqual(matches.firstObject.route.block, block);
XCTAssertEqual(matches.firstObject.route.block, newBlock);
}

@end