Skip to content

Commit a933232

Browse files
committed
fake_api [nfc]: Clarify prepared exception is at HTTP layer
And leave a couple of TODO comments in the one test file that was accidentally preparing exceptions here that won't turn out in the intended way. The next commit will provide a clean way for these tests to do what they intend instead.
1 parent 9a971c4 commit a933232

8 files changed

+31
-19
lines changed

test/api/core_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ Future<T> tryRequest<T extends Object?>({
504504
fromJson ??= (((Map<String, dynamic> x) => x) as T Function(Map<String, dynamic>));
505505
return FakeApiConnection.with_((connection) {
506506
connection.prepare(
507-
exception: exception, httpStatus: httpStatus, json: json, body: body);
507+
httpException: exception, httpStatus: httpStatus, json: json, body: body);
508508
return connection.get(kExampleRouteName, fromJson!, 'example/route', {});
509509
});
510510
}

test/api/fake_api.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,28 +209,31 @@ class FakeApiConnection extends ApiConnection {
209209

210210
List<http.BaseRequest> takeRequests() => client.takeRequests();
211211

212-
/// Prepare the response for the next request.
212+
/// Prepare the HTTP response for the next request.
213213
///
214-
/// If `exception` is null, the next request will produce an [http.Response]
214+
/// If `httpException` is null, the next request will produce an [http.Response]
215215
/// with the given `httpStatus`, defaulting to 200. The body of the response
216216
/// will be `body` if non-null, or `jsonEncode(json)` if `json` is non-null,
217217
/// or else ''. The `body` and `json` parameters must not both be non-null.
218218
///
219-
/// If `exception` is non-null, then `httpStatus`, `body`, and `json` must
220-
/// all be null, and the next request will throw the given exception.
219+
/// If `httpException` is non-null, then
220+
/// `httpStatus`, `body`, and `json` must all be null, and the next request
221+
/// will throw the given exception within the HTTP client layer,
222+
/// causing the API request to throw a [NetworkException]
223+
/// wrapping the given exception.
221224
///
222225
/// In either case, the next request will complete a duration of `delay`
223226
/// after being started.
224227
void prepare({
225-
Object? exception,
228+
Object? httpException,
226229
int? httpStatus,
227230
Map<String, dynamic>? json,
228231
String? body,
229232
Duration delay = Duration.zero,
230233
}) {
231234
assert(isOpen);
232235
client.prepare(
233-
exception: exception,
236+
exception: httpException,
234237
httpStatus: httpStatus, json: json, body: body,
235238
delay: delay,
236239
);

test/api/fake_api_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ void main() {
2525
..asString.contains('FakeApiConnection.prepare'));
2626
});
2727

28+
test('prepare HTTP exception -> get NetworkException', () async {
29+
final connection = FakeApiConnection();
30+
final exception = Exception('oops');
31+
connection.prepare(httpException: exception);
32+
await check(connection.get('aRoute', (json) => json, '/', null))
33+
.throws((it) => it.isA<NetworkException>()
34+
..cause.identicalTo(exception));
35+
});
36+
2837
test('delay success', () => awaitFakeAsync((async) async {
2938
final connection = FakeApiConnection();
3039
connection.prepare(delay: const Duration(seconds: 2),
@@ -44,7 +53,7 @@ void main() {
4453
test('delay exception', () => awaitFakeAsync((async) async {
4554
final connection = FakeApiConnection();
4655
connection.prepare(delay: const Duration(seconds: 2),
47-
exception: Exception("oops"));
56+
httpException: Exception("oops"));
4857

4958
Object? error;
5059
unawaited(connection.get('aRoute', (json) => null, '/', null)

test/model/actions_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void main() {
103103
assert(unregisterDelay > TestGlobalStore.removeAccountDuration);
104104
final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
105105
final newConnection = separateConnection()
106-
..prepare(delay: unregisterDelay, exception: exception);
106+
..prepare(delay: unregisterDelay, httpException: exception); // TODO this isn't an HTTP exception
107107

108108
final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id);
109109
// Unregister-token request and account removal dispatched together
@@ -165,7 +165,7 @@ void main() {
165165

166166
final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
167167
final newConnection = separateConnection()
168-
..prepare(exception: exception);
168+
..prepare(httpException: exception); // TODO this isn't an HTTP exception
169169
final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id);
170170
async.elapse(Duration.zero);
171171
await future;

test/model/store_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ void main() {
473473

474474
// Try to load, inducing an error in the request.
475475
globalStore.useCachedApiConnections = true;
476-
connection.prepare(exception: Exception('failed'));
476+
connection.prepare(httpException: Exception('failed'));
477477
final future = UpdateMachine.load(globalStore, eg.selfAccount.id);
478478
bool complete = false;
479479
unawaited(future.whenComplete(() => complete = true));
@@ -541,7 +541,7 @@ void main() {
541541
check(store.debugServerEmojiData).isNull();
542542

543543
// Try to fetch, inducing an error in the request.
544-
connection.prepare(exception: Exception('failed'));
544+
connection.prepare(httpException: Exception('failed'));
545545
final future = updateMachine.fetchEmojiData(emojiDataUrl);
546546
bool complete = false;
547547
unawaited(future.whenComplete(() => complete = true));
@@ -712,11 +712,11 @@ void main() {
712712
}
713713

714714
void prepareNetworkExceptionSocketException() {
715-
connection.prepare(exception: const SocketException('failed'));
715+
connection.prepare(httpException: const SocketException('failed'));
716716
}
717717

718718
void prepareNetworkException() {
719-
connection.prepare(exception: Exception("failed"));
719+
connection.prepare(httpException: Exception("failed"));
720720
}
721721

722722
void prepareServer5xxException() {

test/widgets/action_sheet_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ void main() {
545545
await prepare(topic: 'zulip');
546546
await showFromRecipientHeader(tester, message: message);
547547
connection.takeRequests();
548-
connection.prepare(exception: http.ClientException('Oops'));
548+
connection.prepare(httpException: http.ClientException('Oops'));
549549
await tester.tap(findButtonForLabel('Mark as resolved'));
550550
await tester.pumpAndSettle();
551551
checkRequest(message.id, '✔ zulip');
@@ -559,7 +559,7 @@ void main() {
559559
await prepare(topic: '✔ zulip');
560560
await showFromRecipientHeader(tester, message: message);
561561
connection.takeRequests();
562-
connection.prepare(exception: http.ClientException('Oops'));
562+
connection.prepare(httpException: http.ClientException('Oops'));
563563
await tester.tap(findButtonForLabel('Mark as unresolved'));
564564
await tester.pumpAndSettle();
565565
checkRequest(message.id, 'zulip');
@@ -1016,7 +1016,7 @@ void main() {
10161016
final message = eg.streamMessage(flags: [MessageFlag.read]);
10171017
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
10181018

1019-
connection.prepare(exception: http.ClientException('Oops'));
1019+
connection.prepare(httpException: http.ClientException('Oops'));
10201020
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
10211021

10221022
await tester.ensureVisible(find.byIcon(Icons.mark_chat_unread_outlined, skipOffstage: false));

test/widgets/actions_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void main() {
329329

330330
testWidgets('catch-all api errors', (tester) async {
331331
await prepare(tester);
332-
connection.prepare(exception: http.ClientException('Oops'));
332+
connection.prepare(httpException: http.ClientException('Oops'));
333333
final didPass = invokeUpdateMessageFlagsStartingFromAnchor();
334334
await tester.pump(Duration.zero);
335335
checkErrorDialog(tester,

test/widgets/message_list_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ void main() {
694694
narrow: narrow, messages: [message], unreadMsgs: unreadMsgs);
695695
check(isMarkAsReadButtonVisible(tester)).isTrue();
696696

697-
connection.prepare(exception: http.ClientException('Oops'));
697+
connection.prepare(httpException: http.ClientException('Oops'));
698698
await tester.tap(find.byType(MarkAsReadWidget));
699699
await tester.pumpAndSettle();
700700
checkErrorDialog(tester,

0 commit comments

Comments
 (0)