@@ -903,6 +903,104 @@ class HttpLoggingInterceptorTest {
903
903
.assertNoMoreLogs()
904
904
}
905
905
906
+ @Suppress(" INVISIBLE_MEMBER" , " INVISIBLE_REFERENCE" )
907
+ @Test
908
+ fun sensitiveQueryParamsAreRedacted () {
909
+ url = server.url(" /api/login?user=test_user&authentication=basic&password=confidential_password" )
910
+ val networkInterceptor =
911
+ HttpLoggingInterceptor (networkLogs).setLevel(
912
+ Level .BASIC ,
913
+ )
914
+ networkInterceptor.redactQueryParams(" user" , " passWord" )
915
+
916
+ val applicationInterceptor =
917
+ HttpLoggingInterceptor (applicationLogs).setLevel(
918
+ Level .BASIC ,
919
+ )
920
+ applicationInterceptor.redactQueryParams(" user" , " PassworD" )
921
+
922
+ client =
923
+ OkHttpClient .Builder ()
924
+ .addNetworkInterceptor(networkInterceptor)
925
+ .addInterceptor(applicationInterceptor)
926
+ .build()
927
+ server.enqueue(
928
+ MockResponse .Builder ()
929
+ .build(),
930
+ )
931
+ val response =
932
+ client
933
+ .newCall(
934
+ request()
935
+ .build(),
936
+ )
937
+ .execute()
938
+ response.body.close()
939
+ val redactedUrl = networkInterceptor.redactUrl(url)
940
+ val redactedUrlPattern = redactedUrl.replace(" ?" , """ \?""" )
941
+ applicationLogs
942
+ .assertLogEqual(" --> GET $redactedUrl " )
943
+ .assertLogMatch(Regex (""" <-- 200 OK $redactedUrlPattern \(\d+ms, \d+-byte body\)""" ))
944
+ .assertNoMoreLogs()
945
+ networkLogs
946
+ .assertLogEqual(" --> GET $redactedUrl http/1.1" )
947
+ .assertLogMatch(Regex (""" <-- 200 OK $redactedUrlPattern \(\d+ms, \d+-byte body\)""" ))
948
+ .assertNoMoreLogs()
949
+ }
950
+
951
+ @Suppress(" INVISIBLE_MEMBER" , " INVISIBLE_REFERENCE" )
952
+ @Test
953
+ fun preserveQueryParamsAfterRedacted () {
954
+ url =
955
+ server.url(
956
+ """ /api/login?
957
+ |user=test_user&
958
+ |authentication=basic&
959
+ |password=confidential_password&
960
+ |authentication=rather simple login method
961
+ """ .trimMargin(),
962
+ )
963
+ val networkInterceptor =
964
+ HttpLoggingInterceptor (networkLogs).setLevel(
965
+ Level .BASIC ,
966
+ )
967
+ networkInterceptor.redactQueryParams(" user" , " passWord" )
968
+
969
+ val applicationInterceptor =
970
+ HttpLoggingInterceptor (applicationLogs).setLevel(
971
+ Level .BASIC ,
972
+ )
973
+ applicationInterceptor.redactQueryParams(" user" , " PassworD" )
974
+
975
+ client =
976
+ OkHttpClient .Builder ()
977
+ .addNetworkInterceptor(networkInterceptor)
978
+ .addInterceptor(applicationInterceptor)
979
+ .build()
980
+ server.enqueue(
981
+ MockResponse .Builder ()
982
+ .build(),
983
+ )
984
+ val response =
985
+ client
986
+ .newCall(
987
+ request()
988
+ .build(),
989
+ )
990
+ .execute()
991
+ response.body.close()
992
+ val redactedUrl = networkInterceptor.redactUrl(url)
993
+ val redactedUrlPattern = redactedUrl.replace(" ?" , """ \?""" )
994
+ applicationLogs
995
+ .assertLogEqual(" --> GET $redactedUrl " )
996
+ .assertLogMatch(Regex (""" <-- 200 OK $redactedUrlPattern \(\d+ms, \d+-byte body\)""" ))
997
+ .assertNoMoreLogs()
998
+ networkLogs
999
+ .assertLogEqual(" --> GET $redactedUrl http/1.1" )
1000
+ .assertLogMatch(Regex (""" <-- 200 OK $redactedUrlPattern \(\d+ms, \d+-byte body\)""" ))
1001
+ .assertNoMoreLogs()
1002
+ }
1003
+
906
1004
@Test
907
1005
fun duplexRequestsAreNotLogged () {
908
1006
platform.assumeHttp2Support()
0 commit comments