This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webview_flutter] Add platform interface method
loadRequest
. (#4450)
- Loading branch information
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md
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
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
58 changes: 58 additions & 0 deletions
58
...ges/webview_flutter/webview_flutter_platform_interface/lib/src/types/webview_request.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,58 @@ | ||
// 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 'dart:typed_data'; | ||
|
||
/// Defines the supported HTTP methods for loading a page in [WebView]. | ||
enum WebViewRequestMethod { | ||
/// HTTP GET method. | ||
get, | ||
|
||
/// HTTP POST method. | ||
post, | ||
} | ||
|
||
/// Extension methods on the [WebViewRequestMethod] enum. | ||
extension WebViewRequestMethodExtensions on WebViewRequestMethod { | ||
/// Converts [WebViewRequestMethod] to [String] format. | ||
String serialize() { | ||
switch (this) { | ||
case WebViewRequestMethod.get: | ||
return 'get'; | ||
case WebViewRequestMethod.post: | ||
return 'post'; | ||
} | ||
} | ||
} | ||
|
||
/// Defines the parameters that can be used to load a page in the [WebView]. | ||
class WebViewRequest { | ||
/// Creates the [WebViewRequest]. | ||
WebViewRequest({ | ||
required this.uri, | ||
required this.method, | ||
this.headers = const {}, | ||
this.body, | ||
}); | ||
|
||
/// URI for the request. | ||
final Uri uri; | ||
|
||
/// HTTP method used to make the request. | ||
final WebViewRequestMethod method; | ||
|
||
/// Headers for the request. | ||
final Map<String, String> headers; | ||
|
||
/// HTTP body for the request. | ||
final Uint8List? body; | ||
|
||
/// Serializes the [WebViewRequest] to JSON. | ||
Map<String, dynamic> toJson() => { | ||
'uri': this.uri.toString(), | ||
'method': this.method.serialize(), | ||
'headers': this.headers, | ||
'body': this.body, | ||
}; | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...bview_flutter/webview_flutter_platform_interface/test/src/types/webview_request_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,39 @@ | ||
// 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 'dart:typed_data'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:webview_flutter_platform_interface/src/types/types.dart'; | ||
|
||
void main() { | ||
test('WebViewRequestMethod should serialize correctly', () { | ||
expect(WebViewRequestMethod.get.serialize(), 'get'); | ||
expect(WebViewRequestMethod.post.serialize(), 'post'); | ||
}); | ||
|
||
test('WebViewRequest should serialize correctly', () { | ||
WebViewRequest request; | ||
Map<String, dynamic> serializedRequest; | ||
// Test serialization without headers or a body | ||
request = WebViewRequest( | ||
uri: Uri.parse('https://flutter.dev'), | ||
method: WebViewRequestMethod.get, | ||
); | ||
serializedRequest = request.toJson(); | ||
expect(serializedRequest['uri'], 'https://flutter.dev'); | ||
expect(serializedRequest['method'], 'get'); | ||
expect(serializedRequest['headers'], {}); | ||
expect(serializedRequest['body'], null); | ||
// Test serialization of headers and body | ||
request = WebViewRequest( | ||
uri: Uri.parse('https://flutter.dev'), | ||
method: WebViewRequestMethod.get, | ||
headers: {'foo': 'bar'}, | ||
body: Uint8List.fromList('Example Body'.codeUnits), | ||
); | ||
serializedRequest = request.toJson(); | ||
expect(serializedRequest['headers'], {'foo': 'bar'}); | ||
expect(serializedRequest['body'], 'Example Body'.codeUnits); | ||
}); | ||
} |