2424import io .qameta .allure .model .TestResult ;
2525import io .qameta .allure .test .AllureResults ;
2626import io .restassured .RestAssured ;
27+ import io .restassured .config .LogConfig ;
28+ import io .restassured .config .RestAssuredConfig ;
2729import io .restassured .http .ContentType ;
30+ import io .restassured .specification .RequestSpecification ;
31+ import java .nio .charset .StandardCharsets ;
2832import org .junit .jupiter .api .Test ;
2933import org .junit .jupiter .api .extension .ExtensionContext ;
3034import org .junit .jupiter .params .ParameterizedTest ;
3135import org .junit .jupiter .params .provider .Arguments ;
3236import org .junit .jupiter .params .provider .ArgumentsProvider ;
3337import org .junit .jupiter .params .provider .ArgumentsSource ;
3438
35- import java .util .ArrayList ;
3639import java .util .Collection ;
3740import java .util .List ;
41+ import java .util .Map ;
3842import java .util .stream .Collectors ;
3943import java .util .stream .Stream ;
4044
@@ -55,10 +59,27 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
5559 }
5660}
5761
62+ class HiddenHeadersArgumentProvider implements ArgumentsProvider {
63+ @ Override
64+ public Stream <? extends Arguments > provideArguments (ExtensionContext context ) {
65+
66+ final String hiddenHeader = "Authorization" ;
67+ final String header = "Accept" ;
68+ final String headerValue = "value" ;
69+
70+ final Map <String , String > headers = Map .of (hiddenHeader , headerValue , header , headerValue );
71+ final List <String > expectedHeaders = List .of (hiddenHeader + ": [ BLACKLISTED ]" , header + ": " + headerValue );
72+
73+ return Stream .of (
74+ arguments (headers , hiddenHeader , expectedHeaders , new AllureRestAssured ()),
75+ arguments (headers , hiddenHeader .toUpperCase (), expectedHeaders , new AllureRestAssured ())
76+ );
77+ }
78+ }
79+
5880/**
5981 * @author charlie (Dmitry Baev).
6082 */
61- @ SuppressWarnings ("unchecked" )
6283class AllureRestAssuredTest {
6384
6485 @ ParameterizedTest
@@ -72,7 +93,7 @@ void shouldCreateAttachment(final List<String> attachmentNames, final AllureRest
7293 .map (TestResult ::getAttachments )
7394 .flatMap (Collection ::stream )
7495 .map (Attachment ::getName ))
75- .isEqualTo (attachmentNames );
96+ .containsExactlyElementsOf (attachmentNames );
7697 }
7798
7899 @ Test
@@ -122,21 +143,46 @@ void shouldCatchAttachmentBody(final List<String> attachmentNames, final AllureR
122143 .collect (Collectors .toList ());
123144
124145 assertThat (actualAttachments )
125- .flatExtracting (Attachment ::getName )
126- .isEqualTo (attachmentNames )
146+ .map (Attachment ::getName )
147+ .containsExactlyElementsOf (attachmentNames )
127148 .doesNotContainNull ();
128149
129150 assertThat (actualAttachments )
130- .flatExtracting (Attachment ::getSource )
131- .containsExactlyInAnyOrderElementsOf (new ArrayList <>( results .getAttachments ().keySet () ))
151+ .map (Attachment ::getSource )
152+ .containsExactlyInAnyOrderElementsOf (results .getAttachments ().keySet ())
132153 .doesNotContainNull ();
133154 }
134155
156+ @ ParameterizedTest
157+ @ ArgumentsSource (HiddenHeadersArgumentProvider .class )
158+ void shouldHideHeadersInAttachments (final Map <String , String > headers ,
159+ final String hiddenHeader ,
160+ final List <String > expectedValues ,
161+ final AllureRestAssured filter ) {
162+
163+ final ResponseDefinitionBuilder responseBuilder = WireMock .aResponse ().withStatus (200 );
164+ headers .forEach (responseBuilder ::withHeader );
165+
166+ RestAssured .config = new RestAssuredConfig ().logConfig (LogConfig .logConfig ().blacklistHeaders (List .of (hiddenHeader )));
167+ RestAssured .replaceFiltersWith (filter );
168+
169+ final AllureResults results = executeWithStub (responseBuilder , RestAssured .with ().headers (headers ));
170+
171+ assertThat (results .getAttachments ().values ())
172+ .hasSize (2 )
173+ .map (at -> new String (at , StandardCharsets .UTF_8 ))
174+ .allSatisfy (at -> expectedValues .forEach (ev -> assertThat (at ).contains (ev )));
175+ }
176+
135177 protected final AllureResults execute () {
136178 return executeWithStub (WireMock .aResponse ().withBody ("some body" ));
137179 }
138180
139181 protected final AllureResults executeWithStub (ResponseDefinitionBuilder responseBuilder ) {
182+ return executeWithStub (responseBuilder , RestAssured .given ());
183+ }
184+
185+ protected final AllureResults executeWithStub (ResponseDefinitionBuilder responseBuilder , RequestSpecification spec ) {
140186 final WireMockServer server = new WireMockServer (WireMockConfiguration .options ().dynamicPort ());
141187 final int statusCode = responseBuilder .build ().getStatus ();
142188
@@ -146,13 +192,13 @@ protected final AllureResults executeWithStub(ResponseDefinitionBuilder response
146192
147193 WireMock .stubFor (WireMock .get (WireMock .urlEqualTo ("/hello?Allure=Form" )).willReturn (responseBuilder ));
148194 try {
149- RestAssured .given ()
150- .contentType (ContentType .URLENC )
195+ spec .contentType (ContentType .URLENC )
151196 .formParams ("Allure" , "Form" )
152197 .get (server .url ("/hello" )).then ().statusCode (statusCode );
153198 } finally {
154199 server .stop ();
155200 RestAssured .replaceFiltersWith (ImmutableList .of ());
201+ RestAssured .config = new RestAssuredConfig ();
156202 }
157203 });
158204 }
0 commit comments