2020import com .epam .reportportal .formatting .http .converters .SanitizingCookieConverter ;
2121import com .epam .reportportal .formatting .http .converters .SanitizingHttpHeaderConverter ;
2222import com .epam .reportportal .formatting .http .converters .SanitizingUriConverter ;
23+ import com .epam .reportportal .formatting .http .entities .Header ;
2324import com .epam .reportportal .listeners .LogLevel ;
2425import com .epam .reportportal .restassured .ReportPortalRestAssuredLoggingFilter ;
26+ import com .github .tomakehurst .wiremock .WireMockServer ;
2527import io .restassured .RestAssured ;
26- import io . restassured . http . Cookie ;
28+ import org . testng . annotations . AfterMethod ;
2729import org .testng .annotations .BeforeClass ;
30+ import org .testng .annotations .BeforeMethod ;
2831import org .testng .annotations .Test ;
2932
33+ import javax .annotation .Nullable ;
34+ import java .nio .charset .StandardCharsets ;
35+ import java .util .Base64 ;
36+ import java .util .function .Function ;
37+
38+ import static com .epam .reportportal .formatting .http .Constants .REMOVED_TAG ;
39+ import static com .github .tomakehurst .wiremock .client .WireMock .ok ;
40+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
41+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
42+ import static java .util .Optional .ofNullable ;
43+
3044/**
3145 * An example of a header, cookies and URI credentials hiding in case they contain sensitive data.
3246 */
3347public class RestAssuredAdvanceSanitizeTest {
3448
49+ private static final Function <Header , String > SANITIZING_HTTP_HEADER_CONVERTER = new Function <Header , String >() {
50+ @ Override
51+ public @ Nullable String apply (@ Nullable Header header ) {
52+ return SanitizingHttpHeaderConverter .INSTANCE .apply (ofNullable (header ).filter (h -> "Set-Cookie" .equalsIgnoreCase (h .getName ()))
53+ .map (h -> {
54+ Header newHeader = h .clone ();
55+ newHeader .setValue (REMOVED_TAG );
56+ return newHeader ;
57+ })
58+ .orElse (header ));
59+ }
60+ };
61+
62+ private WireMockServer wireMockServer ;
63+ private int mockPort ;
64+
3565 /**
3666 * Set {@link ReportPortalRestAssuredLoggingFilter} as one of the REST Assured filters.
3767 */
@@ -41,24 +71,37 @@ public void setupRestAssured() {
4171 RestAssured .filters (new ReportPortalRestAssuredLoggingFilter (
4272 42 ,
4373 LogLevel .INFO ,
44- SanitizingHttpHeaderConverter . INSTANCE ,
74+ SANITIZING_HTTP_HEADER_CONVERTER ,
4575 DefaultHttpHeaderConverter .INSTANCE ,
4676 SanitizingCookieConverter .INSTANCE ,
4777 SanitizingUriConverter .INSTANCE
4878 ));
4979 }
5080
81+ @ BeforeMethod
82+ public void startWireMock () {
83+ wireMockServer = new WireMockServer (options ().dynamicPort ());
84+ wireMockServer .stubFor (post ("/auth/login" ).willReturn (ok ().withHeader ("Set-Cookie" , "session_id=test_session_id; Path=/; HttpOnly" )
85+ .withHeader ("Set-Cookie" , "scope=view-all, edit-self; Path=/; HttpOnly" )));
86+ wireMockServer .start ();
87+ mockPort = wireMockServer .port ();
88+ }
89+
5190 /**
5291 * Make a simple request to a test API and validate the response. Request / Response logs should appear on Report Portal.
5392 */
5493 @ Test
5594 public void restAssuredLoggingTest () {
5695 RestAssured .given ()
57- .header ("Authorization" , "Bearer test_token" )
58- .cookie (new Cookie .Builder ("session_id" , "test_session_id" ).build ())
59- .get ("https://user:password@jsonplaceholder.typicode.com/todos/1" )
96+ .header ("Authorization" , "Basic " + Base64 .getEncoder ().encodeToString ("ui:password" .getBytes (StandardCharsets .UTF_8 )))
97+ .post ("http://user:password@localhost:" + mockPort + "/auth/login" )
6098 .then ()
6199 .assertThat ()
62100 .statusCode (200 );
63101 }
102+
103+ @ AfterMethod
104+ public void stopWireMock () {
105+ wireMockServer .stop ();
106+ }
64107}
0 commit comments