Skip to content

feat(dart_frog): expose captured request params #1818

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

Merged
merged 4 commits into from
Aug 16, 2025
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
6 changes: 6 additions & 0 deletions packages/dart_frog/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Request {
);
}

/// Returns the url parameters captured by the [Router].
/// Returns an empty map if no parameters are captured.
///
/// The returned map is unmodifiable.
Map<String, String> get params => _request.params;

/// Returns a [Stream] representing the body.
Stream<List<int>> bytes() => _request.read();

Expand Down
9 changes: 9 additions & 0 deletions packages/dart_frog/test/src/request_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -79,6 +80,14 @@ ${HttpMethod.values.map((m) => m.value.toUpperCase()).join(', ')}.'''),
expect(request.headers['foo'], equals(headers['foo']));
});

test('has correct params (empty)', () {
final request = Request('GET', localhost);
expect(
request.params,
equals(UnmodifiableMapView<String, String>(const {})),
);
});

test('body can be read multiple times (sync)', () {
final body = json.encode({'test': 'body'});
final request = Request('GET', localhost, body: body);
Expand Down
83 changes: 82 additions & 1 deletion packages/dart_frog/test/src/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@TestOn('vm')
library;

import 'dart:collection';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -291,10 +292,90 @@ void main() {
expect(response.body, equals('hello'));
});

test('request exposes captured params (empty)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/hello', (RequestContext context) {
expect(
context.request.params,
equals(UnmodifiableMapView<String, String>(const {})),
);
return Response(body: 'hello world');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response = await http.get(Uri.parse('${server.url}/hello'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello world'));
});

test('request exposes captured params (single)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/users/<id>/greet', (RequestContext context, String id) {
expect(
context.request.params,
equals(UnmodifiableMapView<String, String>({'id': id})),
);
return Response(body: 'hello $id');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response = await http.get(Uri.parse('${server.url}/users/42/greet'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello 42'));
});

test('request exposes captured params (multiple)', () async {
final context = _MockRequestContext();
final app = Router()
..get('/users/<id>/greet/<name>', (
RequestContext context,
String id,
String name,
) {
expect(
context.request.params,
equals(UnmodifiableMapView<String, String>({'id': id, 'name': name})),
);
return Response(body: 'hello $name ($id)');
});

server.mount((request) async {
when(() => context.request).thenReturn(
Request(request.method, request.requestedUri),
);
final response = await app(context);
final body = await response.body();
return shelf.Response(response.statusCode, body: body);
});

final response =
await http.get(Uri.parse('${server.url}/users/42/greet/felangel'));
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body, equals('hello felangel (42)'));
});

test('mount(Router)', () async {
final context = _MockRequestContext();
final api = Router()
..all('/user/<user>/info', (RequestContext request, String user) {
..all('/user/<user>/info', (RequestContext context, String user) {
return Response(body: 'Hello $user');
});

Expand Down