-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathprovider_test.dart
172 lines (137 loc) · 5.38 KB
/
provider_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
void main() {
test('values can be provided and read via middleware', () async {
const value = '__test_value__';
String? nullableValue;
Handler middleware(Handler handler) {
return handler
.use(provider<String>((_) => value))
.use(provider<String?>((_) => nullableValue));
}
Response onRequest(RequestContext context) {
final value = context.read<String>();
final nullableValue = context.read<String?>();
return Response(body: '$value:$nullableValue');
}
final handler =
const Pipeline().addMiddleware(middleware).addHandler(onRequest);
final server = await serve(handler, 'localhost', 3010);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3010/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
await expectLater(response.body, equals('$value:$nullableValue'));
await server.close();
});
test('descendant providers can access provided values', () async {
const url = 'http://localhost/';
Handler middleware(Handler handler) {
return handler
.use(provider<Uri>((context) => Uri.parse(context.read<String>())))
.use(provider<String>((context) => url));
}
Response onRequest(RequestContext context) {
final value = context.read<Uri>();
return Response(body: value.toString());
}
final handler =
const Pipeline().addMiddleware(middleware).addHandler(onRequest);
final server = await serve(handler, 'localhost', 3011);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3011/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
await expectLater(response.body, equals(url));
await server.close();
});
test('A StateError is thrown when reading an un-provided value', () async {
Object? exception;
Response onRequest(RequestContext context) {
try {
context.read<Uri>();
} catch (e) {
exception = e;
}
return Response();
}
final handler = const Pipeline()
.addMiddleware((handler) => handler)
.addHandler(onRequest);
final server = await serve(handler, 'localhost', 3012);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3012/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
expect(exception, isA<StateError>());
await server.close();
});
group('using tryRead()', () {
test('values can be provided and read via middleware', () async {
const value = '__test_value__';
String? nullableValue;
Handler middleware(Handler handler) {
return handler
.use(provider<String>((_) => value))
.use(provider<String?>((_) => nullableValue));
}
Response onRequest(RequestContext context) {
final value = context.tryRead<String>();
final nullableValue = context.tryRead<String?>();
return Response(body: '$value:$nullableValue');
}
final handler =
const Pipeline().addMiddleware(middleware).addHandler(onRequest);
final server = await serve(handler, 'localhost', 3010);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3010/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
await expectLater(response.body, equals('$value:$nullableValue'));
await server.close();
});
test('descendant providers can access provided values', () async {
const url = 'http://localhost/';
Handler middleware(Handler handler) {
return handler.use(
provider<Uri?>((context) {
final stringValue = context.tryRead<String>();
return stringValue == null ? null : Uri.parse(stringValue);
}),
).use(provider<String>((context) => url));
}
Response onRequest(RequestContext context) {
final value = context.tryRead<Uri?>();
return Response(body: value.toString());
}
final handler =
const Pipeline().addMiddleware(middleware).addHandler(onRequest);
final server = await serve(handler, 'localhost', 3011);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3011/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
await expectLater(response.body, equals(url));
await server.close();
});
test('null is returned and no StateError is thrown', () async {
Object? exception;
Uri? value;
Response onRequest(RequestContext context) {
try {
value = context.tryRead<Uri>();
} catch (e) {
exception = e;
}
return Response();
}
final handler = const Pipeline()
.addMiddleware((handler) => handler)
.addHandler(onRequest);
final server = await serve(handler, 'localhost', 3012);
final client = http.Client();
final response = await client.get(Uri.parse('http://localhost:3012/'));
await expectLater(response.statusCode, equals(HttpStatus.ok));
expect(exception, isNull);
expect(value, isNull);
await server.close();
});
});
}