Skip to content

Commit f521779

Browse files
committed
fake_api: Support preparing a queue of responses.
An issue that exists prior to this change is that there can be unused prepared responses when the test ends, but we don't have a way to detect that. We can potentially do something like what testWidgets does with pending timers. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 261baf9 commit f521779

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

test/api/fake_api.dart

+12-13
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,40 @@ 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.
5454
void prepare({
5555
Object? exception,
5656
int? httpStatus,
5757
Map<String, dynamic>? json,
5858
String? body,
5959
Duration delay = Duration.zero,
6060
}) {
61-
assert(_nextResponse == null,
62-
'FakeApiConnection.prepare was called while already expecting a request');
61+
// TODO: Prevent a source of bugs by ensuring that there is no outstanding
62+
// prepared responses when the test ends.
6363
if (exception != null) {
6464
assert(httpStatus == null && json == null && body == null);
65-
_nextResponse = _PreparedException(exception: exception, delay: delay);
65+
_preparedResponses.addLast(_PreparedException(exception: exception, delay: delay));
6666
} else {
6767
assert((json == null) || (body == null));
6868
final String resolvedBody = switch ((body, json)) {
6969
(var body?, _) => body,
7070
(_, var json?) => jsonEncode(json),
7171
_ => '',
7272
};
73-
_nextResponse = _PreparedSuccess(
73+
_preparedResponses.addLast(_PreparedSuccess(
7474
httpStatus: httpStatus ?? 200,
7575
bytes: utf8.encode(resolvedBody),
7676
delay: delay,
77-
);
77+
));
7878
}
7979
}
8080

8181
@override
8282
Future<http.StreamedResponse> send(http.BaseRequest request) {
8383
requestHistory.add(request);
8484

85-
if (_nextResponse == null) {
85+
if (_preparedResponses.isEmpty) {
8686
throw FlutterError.fromParts([
8787
ErrorSummary(
8888
'An API request was attempted in a test when no response was prepared.'),
@@ -91,8 +91,7 @@ class FakeHttpClient extends http.BaseClient {
9191
'call to [FakeApiConnection.prepare].'),
9292
]);
9393
}
94-
final response = _nextResponse!;
95-
_nextResponse = null;
94+
final response = _preparedResponses.removeFirst();
9695

9796
final http.StreamedResponse Function() computation;
9897
switch (response) {

0 commit comments

Comments
 (0)