@@ -8,19 +8,22 @@ import 'package:zulip/model/store.dart';
8
8
import '../example_data.dart' as eg;
9
9
10
10
sealed class _PreparedResponse {
11
+ final Duration delay;
12
+
13
+ _PreparedResponse ({this .delay = Duration .zero});
11
14
}
12
15
13
16
class _PreparedException extends _PreparedResponse {
14
17
final Object exception;
15
18
16
- _PreparedException ({required this .exception});
19
+ _PreparedException ({super .delay, required this .exception});
17
20
}
18
21
19
22
class _PreparedSuccess extends _PreparedResponse {
20
23
final int httpStatus;
21
24
final List <int > bytes;
22
25
23
- _PreparedSuccess ({required this .httpStatus, required this .bytes});
26
+ _PreparedSuccess ({super .delay, required this .httpStatus, required this .bytes});
24
27
}
25
28
26
29
/// An [http.Client] that accepts and replays canned responses, for testing.
@@ -53,12 +56,13 @@ class FakeHttpClient extends http.BaseClient {
53
56
int ? httpStatus,
54
57
Map <String , dynamic >? json,
55
58
String ? body,
59
+ Duration delay = Duration .zero,
56
60
}) {
57
61
assert (_nextResponse == null ,
58
62
'FakeApiConnection.prepare was called while already expecting a request' );
59
63
if (exception != null ) {
60
64
assert (httpStatus == null && json == null && body == null );
61
- _nextResponse = _PreparedException (exception: exception);
65
+ _nextResponse = _PreparedException (exception: exception, delay : delay );
62
66
} else {
63
67
assert ((json == null ) || (body == null ));
64
68
final String resolvedBody = switch ((body, json)) {
@@ -69,6 +73,7 @@ class FakeHttpClient extends http.BaseClient {
69
73
_nextResponse = _PreparedSuccess (
70
74
httpStatus: httpStatus ?? 200 ,
71
75
bytes: utf8.encode (resolvedBody),
76
+ delay: delay,
72
77
);
73
78
}
74
79
}
@@ -89,14 +94,16 @@ class FakeHttpClient extends http.BaseClient {
89
94
final response = _nextResponse! ;
90
95
_nextResponse = null ;
91
96
97
+ final http.StreamedResponse Function () computation;
92
98
switch (response) {
93
99
case _PreparedException (: var exception):
94
- return Future (( ) => throw exception) ;
100
+ computation = ( ) => throw exception;
95
101
case _PreparedSuccess (: var bytes, : var httpStatus):
96
102
final byteStream = http.ByteStream .fromBytes (bytes);
97
- return Future ( () => http.StreamedResponse (
98
- byteStream, httpStatus, request: request)) ;
103
+ computation = () => http.StreamedResponse (
104
+ byteStream, httpStatus, request: request);
99
105
}
106
+ return Future .delayed (response.delay, computation);
100
107
}
101
108
}
102
109
@@ -203,13 +210,20 @@ class FakeApiConnection extends ApiConnection {
203
210
///
204
211
/// If `exception` is non-null, then `httpStatus` , `body` , and `json` must
205
212
/// all be null, and the next request will throw the given exception.
213
+ ///
214
+ /// In either case, the next request will complete a duration of `delay`
215
+ /// after being started.
206
216
void prepare ({
207
217
Object ? exception,
208
218
int ? httpStatus,
209
219
Map <String , dynamic >? json,
210
220
String ? body,
221
+ Duration delay = Duration .zero,
211
222
}) {
212
223
client.prepare (
213
- exception: exception, httpStatus: httpStatus, json: json, body: body);
224
+ exception: exception,
225
+ httpStatus: httpStatus, json: json, body: body,
226
+ delay: delay,
227
+ );
214
228
}
215
229
}
0 commit comments