Skip to content
Merged
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
4 changes: 4 additions & 0 deletions lib/src/router/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ extension RouteEx<T extends Object> on Router<T> {
attach(path, subRouter);
return subRouter;
}

/// Injects [injectable] at [path]
void injectAt(final String path, final InjectableIn<Router<T>> injectable) =>
group(path).inject(injectable);
}

/// Just a typedef for better auto-complete
Expand Down
20 changes: 20 additions & 0 deletions test/router/router_inject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ void main() {
});
});

group('Router.injectAt', () {
test('Given a HandlerObject, '
'when using injectAt, '
'then it is injected at the specified path', () async {
final router = Router<Handler>();
router.injectAt('/api', const _EchoHandlerObject(mountAt: '/echo'));

final request = Request(
Method.post,
Uri.parse('http://localhost/api/echo'),
body: Body.fromString('injected at path'),
);
final ctx = request.toContext(Object());
final result = await router.asHandler(ctx) as ResponseContext;

expect(result.response.statusCode, 200);
expect(await result.response.readAsString(), 'injected at path');
});
});

group('Router.inject with MiddlewareObject', () {
test(
'Given a MiddlewareObject, '
Expand Down