Better query parameters handling in getSanitizedMessage().

This commit is contained in:
Erik C. Thauvin 2019-04-14 19:03:46 -07:00
parent 4b772f0564
commit 8bbeb8d7c8
3 changed files with 20 additions and 12 deletions

View file

@ -46,7 +46,7 @@ import java.util.regex.Pattern;
class ModuleException extends Exception {
private static final long serialVersionUID = -3036774290621088107L;
private final String debugMessage;
private final Pattern urlPattern = Pattern.compile("(https?://\\S+)");
private final Pattern urlPattern = Pattern.compile("(https?://\\S+)(\\?\\S+)");
/**
* Creates a new exception.
@ -100,20 +100,20 @@ class ModuleException extends Exception {
final String causeMessage = getCause().getMessage();
final Matcher matcher = urlPattern.matcher(causeMessage);
if (matcher.find()) {
final HttpUrl url = HttpUrl.parse(matcher.group());
if ((url != null) && (matcher.group().contains("?"))) {
final StringBuilder query = new StringBuilder();
final HttpUrl url = HttpUrl.parse(matcher.group(1)+matcher.group(2));
if (url != null){
final StringBuilder query = new StringBuilder("?");
for (int i = 0, size = url.querySize(); i < size; i++) {
if (i > 0) query.append('&');
query.append(url.queryParameterName(i)).append('=').append('[')
.append(url.queryParameterValue(i).length()).append(']').append('&');
.append(url.queryParameterValue(i).length()).append(']');
}
return getDebugMessage() + "\nCaused by: " + getCause().getClass().getName() + ": "
+ causeMessage.replace(matcher.group(),
matcher.group().substring(0, matcher.group().indexOf('?') + 1) + query);
+ causeMessage.replace(matcher.group(2), query);
}
}
}
return getMessage();
return getDebugMessage() + "\nCaused by: " + getCause().getClass().getName() + ": " + getCause().getMessage();
}
/**

View file

@ -49,9 +49,12 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ModuleExceptionTest {
@DataProvider(name = "dp")
Object[][] createData(final Method m) {
System.out.println(m.getName()); // print test method name
return new Object[][]{new Object[]{new ModuleException("debugMessage", "message",
new IOException("Secret URL http://foo.com?apiKey=sec&userID=me"))},
new Object[]{new ModuleException("debugMessage", "message",
new IOException("URL http://foobar.com"))},
new Object[]{new ModuleException("debugMessage", "message",
new IOException("URL http://foobar.com?"))},
new Object[]{new ModuleException("debugMessage", "message")}
};
}
@ -67,10 +70,15 @@ public class ModuleExceptionTest {
}
@Test(dataProvider = "dp")
final void testGetStanitizedMessage(final ModuleException e) {
final void testGetSanitizedMessage(final ModuleException e) {
if (e.hasCause()) {
assertThat(e.getSanitizedMessage()).as("get sanitzed url")
.contains("http://foo.com?apiKey=[3]&userID=[2]");
if (e.getSanitizedMessage().contains("Secret")) {
assertThat(e.getSanitizedMessage()).as("get sanitized url")
.contains("http://foo.com?apiKey=[3]&userID=[2]");
} else {
assertThat(e.getSanitizedMessage()).as("get sanitized url")
.contains("http://foobar.com");
}
}
}
}