File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ import 'dart:developer' ;
2
+
3
+ import 'package:dart_frog/dart_frog.dart' ;
4
+
5
+ final _counter = Counter ('counter' , 'a simple request counter' );
6
+
7
+ Handler middleware (Handler handler) {
8
+ return handler.use (provider <Counter >((_) => _counter));
9
+ }
Original file line number Diff line number Diff line change
1
+ import 'dart:developer' ;
2
+
3
+ import 'package:dart_frog/dart_frog.dart' ;
4
+
5
+ Response onRequest (RequestContext context) {
6
+ final counter = context.read <Counter >()..value += 1 ;
7
+ return Response (
8
+ body: 'You have requested this route ${counter .value .toInt ()} time(s).' ,
9
+ );
10
+ }
Original file line number Diff line number Diff line change
1
+ import 'dart:developer' ;
2
+
3
+ import 'package:dart_frog/dart_frog.dart' ;
4
+ import 'package:mocktail/mocktail.dart' ;
5
+ import 'package:test/test.dart' ;
6
+
7
+ import '../../../routes/count/_middleware.dart' ;
8
+
9
+ class _MockRequestContext extends Mock implements RequestContext {}
10
+
11
+ void main () {
12
+ group ('middleware' , () {
13
+ test ('provides count instance' , () async {
14
+ double ? count;
15
+ final handler = middleware (
16
+ (context) {
17
+ final counter = context.read <Counter >();
18
+ count = counter.value += 1 ;
19
+ return Response (body: '' );
20
+ },
21
+ );
22
+ final request = Request .get (Uri .parse ('http://localhost/' ));
23
+ final context = _MockRequestContext ();
24
+ when (() => context.request).thenReturn (request);
25
+
26
+ await handler (context);
27
+ expect (count, equals (1 ));
28
+
29
+ await handler (context);
30
+ expect (count, equals (2 ));
31
+
32
+ await handler (context);
33
+ expect (count, equals (3 ));
34
+ });
35
+ });
36
+ }
Original file line number Diff line number Diff line change
1
+ import 'dart:developer' ;
2
+ import 'dart:io' ;
3
+
4
+ import 'package:dart_frog/dart_frog.dart' ;
5
+ import 'package:mocktail/mocktail.dart' ;
6
+ import 'package:test/test.dart' ;
7
+
8
+ import '../../../routes/count/index.dart' as route;
9
+
10
+ class _MockRequestContext extends Mock implements RequestContext {}
11
+
12
+ class _MockCounter extends Mock implements Counter {}
13
+
14
+ void main () {
15
+ group ('GET /' , () {
16
+ test ('responds with a 200 and count.' , () async {
17
+ const count = 42 ;
18
+ final counter = _MockCounter ();
19
+ when (() => counter.value).thenReturn (count.toDouble ());
20
+ final context = _MockRequestContext ();
21
+ when (() => context.read <Counter >()).thenReturn (counter);
22
+ final response = route.onRequest (context);
23
+ expect (response.statusCode, equals (HttpStatus .ok));
24
+ expect (
25
+ response.body (),
26
+ completion (
27
+ equals ('You have requested this route $count time(s).' ),
28
+ ),
29
+ );
30
+ });
31
+ });
32
+ }
You can’t perform that action at this time.
0 commit comments