1
+ import 'dart:collection' ;
1
2
import 'dart:convert' ;
2
3
3
4
import 'package:flutter/foundation.dart' ;
@@ -37,10 +38,9 @@ class FakeHttpClient extends http.BaseClient {
37
38
return result;
38
39
}
39
40
40
- _PreparedResponse ? _nextResponse ;
41
+ final Queue < _PreparedResponse > _preparedResponses = Queue () ;
41
42
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.
44
44
45
45
/// Prepare the response for the next request.
46
46
///
@@ -49,40 +49,40 @@ class FakeHttpClient extends http.BaseClient {
49
49
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
50
50
/// or else ''. The `body` and `json` parameters must not both be non-null.
51
51
///
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
54
void prepare ({
55
55
Object ? exception,
56
56
int ? httpStatus,
57
57
Map <String , dynamic >? json,
58
58
String ? body,
59
59
Duration delay = Duration .zero,
60
60
}) {
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.
63
63
if (exception != null ) {
64
64
assert (httpStatus == null && json == null && body == null );
65
- _nextResponse = _PreparedException (exception: exception, delay: delay);
65
+ _preparedResponses. addLast ( _PreparedException (exception: exception, delay: delay) );
66
66
} else {
67
67
assert ((json == null ) || (body == null ));
68
68
final String resolvedBody = switch ((body, json)) {
69
69
(var body? , _) => body,
70
70
(_, var json? ) => jsonEncode (json),
71
71
_ => '' ,
72
72
};
73
- _nextResponse = _PreparedSuccess (
73
+ _preparedResponses. addLast ( _PreparedSuccess (
74
74
httpStatus: httpStatus ?? 200 ,
75
75
bytes: utf8.encode (resolvedBody),
76
76
delay: delay,
77
- );
77
+ )) ;
78
78
}
79
79
}
80
80
81
81
@override
82
82
Future <http.StreamedResponse > send (http.BaseRequest request) {
83
83
requestHistory.add (request);
84
84
85
- if (_nextResponse == null ) {
85
+ if (_preparedResponses.isEmpty ) {
86
86
throw FlutterError .fromParts ([
87
87
ErrorSummary (
88
88
'An API request was attempted in a test when no response was prepared.' ),
@@ -91,8 +91,7 @@ class FakeHttpClient extends http.BaseClient {
91
91
'call to [FakeApiConnection.prepare].' ),
92
92
]);
93
93
}
94
- final response = _nextResponse! ;
95
- _nextResponse = null ;
94
+ final response = _preparedResponses.removeFirst ();
96
95
97
96
final http.StreamedResponse Function () computation;
98
97
switch (response) {
0 commit comments