From 8bbeb8d7c8a009bc0ea0407ea04a8fad1c534555 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 14 Apr 2019 19:03:46 -0700 Subject: [PATCH] Better query parameters handling in getSanitizedMessage(). --- .../erik/mobibot/modules/ModuleException.java | 16 ++++++++-------- ...erterTest.java => CurrencyConverterTest.java} | 0 .../mobibot/modules/ModuleExceptionTest.java | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) rename src/test/java/net/thauvin/erik/mobibot/modules/{CurrentConverterTest.java => CurrencyConverterTest.java} (100%) diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java index 9bf8788..9c3009c 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.java @@ -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(); } /** diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/CurrentConverterTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.java similarity index 100% rename from src/test/java/net/thauvin/erik/mobibot/modules/CurrentConverterTest.java rename to src/test/java/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.java diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.java index b3b7a71..aa639e3 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.java @@ -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"); + } } } }