Skip to content

Commit

Permalink
Added new BrowserMob alter method for updating the JSON object instea…
Browse files Browse the repository at this point in the history
…d of using RegeX. Very cool
  • Loading branch information
alombardorccl committed May 25, 2023
1 parent 9bb8f59 commit 40b4583
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<restassured.version>2.9.0</restassured.version>
<saucelabs-java.version>2.1.23</saucelabs-java.version>
<saucelabs.version>1.0.35</saucelabs.version>
<selenium-devtools.version>4.9.1</selenium-devtools.version>
<selenium.version>4.5.2</selenium.version>
<skip.plugins>false</skip.plugins>
<sortpom-maven-plugin.version>2.6.0</sortpom-maven-plugin.version>
Expand Down Expand Up @@ -254,8 +255,8 @@
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-devtools-v106</artifactId>
<version>${selenium.version}</version>
<artifactId>selenium-devtools-v113</artifactId>
<version>${selenium-devtools.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.proxy.CaptureType;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -190,6 +193,58 @@ public void initiateBrowser(final Credentials credentials) {
}
}

/**
* Alter any RESPONSE given the target URL.
*
* @param targetUrl endpoint to proxy and modify
* @param replace response text to replace
* @param parentField response top-most parent field to find off of
* @param fieldToReplace response inner field to replace contents of
*/
public void alterResponse(final String targetUrl, final String replace, final String parentField, final String fieldToReplace) {

getBrowserProxy().addResponseFilter((request, contents, messageInfo) -> {
if (StringUtils.containsIgnoreCase(messageInfo.getOriginalUrl(), targetUrl)) {
String originalResponseContents = contents.getTextContents();
JSONObject jsonObject = new JSONObject(originalResponseContents);
boolean successfulMock = false;
try {
JSONObject parentElement = jsonObject.getJSONObject(parentField);
JSONArray replacementArrayElement = new JSONArray(replace);
parentElement.put(fieldToReplace, replacementArrayElement);
successfulMock = true;
} catch (JSONException e) {
LOG(false, "Unable to alter array-based JSON e=%s", e.getMessage());
}

if (!successfulMock) {
try {
jsonObject.getJSONObject(parentField).put(fieldToReplace, replace);
successfulMock = true;
} catch (JSONException e) {
LOG(false, "Unable to alter parent-child JSON e=%s", e.getMessage());
}
}
if (!successfulMock) {
try {
jsonObject.getJSONObject(fieldToReplace).put(fieldToReplace, replace);
successfulMock = true;
} catch (JSONException e) {
LOG(false, "Unable to simple JSON e=%s", e.getMessage());
}
}

String updatedContents;
if (!successfulMock) {
updatedContents = originalResponseContents;
} else {
updatedContents = jsonObject.toString();
}
contents.setTextContents(updatedContents);
}
});
}

/**
* Alter any RESPONSE given the target URL.
*
Expand Down

0 comments on commit 40b4583

Please sign in to comment.