Skip to content

Commit 1d8fa19

Browse files
authored
feat(dart_frog): expose captured request params (#1818)
1 parent 0bf6bc6 commit 1d8fa19

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

packages/dart_frog/lib/src/request.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ class Request {
135135
);
136136
}
137137

138+
/// Returns the url parameters captured by the [Router].
139+
/// Returns an empty map if no parameters are captured.
140+
///
141+
/// The returned map is unmodifiable.
142+
Map<String, String> get params => _request.params;
143+
138144
/// Returns a [Stream] representing the body.
139145
Stream<List<int>> bytes() => _request.read();
140146

packages/dart_frog/test/src/request_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:collection';
23
import 'dart:convert';
34
import 'dart:io';
45

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

83+
test('has correct params (empty)', () {
84+
final request = Request('GET', localhost);
85+
expect(
86+
request.params,
87+
equals(UnmodifiableMapView<String, String>(const {})),
88+
);
89+
});
90+
8291
test('body can be read multiple times (sync)', () {
8392
final body = json.encode({'test': 'body'});
8493
final request = Request('GET', localhost, body: body);

packages/dart_frog/test/src/router_test.dart

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@TestOn('vm')
1717
library;
1818

19+
import 'dart:collection';
1920
import 'dart:convert';
2021
import 'dart:io';
2122

@@ -291,10 +292,90 @@ void main() {
291292
expect(response.body, equals('hello'));
292293
});
293294

295+
test('request exposes captured params (empty)', () async {
296+
final context = _MockRequestContext();
297+
final app = Router()
298+
..get('/hello', (RequestContext context) {
299+
expect(
300+
context.request.params,
301+
equals(UnmodifiableMapView<String, String>(const {})),
302+
);
303+
return Response(body: 'hello world');
304+
});
305+
306+
server.mount((request) async {
307+
when(() => context.request).thenReturn(
308+
Request(request.method, request.requestedUri),
309+
);
310+
final response = await app(context);
311+
final body = await response.body();
312+
return shelf.Response(response.statusCode, body: body);
313+
});
314+
315+
final response = await http.get(Uri.parse('${server.url}/hello'));
316+
expect(response.statusCode, equals(HttpStatus.ok));
317+
expect(response.body, equals('hello world'));
318+
});
319+
320+
test('request exposes captured params (single)', () async {
321+
final context = _MockRequestContext();
322+
final app = Router()
323+
..get('/users/<id>/greet', (RequestContext context, String id) {
324+
expect(
325+
context.request.params,
326+
equals(UnmodifiableMapView<String, String>({'id': id})),
327+
);
328+
return Response(body: 'hello $id');
329+
});
330+
331+
server.mount((request) async {
332+
when(() => context.request).thenReturn(
333+
Request(request.method, request.requestedUri),
334+
);
335+
final response = await app(context);
336+
final body = await response.body();
337+
return shelf.Response(response.statusCode, body: body);
338+
});
339+
340+
final response = await http.get(Uri.parse('${server.url}/users/42/greet'));
341+
expect(response.statusCode, equals(HttpStatus.ok));
342+
expect(response.body, equals('hello 42'));
343+
});
344+
345+
test('request exposes captured params (multiple)', () async {
346+
final context = _MockRequestContext();
347+
final app = Router()
348+
..get('/users/<id>/greet/<name>', (
349+
RequestContext context,
350+
String id,
351+
String name,
352+
) {
353+
expect(
354+
context.request.params,
355+
equals(UnmodifiableMapView<String, String>({'id': id, 'name': name})),
356+
);
357+
return Response(body: 'hello $name ($id)');
358+
});
359+
360+
server.mount((request) async {
361+
when(() => context.request).thenReturn(
362+
Request(request.method, request.requestedUri),
363+
);
364+
final response = await app(context);
365+
final body = await response.body();
366+
return shelf.Response(response.statusCode, body: body);
367+
});
368+
369+
final response =
370+
await http.get(Uri.parse('${server.url}/users/42/greet/felangel'));
371+
expect(response.statusCode, equals(HttpStatus.ok));
372+
expect(response.body, equals('hello felangel (42)'));
373+
});
374+
294375
test('mount(Router)', () async {
295376
final context = _MockRequestContext();
296377
final api = Router()
297-
..all('/user/<user>/info', (RequestContext request, String user) {
378+
..all('/user/<user>/info', (RequestContext context, String user) {
298379
return Response(body: 'Hello $user');
299380
});
300381

0 commit comments

Comments
 (0)