Skip to content

Commit c47dac5

Browse files
committed
fake_api: Support preparing a queue of responses.
To preserve the original behavior, we add a flag that enables appending responses only when it is set to true. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 1fc317a commit c47dac5

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

test/api/fake_api.dart

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:collection';
12
import 'dart:convert';
23

34
import 'package:flutter/foundation.dart';
@@ -37,10 +38,9 @@ class FakeHttpClient extends http.BaseClient {
3738
return result;
3839
}
3940

40-
_PreparedResponse? _nextResponse;
41+
final Queue<_PreparedResponse> _preparedResponses = Queue();
4142

42-
// Please add more features to this mocking API as needed. For example:
43-
// * preparing more than one request, and logging more than one request
43+
// Please add more features to this mocking API as needed.
4444

4545
/// Prepare the response for the next request.
4646
///
@@ -49,40 +49,44 @@ class FakeHttpClient extends http.BaseClient {
4949
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
5050
/// or else ''. The `body` and `json` parameters must not both be non-null.
5151
///
52-
/// If `exception` is non-null, then `httpStatus`, `body`, and `json` must
53-
/// all be null, and the next request will throw the given exception.
52+
/// If `exception` is non-null, then `httpStatus`, `body`, and `json` must all
53+
/// be null, and the next request will throw the given exception.
54+
///
55+
/// If `append` is true, the prepared response will be added to a First-in
56+
/// First-out responses queue.
5457
void prepare({
5558
Object? exception,
5659
int? httpStatus,
5760
Map<String, dynamic>? json,
5861
String? body,
5962
Duration delay = Duration.zero,
63+
bool append = false,
6064
}) {
61-
assert(_nextResponse == null,
65+
assert(append || _preparedResponses.isEmpty,
6266
'FakeApiConnection.prepare was called while already expecting a request');
6367
if (exception != null) {
6468
assert(httpStatus == null && json == null && body == null);
65-
_nextResponse = _PreparedException(exception: exception, delay: delay);
69+
_preparedResponses.addLast(_PreparedException(exception: exception, delay: delay));
6670
} else {
6771
assert((json == null) || (body == null));
6872
final String resolvedBody = switch ((body, json)) {
6973
(var body?, _) => body,
7074
(_, var json?) => jsonEncode(json),
7175
_ => '',
7276
};
73-
_nextResponse = _PreparedSuccess(
77+
_preparedResponses.addLast(_PreparedSuccess(
7478
httpStatus: httpStatus ?? 200,
7579
bytes: utf8.encode(resolvedBody),
7680
delay: delay,
77-
);
81+
));
7882
}
7983
}
8084

8185
@override
8286
Future<http.StreamedResponse> send(http.BaseRequest request) {
8387
previousRequests.add(request);
8488

85-
if (_nextResponse == null) {
89+
if (_preparedResponses.isEmpty) {
8690
throw FlutterError.fromParts([
8791
ErrorSummary(
8892
'An API request was attempted in a test when no response was prepared.'),
@@ -91,8 +95,7 @@ class FakeHttpClient extends http.BaseClient {
9195
'call to [FakeApiConnection.prepare].'),
9296
]);
9397
}
94-
final response = _nextResponse!;
95-
_nextResponse = null;
98+
final response = _preparedResponses.removeFirst();
9699

97100
final http.StreamedResponse Function() computation;
98101
switch (response) {
@@ -221,17 +224,22 @@ class FakeApiConnection extends ApiConnection {
221224
///
222225
/// In either case, the next request will complete a duration of `delay`
223226
/// after being started.
227+
///
228+
/// If `append` is true, the prepared response will be added to a First-in
229+
/// First-out responses queue.
224230
void prepare({
225231
Object? exception,
226232
int? httpStatus,
227233
Map<String, dynamic>? json,
228234
String? body,
229235
Duration delay = Duration.zero,
236+
bool append = false,
230237
}) {
231238
client.prepare(
232239
exception: exception,
233240
httpStatus: httpStatus, json: json, body: body,
234241
delay: delay,
242+
append: append,
235243
);
236244
}
237245
}

0 commit comments

Comments
 (0)