collection_providers_test: SSE Output in ResponseBody tested

This commit is contained in:
Manas Hejmadi
2025-07-07 20:14:09 +05:30
parent ca1d9971a8
commit c58f6e6705
3 changed files with 92 additions and 13 deletions

View File

@ -378,7 +378,9 @@ class CollectionStateNotifier
}; };
unsave(); unsave();
}, onError: (e) { }, onError: (e) {
print('Stream error: $e'); if (!completer.isCompleted) {
completer.complete((null, null, 'StreamError: $e'));
}
}); });
final (response, duration, errorMessage) = await completer.future; final (response, duration, errorMessage) = await completer.future;

View File

@ -192,17 +192,27 @@ void main() {
); );
}); });
}); });
group('Testing overrideContentType functionality', () {
test('overrideContentType is true', () async {
final request = prepareHttpRequest(
url: Uri.parse('https://www.example.com'),
method: 'POST',
body: 'Hello',
headers: {'content-type': 'application/json'},
overrideContentType: true,
);
expect(request.headers['content-type'], 'application/json');
});
//TODO: Needs to be Discussed test('overrideContentType is false', () async {
// test('prepareHttpRequest: check overrideContentType ', () async { final request = prepareHttpRequest(
// final request = prepareHttpRequest( url: Uri.parse('https://www.example.com'),
// url: Uri.parse('https://www.example.com'), method: 'POST',
// method: 'POST', body: 'Hello',
// body: '{"key":"value"}', headers: {'content-type': 'application/json'},
// headers: {'content-type': 'application/json'}, overrideContentType: false,
// overrideContentType: true, );
// ); expect(request.headers['content-type'], isNot('application/json'));
// print(request.headers); });
// expect(request.headers['content-type'], isNot('application/json')); });
// });
} }

View File

@ -1,5 +1,7 @@
import 'dart:io';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_body.dart'; import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_body.dart';
import 'package:apidash/widgets/editor.dart'; import 'package:apidash/widgets/editor.dart';
import 'package:apidash/widgets/response_body.dart';
import 'package:apidash_core/apidash_core.dart'; import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -51,4 +53,69 @@ void main() async {
// Verify that the Snackbar is shown // Verify that the Snackbar is shown
expect(find.text('Switched to POST method'), findsOneWidget); expect(find.text('Switched to POST method'), findsOneWidget);
}, skip: true); }, skip: true);
testWidgets('SSE Output is rendered correctly in UI',
(WidgetTester tester) async {
HttpOverrides.global = null; //enable networking in flutter_test
final container = createContainer();
final notifier = container.read(collectionStateNotifierProvider.notifier);
const model = HttpRequestModel(
url: 'https://sse-demo.netlify.app/sse',
method: HTTPVerb.get,
);
notifier.addRequestModel(model, name: 'sseM');
final id = notifier.state!.entries.last.key;
//runAsync to enable user-code awaiting
await tester.runAsync(() async {
await notifier.sendRequest();
await Future.delayed(const Duration(seconds: 3));
});
final rm = notifier.getRequestModel(id)!;
cancelHttpRequest(rm.id);
final sseOutput = (rm.httpResponseModel?.sseOutput ?? [])
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.toList();
expect(sseOutput, isNotEmpty, reason: 'No SSE Output found');
// Render the widget
await tester.pumpWidget(
ProviderScope(
// ignore: deprecated_member_use
parent: container,
child: MaterialApp(
home: Scaffold(
body: ResponseBody(selectedRequestModel: rm),
),
),
),
);
await tester.pumpAndSettle();
final textWidgets = tester.widgetList<Text>(find.byType(Text));
final matchingTextCount = textWidgets
.where((text) =>
text.data != null &&
text.data!.startsWith('data') &&
sseOutput.contains(text.data!.trim()))
.length;
expect(
matchingTextCount,
sseOutput.length,
reason: 'UI does not match all SSE output lines',
);
// Waits for all provider actions to complete before exit
await tester.runAsync(() async {
await Future.delayed(const Duration(seconds: 2));
});
});
} }