26
26
import com .epam .reportportal .listeners .LogLevel ;
27
27
import com .epam .reportportal .restassured .support .HttpEntityFactory ;
28
28
import com .epam .reportportal .service .ReportPortal ;
29
+ import io .restassured .config .LogConfig ;
30
+ import io .restassured .config .RestAssuredConfig ;
29
31
import io .restassured .filter .FilterContext ;
30
32
import io .restassured .filter .OrderedFilter ;
31
33
import io .restassured .response .Response ;
39
41
import java .util .function .Function ;
40
42
import java .util .function .Predicate ;
41
43
44
+ import static com .epam .reportportal .formatting .http .Constants .REMOVED_TAG ;
45
+ import static java .util .Optional .ofNullable ;
46
+
42
47
/**
43
48
* REST Assured Request/Response logging filter for Report Portal.
44
49
* <p>
@@ -78,16 +83,9 @@ public class ReportPortalRestAssuredLoggingFilter extends AbstractHttpFormatter<
78
83
* URI "as is"
79
84
*/
80
85
public ReportPortalRestAssuredLoggingFilter (int filterOrder , @ Nonnull LogLevel defaultLogLevel ,
81
- @ Nullable Function <Header , String > headerConvertFunction ,
82
- @ Nullable Function <Header , String > partHeaderConvertFunction ,
83
- @ Nullable Function <Cookie , String > cookieConvertFunction ,
84
- @ Nullable Function <String , String > uriConverterFunction ) {
85
- super (defaultLogLevel ,
86
- headerConvertFunction ,
87
- partHeaderConvertFunction ,
88
- cookieConvertFunction ,
89
- uriConverterFunction
90
- );
86
+ @ Nullable Function <Header , String > headerConvertFunction , @ Nullable Function <Header , String > partHeaderConvertFunction ,
87
+ @ Nullable Function <Cookie , String > cookieConvertFunction , @ Nullable Function <String , String > uriConverterFunction ) {
88
+ super (defaultLogLevel , headerConvertFunction , partHeaderConvertFunction , cookieConvertFunction , uriConverterFunction );
91
89
order = filterOrder ;
92
90
}
93
91
@@ -106,10 +104,10 @@ public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel d
106
104
* formats Cookies with <code>toString</code> method
107
105
*/
108
106
public ReportPortalRestAssuredLoggingFilter (int filterOrder , @ Nonnull LogLevel defaultLogLevel ,
109
- @ Nullable Function <Header , String > headerConvertFunction ,
110
- @ Nullable Function <Header , String > partHeaderConvertFunction ,
107
+ @ Nullable Function <Header , String > headerConvertFunction , @ Nullable Function <Header , String > partHeaderConvertFunction ,
111
108
@ Nullable Function <Cookie , String > cookieConvertFunction ) {
112
- this (filterOrder ,
109
+ this (
110
+ filterOrder ,
113
111
defaultLogLevel ,
114
112
headerConvertFunction ,
115
113
partHeaderConvertFunction ,
@@ -131,14 +129,8 @@ public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel d
131
129
* @param partHeaderConvertFunction the same as fot HTTP Headers, but for parts in Multipart request
132
130
*/
133
131
public ReportPortalRestAssuredLoggingFilter (int filterOrder , @ Nonnull LogLevel defaultLogLevel ,
134
- @ Nullable Function <Header , String > headerConvertFunction ,
135
- @ Nullable Function <Header , String > partHeaderConvertFunction ) {
136
- this (filterOrder ,
137
- defaultLogLevel ,
138
- headerConvertFunction ,
139
- partHeaderConvertFunction ,
140
- DefaultCookieConverter .INSTANCE
141
- );
132
+ @ Nullable Function <Header , String > headerConvertFunction , @ Nullable Function <Header , String > partHeaderConvertFunction ) {
133
+ this (filterOrder , defaultLogLevel , headerConvertFunction , partHeaderConvertFunction , DefaultCookieConverter .INSTANCE );
142
134
}
143
135
144
136
/**
@@ -159,14 +151,31 @@ public int getOrder() {
159
151
}
160
152
161
153
@ Override
162
- public Response filter (FilterableRequestSpecification requestSpec , FilterableResponseSpecification responseSpec ,
163
- FilterContext ctx ) {
164
- if (requestFilters .stream ().anyMatch (f -> f .test (requestSpec ))) {
154
+ public Response filter (FilterableRequestSpecification requestSpec , FilterableResponseSpecification responseSpec , FilterContext ctx ) {
155
+ if (requestSpec == null || requestFilters .stream ().anyMatch (f -> f .test (requestSpec ))) {
165
156
return ctx .next (requestSpec , responseSpec );
166
157
}
167
- emitLog (HttpEntityFactory .createHttpRequestFormatter (requestSpec ,
158
+
159
+ Set <String > blacklistedHeaders = ofNullable (requestSpec .getConfig ()).map (RestAssuredConfig ::getLogConfig )
160
+ .map (LogConfig ::blacklistedHeaders )
161
+ .filter (headers -> !headers .isEmpty ())
162
+ .orElse (null );
163
+ Function <Header , String > myHeaderConverter = headerConverter ;
164
+ if (blacklistedHeaders != null ) {
165
+ myHeaderConverter = header -> {
166
+ if (!blacklistedHeaders .contains (header .getName ())) {
167
+ return headerConverter .apply (header );
168
+ }
169
+ Header newHeader = header .clone ();
170
+ newHeader .setValue (REMOVED_TAG );
171
+ return headerConverter .apply (newHeader );
172
+ };
173
+ }
174
+
175
+ emitLog (HttpEntityFactory .createHttpRequestFormatter (
176
+ requestSpec ,
168
177
uriConverter ,
169
- headerConverter ,
178
+ myHeaderConverter ,
170
179
cookieConverter ,
171
180
contentPrettiers ,
172
181
partHeaderConverter ,
@@ -176,7 +185,8 @@ public Response filter(FilterableRequestSpecification requestSpec, FilterableRes
176
185
if (response == null ) {
177
186
ReportPortal .emitLog (NULL_RESPONSE , logLevel , Calendar .getInstance ().getTime ());
178
187
} else {
179
- emitLog (HttpEntityFactory .createHttpResponseFormatter (response ,
188
+ emitLog (HttpEntityFactory .createHttpResponseFormatter (
189
+ response ,
180
190
headerConverter ,
181
191
cookieConverter ,
182
192
contentPrettiers ,
@@ -191,14 +201,12 @@ public ReportPortalRestAssuredLoggingFilter setBodyTypeMap(@Nonnull Map<String,
191
201
return this ;
192
202
}
193
203
194
- public ReportPortalRestAssuredLoggingFilter setContentPrettiers (
195
- @ Nonnull Map <String , Function <String , String >> contentPrettiers ) {
204
+ public ReportPortalRestAssuredLoggingFilter setContentPrettiers (@ Nonnull Map <String , Function <String , String >> contentPrettiers ) {
196
205
this .contentPrettiers = Collections .unmodifiableMap (new HashMap <>(contentPrettiers ));
197
206
return this ;
198
207
}
199
208
200
- public ReportPortalRestAssuredLoggingFilter addRequestFilter (
201
- @ Nonnull Predicate <FilterableRequestSpecification > requestFilter ) {
209
+ public ReportPortalRestAssuredLoggingFilter addRequestFilter (@ Nonnull Predicate <FilterableRequestSpecification > requestFilter ) {
202
210
requestFilters .add (requestFilter );
203
211
return this ;
204
212
}
0 commit comments