iosWebViewFix/example/integration_test/in_app_webview/post_requests.dart

141 lines
4.8 KiB
Dart
Raw Normal View History

part of 'main.dart';
2022-04-28 18:34:54 +00:00
void postRequests() {
2022-04-29 11:39:28 +00:00
final shouldSkip = kIsWeb
? true
: ![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
2022-04-28 18:34:54 +00:00
skippableGroup('POST requests', () {
skippableTestWidgets('initialUrlRequest', (WidgetTester tester) async {
final Completer<InAppWebViewController> controllerCompleter =
Completer<InAppWebViewController>();
2022-04-28 18:34:54 +00:00
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: WebUri(
2022-04-28 18:34:54 +00:00
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("name=FooBar")),
2022-04-29 11:39:28 +00:00
headers: {'Content-Type': 'application/x-www-form-urlencoded'}),
2022-04-28 18:34:54 +00:00
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
postPageLoaded.complete();
},
),
),
);
final InAppWebViewController controller =
2022-04-29 11:39:28 +00:00
await controllerCompleter.future;
2022-04-28 18:34:54 +00:00
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
2022-04-29 11:39:28 +00:00
expect(
currentUrl, 'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
2022-04-28 18:34:54 +00:00
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
skippableTestWidgets('loadUrl', (WidgetTester tester) async {
final Completer<InAppWebViewController> controllerCompleter =
Completer<InAppWebViewController>();
2022-04-28 18:34:54 +00:00
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: WebUri('about:blank')),
2022-04-28 18:34:54 +00:00
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
2022-04-29 11:39:28 +00:00
await controllerCompleter.future;
2022-04-28 18:34:54 +00:00
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.loadUrl(
urlRequest: URLRequest(
url: WebUri(
2022-04-28 18:34:54 +00:00
"http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
method: 'POST',
body: postData,
2022-04-29 11:39:28 +00:00
headers: {'Content-Type': 'application/x-www-form-urlencoded'}));
2022-04-28 18:34:54 +00:00
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
2022-04-29 11:39:28 +00:00
expect(
currentUrl, 'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
2022-04-28 18:34:54 +00:00
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
skippableTestWidgets('postUrl', (WidgetTester tester) async {
final Completer<InAppWebViewController> controllerCompleter =
Completer<InAppWebViewController>();
2022-04-28 18:34:54 +00:00
final Completer<void> postPageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(url: WebUri('about:blank')),
2022-04-28 18:34:54 +00:00
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
onLoadStop: (controller, url) {
if (url?.scheme != "about") {
postPageLoaded.complete();
}
},
),
),
);
final InAppWebViewController controller =
2022-04-29 11:39:28 +00:00
await controllerCompleter.future;
2022-04-28 18:34:54 +00:00
var postData = Uint8List.fromList(utf8.encode("name=FooBar"));
await controller.postUrl(
url: WebUri("http://${environment["NODE_SERVER_IP"]}:8082/test-post"),
2022-04-28 18:34:54 +00:00
postData: postData);
await postPageLoaded.future;
final String? currentUrl = (await controller.getUrl())?.toString();
2022-04-29 11:39:28 +00:00
expect(
currentUrl, 'http://${environment["NODE_SERVER_IP"]}:8082/test-post');
2022-04-28 18:34:54 +00:00
final String? pContent = await controller.evaluateJavascript(
source: "document.querySelector('p').innerHTML;");
expect(pContent, "HELLO FooBar!");
});
}, skip: shouldSkip);
}