-
Notifications
You must be signed in to change notification settings - Fork 1
/
dart_frog_advanced.dart
115 lines (87 loc) · 3.6 KB
/
dart_frog_advanced.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/// ------------------------------------------------------------
/// ------------Dart Frog - The Advaced Crash Course------------
/// --------https://medium.com/@tomicriedel/4278f1bbfc58--------
/// ------------------------------------------------------------
/// Snap 1 - Create inline provider
import 'package:dart_frog/dart_frog.dart';
Handler middleware(Handler handler) {
// Inject 'Welcome to Dart Frog!' with a provider of type String
return handler.use(provider<String>((context) => 'Welcome to Dart Frog!'));
}
/// Snap 2 - Create extracted provider
Middleware greetingProvider() {
return provider<String>((context) => 'Hello World');
}
/// Snap 3 - Use the extracted provider
Handler middleware(Handler handler) {
return handler.use(greetingProvider());
}
/// Snap 4 - Use asynchronous values
Middleware asyncGreetingProvider() {
return provider<Future<String>>((context) async => 'Hello World');
}
/// Snap 5 - Access provided values
// "Normal" values
Response onRequest(RequestContext context) {
final greeting = context.read<String>();
return Response(body: greeting);
}
// Asynchronous values
// Make sure to add async keyword
Future<Response> onRequest(RequestContext context) async {
// Use await keyword and Future<T>
final value = await context.read<Future<String>>();
return Response(body: value);
}
/// Snap 6 - Unit Testing - use this in your testing file
// Import all necessary packages
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
// Maybe you are not familiar with this one:
// It is used to create a mock for our testing
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
import '../../routes/index.dart' as route;
// We create a Mock that gives us a RequestContext that we can use
class _MockRequestContext extends Mock implements RequestContext {}
void main() {
// Group is used to group up multiple tests
// If you are not familiar with this, check out
// this article again: https://tomicriedel.medium.com/490a577fe077
group('GET /', () {
test('responds with a 200 and greeting.', () async {
const greeting = 'Hello World!';
// Create our context out of _MockRequestContext
final context = _MockRequestContext();
// Call onRequest with the mocked RequestContext
when(() => context.read<String>()).thenReturn(greeting);
final response = route.onRequest(context);
// Expect a 200 response code with the response 'Hello World!'
expect(response.statusCode, equals(HttpStatus.ok));
expect(response.body(), completion(equals(greeting)));
});
});
}
/// Snap 7 - Use custom entry point
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
// 1. Execute any custom code prior to starting the server...
// 2. Use the provided `handler`, `ip`, and `port` to create a custom `HttpServer`.
// Or use the Dart Frog serve method to do that for you.
return serve(handler, ip, port);
}
/// Snap 8 - Add custom static file directory
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
// 1. Execute any custom code prior to starting the server...
// In this example it is our custom file path
const customStaticFilePath = 'api/static';
final cascade = Cascade()
.add(createStaticFileHandler(path: customStaticFilePath))
.add(handler);
// 2. Use the provided `handler`, `ip`, and `port` to create a custom `HttpServer`.
// Or use the Dart Frog serve method to do that for you.
return serve(handler, ip, port);
}