import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_test/flutter_test.dart'; import '../env.dart'; void interceptFetchRequest() { final shouldSkip = kIsWeb ? true : ![ TargetPlatform.android, TargetPlatform.iOS, TargetPlatform.macOS, ].contains(defaultTargetPlatform); group('intercept fetch request', () { testWidgets('send string data', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer> fetchPostCompleter = Completer>(); final Completer shouldInterceptFetchPostRequestCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialData: InAppWebViewInitialData(data: """ InAppWebViewFetchTest

InAppWebViewFetchTest

"""), onWebViewCreated: (controller) { controllerCompleter.complete(controller); controller.addJavaScriptHandler( handlerName: "fetchPost", callback: (args) { fetchPostCompleter .complete(args[0] as Map); }); }, shouldInterceptFetchRequest: (controller, fetchRequest) async { assert(fetchRequest.body == "firstname=Foo&lastname=Bar"); fetchRequest.body = "firstname=Foo2&lastname=Bar2"; shouldInterceptFetchPostRequestCompleter.complete(); return fetchRequest; }, ), ), ); await shouldInterceptFetchPostRequestCompleter.future; var fetchPostCompleterValue = await fetchPostCompleter.future; expect( mapEquals(fetchPostCompleterValue, {'firstname': 'Foo2', 'lastname': 'Bar2'}), true); }); testWidgets('send json data', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer> fetchPostCompleter = Completer>(); final Completer shouldInterceptFetchPostRequestCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialData: InAppWebViewInitialData(data: """ InAppWebViewFetchTest

InAppWebViewFetchTest

"""), onWebViewCreated: (controller) { controllerCompleter.complete(controller); controller.addJavaScriptHandler( handlerName: "fetchPost", callback: (args) { fetchPostCompleter .complete(args[0] as Map); }); }, shouldInterceptFetchRequest: (controller, fetchRequest) async { String body = fetchRequest.body; assert(body.contains('"firstname":"Foo"') && body.contains('"lastname":"Bar"')); fetchRequest.body = '{"firstname": "Foo2", "lastname": "Bar2"}'; shouldInterceptFetchPostRequestCompleter.complete(); return fetchRequest; }, ), ), ); await shouldInterceptFetchPostRequestCompleter.future; var fetchPostCompleterValue = await fetchPostCompleter.future; expect( mapEquals(fetchPostCompleterValue, {'firstname': 'Foo2', 'lastname': 'Bar2'}), true); }); testWidgets('send URLSearchParams data', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer> fetchPostCompleter = Completer>(); final Completer shouldInterceptFetchPostRequestCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialData: InAppWebViewInitialData(data: """ InAppWebViewFetchTest

InAppWebViewFetchTest

"""), onWebViewCreated: (controller) { controllerCompleter.complete(controller); controller.addJavaScriptHandler( handlerName: "fetchPost", callback: (args) { fetchPostCompleter .complete(args[0] as Map); }); }, shouldInterceptFetchRequest: (controller, fetchRequest) async { assert(fetchRequest.body == "firstname=Foo&lastname=Bar"); fetchRequest.body = "firstname=Foo2&lastname=Bar2"; shouldInterceptFetchPostRequestCompleter.complete(); return fetchRequest; }, ), ), ); await shouldInterceptFetchPostRequestCompleter.future; var fetchPostCompleterValue = await fetchPostCompleter.future; expect( mapEquals(fetchPostCompleterValue, {'firstname': 'Foo2', 'lastname': 'Bar2'}), true); }); testWidgets('send FormData', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer> fetchPostCompleter = Completer>(); final Completer shouldInterceptFetchPostRequestCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialData: InAppWebViewInitialData(data: """ InAppWebViewFetchTest

InAppWebViewFetchTest

"""), onWebViewCreated: (controller) { controllerCompleter.complete(controller); controller.addJavaScriptHandler( handlerName: "fetchPost", callback: (args) { fetchPostCompleter .complete(args[0] as Map); }); }, shouldInterceptFetchRequest: (controller, fetchRequest) async { assert(fetchRequest.body != null); var body = fetchRequest.body.cast(); var bodyString = String.fromCharCodes(body); assert(bodyString.indexOf("WebKitFormBoundary") >= 0); fetchRequest.body = utf8.encode(bodyString .replaceFirst("Foo", "Foo2") .replaceFirst("Bar", "Bar2")); shouldInterceptFetchPostRequestCompleter.complete(); return fetchRequest; }, ), ), ); await shouldInterceptFetchPostRequestCompleter.future; var fetchPostCompleterValue = await fetchPostCompleter.future; expect( mapEquals(fetchPostCompleterValue, {'firstname': 'Foo2', 'lastname': 'Bar2'}), true); }); }, skip: shouldSkip); }