@@ -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});
11
14
}
12
15
13
16
class _PreparedException extends _PreparedResponse {
14
17
final Object exception;
15
18
16
- _PreparedException ({required this .exception});
19
+ _PreparedException ({required this .exception, super .delay });
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 ({required this .httpStatus, required this .bytes, super .delay });
24
27
}
25
28
26
29
/// An [http.Client] that accepts and replays canned responses, for testing.
@@ -46,19 +49,22 @@ class FakeHttpClient extends http.BaseClient {
46
49
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
47
50
/// or else ''. The `body` and `json` parameters must not both be non-null.
48
51
///
52
+ /// if `delay` is non-null, the response will be delayed by that duration.
53
+ ///
49
54
/// If `exception` is non-null, then `httpStatus` , `body` , and `json` must
50
55
/// all be null, and the next request will throw the given exception.
51
56
void prepare ({
52
57
Object ? exception,
53
58
int ? httpStatus,
54
59
Map <String , dynamic >? json,
55
60
String ? body,
61
+ Duration ? delay,
56
62
}) {
57
63
assert (_nextResponse == null ,
58
64
'FakeApiConnection.prepare was called while already expecting a request' );
59
65
if (exception != null ) {
60
66
assert (httpStatus == null && json == null && body == null );
61
- _nextResponse = _PreparedException (exception: exception);
67
+ _nextResponse = _PreparedException (exception: exception, delay : delay );
62
68
} else {
63
69
assert ((json == null ) || (body == null ));
64
70
final String resolvedBody = switch ((body, json)) {
@@ -69,6 +75,7 @@ class FakeHttpClient extends http.BaseClient {
69
75
_nextResponse = _PreparedSuccess (
70
76
httpStatus: httpStatus ?? 200 ,
71
77
bytes: utf8.encode (resolvedBody),
78
+ delay: delay,
72
79
);
73
80
}
74
81
}
@@ -90,11 +97,11 @@ class FakeHttpClient extends http.BaseClient {
90
97
_nextResponse = null ;
91
98
92
99
switch (response) {
93
- case _PreparedException (: var exception):
94
- return Future ( () => throw exception);
95
- case _PreparedSuccess (: var bytes, : var httpStatus):
100
+ case _PreparedException (: var exception, : var delay ):
101
+ return Future . delayed (delay ?? Duration .zero, () => throw exception);
102
+ case _PreparedSuccess (: var bytes, : var httpStatus, : var delay ):
96
103
final byteStream = http.ByteStream .fromBytes (bytes);
97
- return Future ( () => http.StreamedResponse (
104
+ return Future . delayed (delay ?? Duration .zero, () => http.StreamedResponse (
98
105
byteStream, httpStatus, request: request));
99
106
}
100
107
}
@@ -201,15 +208,18 @@ class FakeApiConnection extends ApiConnection {
201
208
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
202
209
/// or else ''. The `body` and `json` parameters must not both be non-null.
203
210
///
211
+ /// if `delay` is non-null, the response will be delayed by that duration.
212
+ ///
204
213
/// If `exception` is non-null, then `httpStatus` , `body` , and `json` must
205
214
/// all be null, and the next request will throw the given exception.
206
215
void prepare ({
207
216
Object ? exception,
208
217
int ? httpStatus,
209
218
Map <String , dynamic >? json,
210
219
String ? body,
220
+ Duration ? delay,
211
221
}) {
212
222
client.prepare (
213
- exception: exception, httpStatus: httpStatus, json: json, body: body);
223
+ exception: exception, httpStatus: httpStatus, json: json, body: body, delay : delay );
214
224
}
215
225
}
0 commit comments