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,44 @@ 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
+ ///
55
+ /// If `append` is true, the prepared response will be added to a First-in
56
+ /// First-out responses queue.
54
57
void prepare ({
55
58
Object ? exception,
56
59
int ? httpStatus,
57
60
Map <String , dynamic >? json,
58
61
String ? body,
59
62
Duration delay = Duration .zero,
63
+ bool append = false ,
60
64
}) {
61
- assert (_nextResponse == null ,
65
+ assert (append || _preparedResponses.isEmpty ,
62
66
'FakeApiConnection.prepare was called while already expecting a request' );
63
67
if (exception != null ) {
64
68
assert (httpStatus == null && json == null && body == null );
65
- _nextResponse = _PreparedException (exception: exception, delay: delay);
69
+ _preparedResponses. addLast ( _PreparedException (exception: exception, delay: delay) );
66
70
} else {
67
71
assert ((json == null ) || (body == null ));
68
72
final String resolvedBody = switch ((body, json)) {
69
73
(var body? , _) => body,
70
74
(_, var json? ) => jsonEncode (json),
71
75
_ => '' ,
72
76
};
73
- _nextResponse = _PreparedSuccess (
77
+ _preparedResponses. addLast ( _PreparedSuccess (
74
78
httpStatus: httpStatus ?? 200 ,
75
79
bytes: utf8.encode (resolvedBody),
76
80
delay: delay,
77
- );
81
+ )) ;
78
82
}
79
83
}
80
84
81
85
@override
82
86
Future <http.StreamedResponse > send (http.BaseRequest request) {
83
87
previousRequests.add (request);
84
88
85
- if (_nextResponse == null ) {
89
+ if (_preparedResponses.isEmpty ) {
86
90
throw FlutterError .fromParts ([
87
91
ErrorSummary (
88
92
'An API request was attempted in a test when no response was prepared.' ),
@@ -91,8 +95,7 @@ class FakeHttpClient extends http.BaseClient {
91
95
'call to [FakeApiConnection.prepare].' ),
92
96
]);
93
97
}
94
- final response = _nextResponse! ;
95
- _nextResponse = null ;
98
+ final response = _preparedResponses.removeFirst ();
96
99
97
100
final http.StreamedResponse Function () computation;
98
101
switch (response) {
@@ -221,17 +224,22 @@ class FakeApiConnection extends ApiConnection {
221
224
///
222
225
/// In either case, the next request will complete a duration of `delay`
223
226
/// after being started.
227
+ ///
228
+ /// If `append` is true, the prepared response will be added to a First-in
229
+ /// First-out responses queue.
224
230
void prepare ({
225
231
Object ? exception,
226
232
int ? httpStatus,
227
233
Map <String , dynamic >? json,
228
234
String ? body,
229
235
Duration delay = Duration .zero,
236
+ bool append = false ,
230
237
}) {
231
238
client.prepare (
232
239
exception: exception,
233
240
httpStatus: httpStatus, json: json, body: body,
234
241
delay: delay,
242
+ append: append,
235
243
);
236
244
}
237
245
}
0 commit comments