Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Attempt to fix tests
  • Loading branch information
ThomasAunvik authored and ditman committed Jul 9, 2024
commit 5775d2e445a727aa5d90e7bbe4aa795ffa01a602
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:web/web.dart';
import 'dart:typed_data';

import 'package:http/browser_client.dart';
import 'package:http/http.dart' as http;

/// Factory class for creating [HttpRequest] instances.
class HttpRequestFactory {
Expand Down Expand Up @@ -60,21 +63,33 @@ class HttpRequestFactory {
/// when the file cannot be found.
///
/// See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access_authentication).
Future<XMLHttpRequest> request(String url,
{String? method,
bool? withCredentials,
String? responseType,
String? mimeType,
Map<String, String>? requestHeaders,
dynamic sendData,
void Function(ProgressEvent e)? onProgress}) {
return HttpRequest.request(url,
method: method,
withCredentials: withCredentials,
responseType: responseType,
mimeType: mimeType,
requestHeaders: requestHeaders,
sendData: sendData,
onProgress: onProgress);
Future<http.StreamedResponse> request(
String url, {
String? method,
bool? withCredentials,
String? mimeType,
Map<String, String>? requestHeaders,
Uint8List? sendData,
}) {
final BrowserClient client = BrowserClient();
if (withCredentials != null) {
client.withCredentials = withCredentials;
}

final http.Request request = http.Request(method ?? 'GET', Uri.parse(url));

if (sendData != null) {
request.bodyBytes = sendData.toList();
}

if (mimeType != null) {
request.headers['content-type'] = mimeType;
}

if (requestHeaders != null) {
request.headers.addAll(requestHeaders);
}

return client.send(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:ui_web' as ui_web;

import 'package:flutter/cupertino.dart';
import 'package:web/web.dart' as html;
import 'package:http/http.dart' as http;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'content_type.dart';
Expand Down Expand Up @@ -86,22 +87,23 @@ class WebWebViewController extends PlatformWebViewController {

/// Performs an AJAX request defined by [params].
Future<void> _updateIFrameFromXhr(LoadRequestParams params) async {
final html.XMLHttpRequest httpReq =
final http.StreamedResponse httpReq =
await _webWebViewParams.httpRequestFactory.request(
params.uri.toString(),
method: params.method.serialize(),
requestHeaders: params.headers,
sendData: params.body,
);

final String header =
httpReq.getResponseHeader('content-type') ?? 'text/html';
final String header = httpReq.headers['content-type'] ?? 'text/html';
final ContentType contentType = ContentType.parse(header);
final Encoding encoding = Encoding.getByName(contentType.charset) ?? utf8;

final http.Response response = await http.Response.fromStream(httpReq);

// ignore: unsafe_html
_webWebViewParams.iFrame.src = Uri.dataFromString(
httpReq.responseText,
response.body,
mimeType: contentType.mimeType,
encoding: encoding,
).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:http/src/streamed_response.dart';
import 'package:web/web.dart' as html;
import 'package:http/http.dart' as http;
// ignore: implementation_imports
import 'package:webview_flutter_platform_interface/src/webview_flutter_platform_interface_legacy.dart';

Expand Down Expand Up @@ -46,8 +48,8 @@ class WebWebViewPlatform implements WebViewPlatform {
if (onWebViewPlatformCreated == null) {
return;
}
final html.HTMLIFrameElement element =
html.document.getElementById('webview-$viewId')! as html.HTMLIFrameElement;
final html.HTMLIFrameElement element = html.document
.getElementById('webview-$viewId')! as html.HTMLIFrameElement;

final String? initialUrl = creationParams.initialUrl;
if (initialUrl != null) {
Expand Down Expand Up @@ -202,16 +204,18 @@ class WebWebViewPlatformController implements WebViewPlatformController {
if (!request.uri.hasScheme) {
throw ArgumentError('WebViewRequest#uri is required to have a scheme.');
}
final html.XMLHttpRequest httpReq = await _httpRequestFactory.request(
final http.StreamedResponse httpReq = await _httpRequestFactory.request(
request.uri.toString(),
method: request.method.serialize(),
requestHeaders: request.headers,
sendData: request.body);
final String contentType =
httpReq.getResponseHeader('content-type') ?? 'text/html';
// ignore: unsafe_html

final String contentType = httpReq.headers['content-type'] ?? 'text/html';

final http.Response response = await http.Response.fromStream(httpReq);

_element.src = Uri.dataFromString(
httpReq.responseText,
response.body,
mimeType: contentType,
encoding: utf8,
).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
http: ^1.2.1
web: ^0.5.1
webview_flutter_platform_interface: ^2.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


import 'package:web/helpers.dart';
import 'package:web/web.dart' as html;
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:web/web.dart' as html;
import 'package:webview_flutter_platform_interface/src/webview_flutter_platform_interface_legacy.dart';
import 'package:webview_flutter_web/src/http_request_factory.dart';
import 'package:webview_flutter_web/src/webview_flutter_web_legacy.dart';
Expand All @@ -23,7 +22,7 @@ import 'webview_flutter_web_test.mocks.dart';
CreationParams,
WebViewPlatformCallbacksHandler,
HttpRequestFactory,
html.XMLHttpRequest,
http.StreamedResponse,
])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Expand All @@ -47,7 +46,7 @@ void main() {
group('WebWebViewPlatformController', () {
test('loadUrl sets url on iframe src attribute', () {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -61,7 +60,7 @@ void main() {
group('loadHtmlString', () {
test('loadHtmlString loads html into iframe', () {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -75,22 +74,22 @@ void main() {

test('loadHtmlString escapes "#" correctly', () {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
);
// Run
controller.loadHtmlString('#');
// Verify
verify(mockElement.src = argThat(contains('%23')));
verify(mockElement.src = argThat(contains('%23')) ?? '');
});
});

group('loadRequest', () {
test('loadRequest throws ArgumentError on missing scheme', () {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
Expand All @@ -109,23 +108,26 @@ void main() {
test('loadRequest makes request and loads response into iframe',
() async {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
);
final MockHttpRequest mockHttpRequest = MockHttpRequest();
when(mockHttpRequest.getResponseHeader('content-type'))
.thenReturn('text/plain');
when(mockHttpRequest.responseText).thenReturn('test data');
final MockStreamedResponse mockHttpRequest = MockStreamedResponse();
when(mockHttpRequest.headers['content-type']).thenReturn('text/plain');

final http.Response res =
await http.Response.fromStream(mockHttpRequest);
when(res.body).thenReturn('test data');
final MockHttpRequestFactory mockHttpRequestFactory =
MockHttpRequestFactory();
when(mockHttpRequestFactory.request(
any,
method: anyNamed('method'),
requestHeaders: anyNamed('requestHeaders'),
sendData: anyNamed('sendData'),
)).thenAnswer((_) => Future<XMLHttpRequest>.value(mockHttpRequest));
)).thenAnswer(
(_) => Future<http.StreamedResponse>.value(mockHttpRequest));
controller.httpRequestFactory = mockHttpRequestFactory;
// Run
await controller.loadRequest(
Expand All @@ -148,23 +150,26 @@ void main() {

test('loadRequest escapes "#" correctly', () async {
// Setup
final MockIFrameElement mockElement = MockIFrameElement();
final html.HTMLIFrameElement mockElement = html.HTMLIFrameElement();
final WebWebViewPlatformController controller =
WebWebViewPlatformController(
mockElement,
);
final MockHttpRequest mockHttpRequest = MockHttpRequest();
when(mockHttpRequest.getResponseHeader('content-type'))
.thenReturn('text/html');
when(mockHttpRequest.responseText).thenReturn('#');
final MockStreamedResponse mockHttpRequest = MockStreamedResponse();
when(mockHttpRequest.headers['content-type']).thenReturn('text/html');

final http.Response res =
await http.Response.fromStream(mockHttpRequest);
when(res.body).thenReturn('#');
final MockHttpRequestFactory mockHttpRequestFactory =
MockHttpRequestFactory();
when(mockHttpRequestFactory.request(
any,
method: anyNamed('method'),
requestHeaders: anyNamed('requestHeaders'),
sendData: anyNamed('sendData'),
)).thenAnswer((_) => Future<XMLHttpRequest>.value(mockHttpRequest));
)).thenAnswer(
(_) => Future<http.StreamedResponse>.value(mockHttpRequest));
controller.httpRequestFactory = mockHttpRequestFactory;
// Run
await controller.loadRequest(
Expand All @@ -175,7 +180,7 @@ void main() {
headers: <String, String>{'Foo': 'Bar'}),
);
// Verify
verify(mockElement.src = argThat(contains('%23')));
verify(mockElement.src = argThat(contains('%23')) ?? '');
});
});
});
Expand Down
Loading