1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-27 18:21:14 +03:00

Support header redaction in HttpLoggingInterceptor (#4287)

Add redactHeader() method that allows clients to mark (case insensitive) headers carrying private data that can't be logged.
This commit is contained in:
Amir Livneh
2018-09-24 14:44:56 -04:00
committed by Yuri Schimke
parent ea10cb4715
commit d2ddd4ef9d
2 changed files with 77 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
@@ -745,6 +746,63 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs();
}
@Test
public void headersAreRedacted() throws Exception {
HttpLoggingInterceptor networkInterceptor =
new HttpLoggingInterceptor(networkLogs).setLevel(Level.HEADERS);
networkInterceptor.redactHeader("sEnSiTiVe");
HttpLoggingInterceptor applicationInterceptor =
new HttpLoggingInterceptor(applicationLogs).setLevel(Level.HEADERS);
applicationInterceptor.redactHeader("sEnSiTiVe");
client =
new OkHttpClient.Builder()
.addNetworkInterceptor(networkInterceptor)
.addInterceptor(applicationInterceptor)
.build();
server.enqueue(
new MockResponse().addHeader("SeNsItIvE", "Value").addHeader("Not-Sensitive", "Value"));
Response response =
client
.newCall(
request()
.addHeader("SeNsItIvE", "Value")
.addHeader("Not-Sensitive", "Value")
.build())
.execute();
response.body().close();
applicationLogs
.assertLogEqual("--> GET " + url)
.assertLogEqual("SeNsItIvE: ██")
.assertLogEqual("Not-Sensitive: Value")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("SeNsItIvE: ██")
.assertLogEqual("Not-Sensitive: Value")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET " + url + " http/1.1")
.assertLogEqual("SeNsItIvE: ██")
.assertLogEqual("Not-Sensitive: Value")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("SeNsItIvE: ██")
.assertLogEqual("Not-Sensitive: Value")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
}
private Request.Builder request() {
return new Request.Builder().url(url);
}