Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter]Add the loadRequest method #4169

Closed
wants to merge 87 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
9eb8dd3
Add postUrl method to platform interface
ydag Jul 20, 2021
957ba7b
Add postUrl method to method channel
ydag Jul 20, 2021
b361ed7
Add postUrl method to app-facing
ydag Jul 20, 2021
5ba03b8
Add postUrl call to OnMethodCall on Android
ydag Jul 20, 2021
a99ce24
Add postUrl method on Android
ydag Jul 20, 2021
730018c
Add mockito dependency
ydag Jul 20, 2021
aafea35
Add a constructor for testing
ydag Jul 20, 2021
2c60a45
Add unit tests on Android
ydag Jul 20, 2021
d7d54f3
Add postUrl method call on iOS
ydag Jul 20, 2021
58121df
Add onPostUrl method
ydag Jul 20, 2021
83557f8
Add postRequest method
ydag Jul 20, 2021
8a6a524
Add postUrl method
ydag Jul 20, 2021
b941672
Add new constructor for testing
ydag Jul 20, 2021
6233114
Add mocking for testing on iOS
ydag Jul 20, 2021
1bc51fc
Add unit tests on iOS
ydag Jul 20, 2021
3c0b775
Update version and CHANGELOG
ydag Jul 20, 2021
377a0f8
Fix format
ydag Jul 20, 2021
68463e6
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 21, 2021
c405740
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 23, 2021
6fa4255
Add DisplayListenerProxy to constructor
ydag Jul 26, 2021
677f8b5
Move the unit test to FlutterWebViewTest class
ydag Jul 26, 2021
8663b07
Fix the format
ydag Jul 26, 2021
3a7d9c7
Merge branch 'master' into webview_flutter/add_post_url
ydag Jul 26, 2021
56c2aba
Change names of unit tests
ydag Jul 26, 2021
5aac75a
Make postUrl private
ydag Jul 26, 2021
a8ded90
Call public method instead private
ydag Jul 26, 2021
b2d364d
Remove constructor
ydag Jul 26, 2021
a260ca3
Remove initWithWebView constructor
ydag Jul 26, 2021
4936dda
Add createFLTWKWebViewWithFrame method
ydag Jul 26, 2021
37c7930
Create MockFLTWebViewController to mock FLTWKWebView
ydag Jul 26, 2021
bb6c67c
Refactor unit tests with the new implementation
ydag Jul 26, 2021
96c4922
Add method comments
ydag Jul 27, 2021
7253d1a
Refactor postRequest method
ydag Jul 27, 2021
2332582
Add buildNSURLRequest and remove postUrl, postRequest
ydag Jul 28, 2021
495a6d7
Refactor unit tests with the new implementation
ydag Jul 28, 2021
a6b5dc2
Add method comments
ydag Jul 28, 2021
0b41767
Remove comments
ydag Jul 28, 2021
a7567f3
Add WebViewRequest and WebViewLoadMethod
ydag Aug 9, 2021
22bd781
Refactor loadUrl method with request object
ydag Aug 9, 2021
af45469
Refactor loadUrl method in the app-facing
ydag Aug 9, 2021
a496344
Refactor loadUrl method
ydag Aug 9, 2021
d04e74a
Refactor tests after changes in loadUrl method.
ydag Aug 9, 2021
916ae97
Refactor integration test after changes in loadUrl method.
ydag Aug 9, 2021
8bfaa6a
Refactor buildNSURLRequest
ydag Aug 9, 2021
215a4e1
Remove loadRequest and loadUrl methods
ydag Aug 9, 2021
9264a6a
Refactor onLoadUrl to unify all HTTP load with URL methods
ydag Aug 9, 2021
5dbe50a
Refactor unit tests
ydag Aug 9, 2021
ea8b782
Add WebViewRequest
ydag Aug 9, 2021
b581476
Add CustomHttpPostRequest
ydag Aug 9, 2021
3d5c954
Add to instantiate CustomHttpPostRequest
ydag Aug 9, 2021
f43c386
Add createCustomHttpPostRequest method
ydag Aug 9, 2021
7a74436
Refactor loadUrl to unify all HTTP load with URL methods
ydag Aug 9, 2021
83da7ee
Refactor unit tests
ydag Aug 9, 2021
7ae1821
Add unit tests
ydag Aug 9, 2021
88a6db7
Merge branch 'master' into webview_flutter/add_post_url
ydag Aug 9, 2021
4fc237c
Update the CHANGELOG and version
ydag Aug 9, 2021
00814be
Fix the comment
ydag Aug 9, 2021
91f152b
Add licence comment
ydag Aug 9, 2021
562fec9
Add licence on android
ydag Aug 9, 2021
9c85d79
Remove unused import
ydag Aug 9, 2021
28f03cd
Add Uri to get URLs and set headers empty by default
ydag Aug 19, 2021
7f4b50b
Add loadRequest
ydag Aug 19, 2021
92448f6
Add loadRequest and refactor comment
ydag Aug 19, 2021
2f7b146
Add _validateUri method
ydag Aug 19, 2021
e71a7de
Add loadRequest and deprecate loadUrl
ydag Aug 19, 2021
fbbf1da
Revert changes on tests
ydag Aug 19, 2021
ec8f786
Fix the comment
ydag Aug 19, 2021
5711c5c
Refactor WebViewRequest
ydag Aug 19, 2021
3c0cbf3
Add loadRequest and deprecate loadUrl
ydag Aug 19, 2021
9db2760
Refactor unit tests on Android
ydag Aug 19, 2021
8b030be
Add onLoadRequest
ydag Aug 20, 2021
f5e5d21
Refactor according to changes on request
ydag Aug 20, 2021
3608d40
Change the method to onLoadRequest
ydag Aug 20, 2021
7862d30
Refactor test after changes on buildNSURLRequest
ydag Aug 20, 2021
b70b227
Fix unit tests
ydag Aug 27, 2021
a37d770
Revert changes for deprecated method
ydag Aug 27, 2021
433218c
Update version and CHANGELOG
ydag Aug 27, 2021
b702e1b
Merge branch 'master' into webview_flutter/add_post_url
ydag Aug 27, 2021
64be755
Fix the import
ydag Aug 27, 2021
17a30f0
Refactor headers to make it empty collections instead nullable
ydag Aug 27, 2021
58fdf9e
Add ignore for deprecated method
ydag Aug 27, 2021
9c78996
Fix parameter
ydag Aug 31, 2021
139b377
Java: Added additional tests and small refactors
BeMacized Aug 31, 2021
374bd47
Merge remote-tracking branch 'origin/webview_flutter/add_post_url' in…
BeMacized Aug 31, 2021
c095dc3
Fix tests
BeMacized Aug 31, 2021
817abe0
Add additional tests
BeMacized Aug 31, 2021
27692b3
Add license to test classes
ydag Sep 2, 2021
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
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0

* Add postUrl to use POST request and accept POST data.

## 2.0.10

* Fix keyboard issues link in the README.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ android {
implementation 'androidx.annotation:annotation:1.0.0'
implementation 'androidx.webkit:webkit:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.10.0'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -79,6 +80,14 @@ public void onProgressChanged(WebView view, int progress) {
}
}

@VisibleForTesting
FlutterWebView(WebView webView, MethodChannel methodChannel, Handler platformThreadHandler) {
this.webView = webView;
this.methodChannel = methodChannel;
flutterWebViewClient = new FlutterWebViewClient(methodChannel);
this.platformThreadHandler = platformThreadHandler;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("unchecked")
FlutterWebView(
Expand Down Expand Up @@ -193,6 +202,9 @@ public void onMethodCall(MethodCall methodCall, Result result) {
case "loadUrl":
loadUrl(methodCall, result);
break;
case "postUrl":
postUrl(methodCall, result);
break;
case "updateSettings":
updateSettings(methodCall, result);
break;
Expand Down Expand Up @@ -258,6 +270,14 @@ private void loadUrl(MethodCall methodCall, Result result) {
result.success(null);
}

public void postUrl(MethodCall methodCall, Result result) {
Map<String, Object> request = (Map<String, Object>) methodCall.arguments;
String url = (String) request.get("url");
byte[] postData = (byte[]) request.get("postData");
webView.postUrl(url, postData);
result.success(null);
}

private void canGoBack(Result result) {
result.success(webView.canGoBack());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,83 @@
package io.flutter.plugins.webviewflutter;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doNothing;

import android.os.Handler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.matchers.Null;

public class WebViewTest {
private static final String POST_URL = "postUrl";
private static final String URL = "www.example.com";
byte[] postData;

@Mock WebView mockWebView;
@Mock MethodChannel mockChannel;
@Mock Handler mockHandler;
@Mock MethodChannel.Result mockResult;

FlutterWebView flutterWebView;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
postData = new byte[5];
}

@Test
public void testPostUrl_should_call_webView_postUrl_with_correct_url() {
MethodCall call = buildMethodCall(POST_URL, URL, postData);
flutterWebView = new FlutterWebView(mockWebView, mockChannel, mockHandler);

ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);

doNothing().when(mockWebView).postUrl(valueCapture.capture(), isA(byte[].class));

flutterWebView.postUrl(call, mockResult);

assertEquals(URL, valueCapture.getValue());
}

@Test
public void testPostUrl_should_call_webView_postUrl_with_correct_http_body() {
MethodCall call = buildMethodCall(POST_URL, URL, postData);
flutterWebView = new FlutterWebView(mockWebView, mockChannel, mockHandler);

ArgumentCaptor<byte[]> valueCapture = ArgumentCaptor.forClass(byte[].class);

doNothing().when(mockWebView).postUrl(isA(String.class), valueCapture.capture());

flutterWebView.postUrl(call, mockResult);

assertEquals(postData, valueCapture.getValue());
}

@Test
public void testPostUrl_should_call_result_success_with_null() {
MethodCall call = buildMethodCall(POST_URL, URL, postData);
flutterWebView = new FlutterWebView(mockWebView, mockChannel, mockHandler);

ArgumentCaptor<Null> valueCapture = ArgumentCaptor.forClass(Null.class);

doNothing().when(mockResult).success(valueCapture.capture());

flutterWebView.postUrl(call, mockResult);

assertEquals(null, valueCapture.getValue());
}

@Test
public void errorCodes() {
assertEquals(
Expand Down Expand Up @@ -46,4 +118,12 @@ public void errorCodes() {
FlutterWebViewClient.errorCodeToString(WebViewClient.ERROR_UNSUPPORTED_SCHEME),
"unsupportedScheme");
}

private MethodCall buildMethodCall(String method, final String url, final byte[] postData) {
final Map<String, Object> arguments = new HashMap<>();
arguments.put("url", url);
arguments.put("postData", postData);

return new MethodCall(method, arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,219 @@

static bool feq(CGFloat a, CGFloat b) { return fabs(b - a) < FLT_EPSILON; }

@interface FLTWebViewController (Test)
- (bool)postUrl:(NSString *)url withBody:(FlutterStandardTypedData *)postData;
- (void)onPostUrl:(FlutterMethodCall *)call result:(FlutterResult)result;
- (bool)postRequest:(NSDictionary<NSString *, id> *)request;
@end

@interface MockFLTWebViewControllerForOnPostUrl : FLTWebViewController
- (instancetype)initWithPostRequest:(BOOL)postRequestResult;
@end

@implementation MockFLTWebViewControllerForOnPostUrl {
bool _postRequestResult;
}

- (instancetype)initWithPostRequest:(bool)postRequestResult {
_postRequestResult = postRequestResult;
return self;
}

- (bool)postRequest:(NSDictionary<NSString *, id> *)request {
return _postRequestResult;
}

@end

@interface MockFLTWebViewControllerForPostRequest : FLTWebViewController
- (instancetype)initWithPostUrl:(BOOL)postUrlResult;
@end

@implementation MockFLTWebViewControllerForPostRequest {
bool _postUrlResult;
}

- (instancetype)initWithPostUrl:(bool)postUrlResult {
_postUrlResult = postUrlResult;
return self;
}

- (bool)postUrl:(NSString *)url withBody:(FlutterStandardTypedData *)postData {
return _postUrlResult;
}

@end

@interface MockWKWebViewForPostUrl : FLTWKWebView
@property(nonatomic, nullable) NSMutableURLRequest *receivedResult;
@end

@implementation MockWKWebViewForPostUrl

- (WKNavigation *)loadRequest:(NSMutableURLRequest *)request {
_receivedResult = request;
return nil;
}

@end

@interface FLTWebViewTests : XCTestCase

@property(strong, nonatomic) NSObject<FlutterBinaryMessenger> *mockBinaryMessenger;

@property(readonly, nonatomic) MockWKWebViewForPostUrl *resultObject;

@end

@implementation FLTWebViewTests

- (void)setUp {
[super setUp];
self.mockBinaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
_resultObject = [MockWKWebViewForPostUrl new];
}

- (void)testPostUrl_should_return_false_when_url_is_nil {
// Initialise data
NSString *url = nil;
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
FlutterStandardTypedData *postData = [FlutterStandardTypedData typedDataWithBytes:data];

FLTWebViewController *controller = [[FLTWebViewController alloc] initWithWebView:_resultObject];

// Run test
bool result = [controller postUrl:url withBody:postData];

XCTAssertFalse(result);
}

- (void)testPostUrl_should_return_true_when_url_is_not_nil {
// Initialise data
NSString *url = @"http://example.com";
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
FlutterStandardTypedData *postData = [FlutterStandardTypedData typedDataWithBytes:data];

FLTWebViewController *controller = [[FLTWebViewController alloc] initWithWebView:_resultObject];

// Run test
bool result = [controller postUrl:url withBody:postData];
NSString *decodedHTTPBody = [[NSString alloc] initWithData:_resultObject.receivedResult.HTTPBody
encoding:NSUTF8StringEncoding];

XCTAssertTrue(result);
XCTAssertTrue([decodedHTTPBody isEqualToString:str]);
XCTAssertTrue([_resultObject.receivedResult.HTTPMethod isEqualToString:@"POST"]);
XCTAssertTrue([_resultObject.receivedResult.URL.absoluteString isEqualToString:url]);
}

- (void)testOnPostUrl_should_call_result_flutter_error_when_postRequest_return_false {
MockFLTWebViewControllerForOnPostUrl *mockController =
[[MockFLTWebViewControllerForOnPostUrl alloc] initWithPostRequest:false];

__block FlutterError *result = nil;

[mockController onPostUrl:nil
result:^(id _Nullable r) {
result = r;
}];

XCTAssertEqualObjects(result.code, @"postUrl_failed");
}

- (void)testOnPostUrl_should_call_result_nil_when_postRequest_return_true {
MockFLTWebViewControllerForOnPostUrl *mockController =
[[MockFLTWebViewControllerForOnPostUrl alloc] initWithPostRequest:true];

__block id result = @"test";

[mockController onPostUrl:nil
result:^(id _Nullable r) {
result = r;
}];

XCTAssertEqual(result, nil);
}

- (void)testPostRequest_should_return_false_when_request_is_nil {
FLTWebViewController *controller = [[FLTWebViewController alloc] initWithWebView:_resultObject];

bool result = [controller postRequest:nil];

XCTAssertFalse(result);
}

- (void)testPostRequest_should_return_false_when_postUrl_return_false {
NSString *url = @"http://example.com";
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
FlutterStandardTypedData *postData = [FlutterStandardTypedData typedDataWithBytes:data];

FlutterMethodCall *call =
[FlutterMethodCall methodCallWithMethodName:@"postUrl"
arguments:@{@"url" : url, @"postData" : postData}];

MockFLTWebViewControllerForPostRequest *mockController =
[[MockFLTWebViewControllerForPostRequest alloc] initWithPostUrl:false];

bool result = [mockController postRequest:[call arguments]];

XCTAssertFalse(result);
}

- (void)testPostRequest_should_return_true_when_postUrl_return_true {
NSString *url = @"http://example.com";
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
FlutterStandardTypedData *postData = [FlutterStandardTypedData typedDataWithBytes:data];

FlutterMethodCall *call =
[FlutterMethodCall methodCallWithMethodName:@"postUrl"
arguments:@{@"url" : url, @"postData" : postData}];

MockFLTWebViewControllerForPostRequest *mockController =
[[MockFLTWebViewControllerForPostRequest alloc] initWithPostUrl:true];

bool result = [mockController postRequest:[call arguments]];

XCTAssertTrue(result);
}

- (void)testPostRequest_should_return_false_when_url_is_not_NSString {
NSError *url = [NSError new];
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
FlutterStandardTypedData *postData = [FlutterStandardTypedData typedDataWithBytes:data];

FlutterMethodCall *call =
[FlutterMethodCall methodCallWithMethodName:@"postUrl"
arguments:@{@"url" : url, @"postData" : postData}];

MockFLTWebViewControllerForPostRequest *mockController =
[[MockFLTWebViewControllerForPostRequest alloc] initWithPostUrl:true];

bool result = [mockController postRequest:[call arguments]];

XCTAssertFalse(result);
}

- (void)testPostRequest_should_return_false_when_postData_is_not_FlutterStandardTypedData {
NSString *url = @"http://example.com";
NSString *str = [NSString stringWithFormat:@"name=%@&pass=%@", @"john", @"123"];
NSData *postData = [str dataUsingEncoding:NSUTF8StringEncoding];

FlutterMethodCall *call =
[FlutterMethodCall methodCallWithMethodName:@"postUrl"
arguments:@{@"url" : url, @"postData" : postData}];

MockFLTWebViewControllerForPostRequest *mockController =
[[MockFLTWebViewControllerForPostRequest alloc] initWithPostUrl:true];

bool result = [mockController postRequest:[call arguments]];

XCTAssertFalse(result);
}

- (void)testCanInitFLTWebViewController {
Expand Down
Loading