Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ To manually process requests:
}
```

For both cases you can choose between different strategies for how the recorded requests (including
the body) and the intercepted requests are matched together. By default, only the url is used. If
you want to use a different strategy, for example if you have parallel requests to the same url with
different bodies (e.g. GraphQL queries), you can pass a custom `RequestMatcher` to the constructor
of `RequestInspectorWebViewClient`:

```kotlin
val webView = WebView(this)
webView.webViewClient = RequestInspectorWebViewClient(
webView,
matcher = RequestGeneratedUuidInHeaderMatcher()
)
```

Currently available matchers are `GeneratedUuidInHeaderRequestMatcher` and
`GeneratedUuidInUrlRequestMatcher`, which both create an UUID and add it to the request before it's
recorded and sent from JavaScript. They only differ by how they attach the UUID to the request, as
an additional header or as an additional query param. But both clean up the request when it's
intecepted on native side, so before it's really sent out to the target.

If you want to implement your own matching strategy, you can implement the `RequestMatcher`
interface and pass an instance of it to the constructor of `RequestInspectorWebViewClient`.

Known limitations
===

Expand Down
27 changes: 22 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ group = "com.acsbendi"
version = currentVersion

android {
compileSdk = 31
compileSdk = 36
namespace = "com.acsbendi.requestinspectorwebview"

defaultConfig {
minSdk = 21
targetSdk = 31

testOptions {
targetSdk = 36
}
lint {
targetSdk = 36
}
version = currentVersion
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -28,8 +33,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

publishing {
Expand All @@ -39,6 +44,10 @@ android {
}
}

kotlin {
jvmToolchain(21)
}

publishing {
publications {
register<MavenPublication>("release") {
Expand All @@ -52,3 +61,11 @@ publishing {
}
}
}

dependencies {
implementation("androidx.core:core-ktx:1.17.0")

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test:rules:1.5.0")
}
30 changes: 30 additions & 0 deletions app/src/androidTest/assets/test_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="test_requests.js"></script>
</head>
<body>
<form id="formGet" action="https://example.com/form-get" method="GET">
<input type="text" name="query" value="hello">
<input type="hidden" name="source" value="test">
</form>

<form id="formPostUrlEncoded" action="https://example.com/form-post-urlencoded" method="POST"
enctype="application/x-www-form-urlencoded">
<input type="text" name="username" value="testuser">
<input type="password" name="password" value="secret123">
</form>

<form id="formPostMultipart" action="https://example.com/form-post-multipart" method="POST"
enctype="multipart/form-data">
<input type="text" name="field1" value="value1">
<input type="text" name="field2" value="value2">
</form>

<form id="formPostPlainText" action="https://example.com/form-post-plaintext" method="POST"
enctype="text/plain">
<input type="text" name="message" value="hello world">
</form>
</body>
</html>
41 changes: 41 additions & 0 deletions app/src/androidTest/assets/test_requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function triggerFetchStringUrl() {
fetch('https://example.com/api/data', {
method: 'POST',
body: '{"key":"value"}',
headers: {
'Content-Type': 'application/json',
'X-Custom': 'test-value'
}
});
}

function triggerFetchRequestObject() {
fetch(new Request('https://example.com/api/request', {
method: 'PUT',
headers: { 'X-Request-Header': 'request-value' }
}));
}

function triggerXhr() {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://example.com/api/xhr', true);
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.setRequestHeader('X-Xhr-Header', 'xhr-value');
xhr.send('xhr-body-content');
}

function submitFormGet() {
document.getElementById('formGet').submit();
}

function submitFormPostUrlEncoded() {
document.getElementById('formPostUrlEncoded').submit();
}

function submitFormPostMultipart() {
document.getElementById('formPostMultipart').submit();
}

function submitFormPostPlainText() {
document.getElementById('formPostPlainText').submit();
}

This file was deleted.

Loading