HttpUtils { public static String hideApiKeys(@NonNull String apiKeys) { StringBuilder buffer = new StringBuilder(); Matcher matcher = API_KEY_PATTERN.matcher(apiKeys); int lastEnd = 0; while (matcher.find()) { buffer.append(apiKeys.substring(lastEnd, matcher.start())); buffer.append("-***"); buffer.append(matcher.group(1)); lastEnd = matcher.end(); } if (lastEnd < apiKeys.length()) { buffer.append(apiKeys.substring(lastEnd)); } return buffer.toString(); } @VisibleForTesting  HttpUtils(); static boolean isRecoverableError(Throwable t); static String hideSecret(@NonNull String secret); static String hideApiKeys(@NonNull String apiKeys); static String hideTickets(@NonNull String tickets); static HttpClient createHttpClient(@NonNull Context context); static HttpClient createHttpClient(@NonNull Context context, boolean compressionEnabled); static HttpClient createHttpClientWithoutRetryer(@NonNull Context context, boolean compressionEnabled); @NonNull static HttpsURLConnection createHttpsConnection(@NonNull URL url); static final int THREAD_STATS_TAG; static final int READ_BUFFER_SIZE; static final int WRITE_BUFFER_SIZE; static final int CONNECT_TIMEOUT; static final int READ_TIMEOUT; }
@Test public void hide1ApiKey() { String key = "bb8b06dfcd8f4b4f925cca3e980fcdc4-a8b05ade-4dd6-4fd1-8dc7-c76b0cbc6bdf-1234"; String hiddenKeys = HttpUtils.hideApiKeys(key); assertEquals("bb8b06dfcd8f4b4f925cca3e980fcdc4-***", hiddenKeys); }
@Test public void hide2ApiKeys() { String key1 = "bb8b06dfcd8f4b4f925cca3e980fcdc4-a8b05ade-4dd6-4fd1-8dc7-c76b0cbc6bdf-1234"; String key2 = "6721516c7c754b49ba4ec5e7170a0c4c-a5f44609-30df-4fae-9202-49836a6cf71d-4567"; String keys = key1 + "," + key2; String hiddenKeys = HttpUtils.hideApiKeys(keys); assertEquals("bb8b06dfcd8f4b4f925cca3e980fcdc4-***,6721516c7c754b49ba4ec5e7170a0c4c-***", hiddenKeys); }
@Test public void hideEmptyApiKeys() { assertEquals("", HttpUtils.hideApiKeys("")); }
@Test public void hideInvalidApiKey() { assertEquals("asIs", HttpUtils.hideApiKeys("asIs")); }