-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webview_flutter_web] Avoids XHR when possible. (#7090)
* update * Request body must be empty too to skip XHR request. Add test. * Add ContentType class to parse response headers. * Use content-type in response to encode iframe contents. * Attempt to run integration_tests. Do they ever fail? * Update docs. * Set Widget styles in a way the flutter engine likes. * Add bash to codeblocks in readme. --------- Co-authored-by: David Iglesias Teixeira <ditman@gmail.com>
- Loading branch information
1 parent
634c140
commit 0d9b408
Showing
10 changed files
with
317 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/webview_flutter/webview_flutter_web/example/run_test.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/bash | ||
# 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. | ||
|
||
if pgrep -lf chromedriver > /dev/null; then | ||
echo "chromedriver is running." | ||
|
||
if [ $# -eq 0 ]; then | ||
echo "No target specified, running all tests..." | ||
find integration_test/ -iname *_test.dart | xargs -n1 -i -t flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_test.dart --target='{}' | ||
else | ||
echo "Running test target: $1..." | ||
set -x | ||
flutter drive -d web-server --web-port=7357 --browser-name=chrome --driver=test_driver/integration_test.dart --target=$1 | ||
fi | ||
|
||
else | ||
echo "chromedriver is not running." | ||
echo "Please, check the README.md for instructions on how to use run_test.sh" | ||
fi | ||
|
48 changes: 48 additions & 0 deletions
48
packages/webview_flutter/webview_flutter_web/lib/src/content_type.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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. | ||
|
||
/// Class to represent a content-type header value. | ||
class ContentType { | ||
/// Creates a [ContentType] instance by parsing a "content-type" response [header]. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type | ||
/// See: https://httpwg.org/specs/rfc9110.html#media.type | ||
ContentType.parse(String header) { | ||
final Iterable<String> chunks = | ||
header.split(';').map((String e) => e.trim().toLowerCase()); | ||
|
||
for (final String chunk in chunks) { | ||
if (!chunk.contains('=')) { | ||
_mimeType = chunk; | ||
} else { | ||
final List<String> bits = | ||
chunk.split('=').map((String e) => e.trim()).toList(); | ||
assert(bits.length == 2); | ||
switch (bits[0]) { | ||
case 'charset': | ||
_charset = bits[1]; | ||
break; | ||
case 'boundary': | ||
_boundary = bits[1]; | ||
break; | ||
default: | ||
throw StateError('Unable to parse "$chunk" in content-type.'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
String? _mimeType; | ||
String? _charset; | ||
String? _boundary; | ||
|
||
/// The MIME-type of the resource or the data. | ||
String? get mimeType => _mimeType; | ||
|
||
/// The character encoding standard. | ||
String? get charset => _charset; | ||
|
||
/// The separation boundary for multipart entities. | ||
String? get boundary => _boundary; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/webview_flutter/webview_flutter_web/test/content_type_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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:flutter_test/flutter_test.dart'; | ||
import 'package:webview_flutter_web/src/content_type.dart'; | ||
|
||
void main() { | ||
group('ContentType.parse', () { | ||
test('basic content-type (lowers case)', () { | ||
final ContentType contentType = ContentType.parse('text/pLaIn'); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, isNull); | ||
expect(contentType.charset, isNull); | ||
}); | ||
|
||
test('with charset', () { | ||
final ContentType contentType = | ||
ContentType.parse('text/pLaIn; charset=utf-8'); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, isNull); | ||
expect(contentType.charset, 'utf-8'); | ||
}); | ||
|
||
test('with boundary', () { | ||
final ContentType contentType = | ||
ContentType.parse('text/pLaIn; boundary=---xyz'); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, '---xyz'); | ||
expect(contentType.charset, isNull); | ||
}); | ||
|
||
test('with charset and boundary', () { | ||
final ContentType contentType = | ||
ContentType.parse('text/pLaIn; charset=utf-8; boundary=---xyz'); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, '---xyz'); | ||
expect(contentType.charset, 'utf-8'); | ||
}); | ||
|
||
test('with boundary and charset', () { | ||
final ContentType contentType = | ||
ContentType.parse('text/pLaIn; boundary=---xyz; charset=utf-8'); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, '---xyz'); | ||
expect(contentType.charset, 'utf-8'); | ||
}); | ||
|
||
test('with a bunch of whitespace, boundary and charset', () { | ||
final ContentType contentType = ContentType.parse( | ||
' text/pLaIn ; boundary=---xyz; charset=utf-8 '); | ||
|
||
expect(contentType.mimeType, 'text/plain'); | ||
expect(contentType.boundary, '---xyz'); | ||
expect(contentType.charset, 'utf-8'); | ||
}); | ||
|
||
test('empty string', () { | ||
final ContentType contentType = ContentType.parse(''); | ||
|
||
expect(contentType.mimeType, ''); | ||
expect(contentType.boundary, isNull); | ||
expect(contentType.charset, isNull); | ||
}); | ||
|
||
test('unknown parameter (throws)', () { | ||
expect(() { | ||
ContentType.parse('text/pLaIn; wrong=utf-8'); | ||
}, throwsStateError); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.