GH-359: Fix beforeload to work with POST requests #367
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Platforms affected
iOS and Android
What does this PR do?
Fixes the behaviour of
beforeloadto resolve the problem with POST requests outlined in #359.The
beforeloadparameter has been changed from taking only a boolean (yesor not defined) to a discrete string with possible values ofget,post, oryeswhich correspond to request types of GET, POST or GET&POST respectively. TheREADME.mdhas been updated to reflect this.Note that use of
beforeloadto intercept POST requests is currently not supported on Android or iOS, so ifbeforeload=yesis specified and a POST request is detected as the HTTP request method,beforeloadbehaviour will not be applied. Ifbeforeload=postis specified, aloaderrorevent will be dispatched which states that POST requests are not yet supported.Notes for Android
The
shouldOverrideUrlLoading()override method has been updated to support the new method interface added in API 24 / Android 7 which receives theWebResourceRequestinstead of just theString url, enabling the HTTP method of the request to be determined. The deprecated method interface has also been preserved for API <=23, but in this case the HTTP method cannot be determined so is passed as null.Also note that due to a Chromium bug,
shouldOverrideUrlLoading()is currently not called for POST requests. It's possible this may be resolved in a future Chromium version in the Android System Webview (given that this is now self-updating and independent of Android version since Android 5.0) - in prospective anticipation of this, code to handle POST requests has been added toshouldOverrideUrlLoading().However, it seems more likely that this won't be resolved any time soon given that a Chromium dev said:
Therefore if we want to go ahead and use
beforeloadto intercept request types other than GET, it's likely we'll instead need to use theshouldInterceptRequest()method override. As withshouldOverrideUrlLoading(), there are a two variants: the new method interface added in API 21 / Android 5.0 which which receives theWebResourceRequestobject and the deprecated one which receives onlyString url. If we want to determine the HTTP request method, we'll need to use the new implementation. This has been empirically tested and is called for POST requests so would allow the possibility to intercept, delay, modify and send the POST request and its data viabeforeload.Both
shouldInterceptRequest()method interfaces have been exposed in the Android implentation for potential future use but they currently do nothing other than return the unadulterated request object.What testing has been done on this change?
Manual testing of POST and GET requests on both platforms using a test app container:
https://github.com/dpa99c/cordova-plugin-inappbrowser-test