HttpFormatter { static String urlWithRedactedToken( String rawUrl ) { String[] urlSplitOnToken = rawUrl.split("token="); String prefix = urlSplitOnToken[0]; String rest = urlSplitOnToken[1]; String[] restOfUrlSplit = rest.split("&", 2); String token = restOfUrlSplit[0]; StringBuilder redactedUrl = new StringBuilder(); redactedUrl.append(prefix) .append("token=").append(redactedToken(token)); if (restOfUrlSplit.length > 1) { redactedUrl.append("&").append(restOfUrlSplit[1]); } return redactedUrl.toString(); }    }
@Test public void itDoesElideAuthTokensInUrl() throws Exception { assertThat( HttpFormatter.urlWithRedactedToken( "https: ) ) .describedAs("If it looks like a URL with only the raw token, we should redact the full token, providing just a fingerprint") .isEqualTo("https: assertThat( HttpFormatter.urlWithRedactedToken( "https: ) ) .describedAs("If it looks like a URL with the raw token and other query params, we should redact the full token, providing just a fingerprint") .isEqualTo("https: }