1
+ import 'dart:collection' ;
1
2
import 'dart:convert' ;
2
3
3
4
import 'package:flutter/foundation.dart' ;
@@ -38,10 +39,9 @@ class FakeHttpClient extends http.BaseClient {
38
39
return result;
39
40
}
40
41
41
- _PreparedResponse ? _nextResponse ;
42
+ final Queue < _PreparedResponse > _preparedResponses = Queue () ;
42
43
43
- // Please add more features to this mocking API as needed. For example:
44
- // * preparing more than one request, and logging more than one request
44
+ // Please add more features to this mocking API as needed.
45
45
46
46
/// Prepare the response for the next request.
47
47
///
@@ -59,31 +59,31 @@ class FakeHttpClient extends http.BaseClient {
59
59
String ? body,
60
60
Duration delay = Duration .zero,
61
61
}) {
62
- assert (_nextResponse == null ,
63
- 'FakeApiConnection.prepare was called while already expecting a request' );
62
+ // TODO: Prevent a source of bugs by ensuring that there are no outstanding
63
+ // prepared responses when the test ends.
64
64
if (exception != null ) {
65
65
assert (httpStatus == null && json == null && body == null );
66
- _nextResponse = _PreparedException (exception: exception, delay: delay);
66
+ _preparedResponses. addLast ( _PreparedException (exception: exception, delay: delay) );
67
67
} else {
68
68
assert ((json == null ) || (body == null ));
69
69
final String resolvedBody = switch ((body, json)) {
70
70
(var body? , _) => body,
71
71
(_, var json? ) => jsonEncode (json),
72
72
_ => '' ,
73
73
};
74
- _nextResponse = _PreparedSuccess (
74
+ _preparedResponses. addLast ( _PreparedSuccess (
75
75
httpStatus: httpStatus ?? 200 ,
76
76
bytes: utf8.encode (resolvedBody),
77
77
delay: delay,
78
- );
78
+ )) ;
79
79
}
80
80
}
81
81
82
82
@override
83
83
Future <http.StreamedResponse > send (http.BaseRequest request) {
84
84
_requestHistory.add (request);
85
85
86
- if (_nextResponse == null ) {
86
+ if (_preparedResponses.isEmpty ) {
87
87
throw FlutterError .fromParts ([
88
88
ErrorSummary (
89
89
'An API request was attempted in a test when no response was prepared.' ),
@@ -92,8 +92,7 @@ class FakeHttpClient extends http.BaseClient {
92
92
'call to [FakeApiConnection.prepare].' ),
93
93
]);
94
94
}
95
- final response = _nextResponse! ;
96
- _nextResponse = null ;
95
+ final response = _preparedResponses.removeFirst ();
97
96
98
97
final http.StreamedResponse Function () computation;
99
98
switch (response) {
0 commit comments