Skip to content

Commit 38ed5e5

Browse files
committed
Example update
1 parent bd4d53d commit 38ed5e5

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

example-testng-logback/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
<artifactId>logger-java-selenide</artifactId>
117117
<version>5.2.3</version>
118118
</dependency>
119+
<dependency>
120+
<groupId>org.wiremock</groupId>
121+
<artifactId>wiremock</artifactId>
122+
<version>3.10.0</version>
123+
</dependency>
119124
</dependencies>
120125

121126
<build>

example-testng-logback/src/main/java/com/epam/reportportal/example/testng/logback/logging/restassured/RestAssuredAdvanceSanitizeTest.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,48 @@
2020
import com.epam.reportportal.formatting.http.converters.SanitizingCookieConverter;
2121
import com.epam.reportportal.formatting.http.converters.SanitizingHttpHeaderConverter;
2222
import com.epam.reportportal.formatting.http.converters.SanitizingUriConverter;
23+
import com.epam.reportportal.formatting.http.entities.Header;
2324
import com.epam.reportportal.listeners.LogLevel;
2425
import com.epam.reportportal.restassured.ReportPortalRestAssuredLoggingFilter;
26+
import com.github.tomakehurst.wiremock.WireMockServer;
2527
import io.restassured.RestAssured;
26-
import io.restassured.http.Cookie;
28+
import org.testng.annotations.AfterMethod;
2729
import org.testng.annotations.BeforeClass;
30+
import org.testng.annotations.BeforeMethod;
2831
import 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
*/
3347
public 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

Comments
 (0)