Skip to content

Commit

Permalink
Merge pull request #31 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Nov 28, 2024
2 parents be91484 + bb6211f commit b67988d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
java-version: '8'

- name: Setup git credentials
uses: oleksiyrudenko/gha-git-credentials@v2.1.1
uses: oleksiyrudenko/gha-git-credentials@v2-latest
with:
name: 'reportportal.io'
email: '[email protected]'
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## [Unreleased]
### Added
- RestAssured blacklisted headers support, by @HardNorth
### Changed
- Client version updated on [5.2.23](https://github.com/reportportal/client-java/releases/tag/5.2.23), by @HardNorth
- `utils-java-formatting` dependency version updated on [5.2.4](https://github.com/reportportal/utils-java-formatting/releases/tag/5.2.4), by @HardNorth

## [5.3.4]
### Changed
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repositories {
}

dependencies {
api 'com.epam.reportportal:utils-java-formatting:5.2.3'
api 'com.epam.reportportal:utils-java-formatting:5.2.4'
compileOnly "com.epam.reportportal:client-java:${client_version}"
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'commons-codec:commons-codec:1.15'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description=Report Portal logger for REST Assured
junit5_version=5.6.3
junit5_runner_version=1.6.3
mockito_version=4.5.1
client_version=5.2.13
client_version=5.2.23
scripts_url=https://raw.githubusercontent.com/reportportal/gradle-scripts
scripts_branch=master
excludeTests=
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.epam.reportportal.listeners.LogLevel;
import com.epam.reportportal.restassured.support.HttpEntityFactory;
import com.epam.reportportal.service.ReportPortal;
import io.restassured.config.LogConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.response.Response;
Expand All @@ -39,6 +41,9 @@
import java.util.function.Function;
import java.util.function.Predicate;

import static com.epam.reportportal.formatting.http.Constants.REMOVED_TAG;
import static java.util.Optional.ofNullable;

/**
* REST Assured Request/Response logging filter for Report Portal.
* <p>
Expand Down Expand Up @@ -78,16 +83,9 @@ public class ReportPortalRestAssuredLoggingFilter extends AbstractHttpFormatter<
* URI "as is"
*/
public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel defaultLogLevel,
@Nullable Function<Header, String> headerConvertFunction,
@Nullable Function<Header, String> partHeaderConvertFunction,
@Nullable Function<Cookie, String> cookieConvertFunction,
@Nullable Function<String, String> uriConverterFunction) {
super(defaultLogLevel,
headerConvertFunction,
partHeaderConvertFunction,
cookieConvertFunction,
uriConverterFunction
);
@Nullable Function<Header, String> headerConvertFunction, @Nullable Function<Header, String> partHeaderConvertFunction,
@Nullable Function<Cookie, String> cookieConvertFunction, @Nullable Function<String, String> uriConverterFunction) {
super(defaultLogLevel, headerConvertFunction, partHeaderConvertFunction, cookieConvertFunction, uriConverterFunction);
order = filterOrder;
}

Expand All @@ -106,10 +104,10 @@ public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel d
* formats Cookies with <code>toString</code> method
*/
public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel defaultLogLevel,
@Nullable Function<Header, String> headerConvertFunction,
@Nullable Function<Header, String> partHeaderConvertFunction,
@Nullable Function<Header, String> headerConvertFunction, @Nullable Function<Header, String> partHeaderConvertFunction,
@Nullable Function<Cookie, String> cookieConvertFunction) {
this(filterOrder,
this(
filterOrder,
defaultLogLevel,
headerConvertFunction,
partHeaderConvertFunction,
Expand All @@ -131,14 +129,8 @@ public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel d
* @param partHeaderConvertFunction the same as fot HTTP Headers, but for parts in Multipart request
*/
public ReportPortalRestAssuredLoggingFilter(int filterOrder, @Nonnull LogLevel defaultLogLevel,
@Nullable Function<Header, String> headerConvertFunction,
@Nullable Function<Header, String> partHeaderConvertFunction) {
this(filterOrder,
defaultLogLevel,
headerConvertFunction,
partHeaderConvertFunction,
DefaultCookieConverter.INSTANCE
);
@Nullable Function<Header, String> headerConvertFunction, @Nullable Function<Header, String> partHeaderConvertFunction) {
this(filterOrder, defaultLogLevel, headerConvertFunction, partHeaderConvertFunction, DefaultCookieConverter.INSTANCE);
}

/**
Expand All @@ -159,14 +151,31 @@ public int getOrder() {
}

@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,
FilterContext ctx) {
if (requestFilters.stream().anyMatch(f -> f.test(requestSpec))) {
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
if (requestSpec == null || requestFilters.stream().anyMatch(f -> f.test(requestSpec))) {
return ctx.next(requestSpec, responseSpec);
}
emitLog(HttpEntityFactory.createHttpRequestFormatter(requestSpec,

Set<String> blacklistedHeaders = ofNullable(requestSpec.getConfig()).map(RestAssuredConfig::getLogConfig)
.map(LogConfig::blacklistedHeaders)
.filter(headers -> !headers.isEmpty())
.orElse(null);
Function<Header, String> myHeaderConverter = headerConverter;
if (blacklistedHeaders != null) {
myHeaderConverter = header -> {
if (!blacklistedHeaders.contains(header.getName())) {
return headerConverter.apply(header);
}
Header newHeader = header.clone();
newHeader.setValue(REMOVED_TAG);
return headerConverter.apply(newHeader);
};
}

emitLog(HttpEntityFactory.createHttpRequestFormatter(
requestSpec,
uriConverter,
headerConverter,
myHeaderConverter,
cookieConverter,
contentPrettiers,
partHeaderConverter,
Expand All @@ -176,7 +185,8 @@ public Response filter(FilterableRequestSpecification requestSpec, FilterableRes
if (response == null) {
ReportPortal.emitLog(NULL_RESPONSE, logLevel, Calendar.getInstance().getTime());
} else {
emitLog(HttpEntityFactory.createHttpResponseFormatter(response,
emitLog(HttpEntityFactory.createHttpResponseFormatter(
response,
headerConverter,
cookieConverter,
contentPrettiers,
Expand All @@ -191,14 +201,12 @@ public ReportPortalRestAssuredLoggingFilter setBodyTypeMap(@Nonnull Map<String,
return this;
}

public ReportPortalRestAssuredLoggingFilter setContentPrettiers(
@Nonnull Map<String, Function<String, String>> contentPrettiers) {
public ReportPortalRestAssuredLoggingFilter setContentPrettiers(@Nonnull Map<String, Function<String, String>> contentPrettiers) {
this.contentPrettiers = Collections.unmodifiableMap(new HashMap<>(contentPrettiers));
return this;
}

public ReportPortalRestAssuredLoggingFilter addRequestFilter(
@Nonnull Predicate<FilterableRequestSpecification> requestFilter) {
public ReportPortalRestAssuredLoggingFilter addRequestFilter(@Nonnull Predicate<FilterableRequestSpecification> requestFilter) {
requestFilters.add(requestFilter);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,51 +68,49 @@ private static List<HttpPartFormatter> toParts(@Nonnull FilterableRequestSpecifi
TypeAwareByteSource file = Utils.getFile((File) body);
byte[] data = file.read();
if (partType == HttpPartFormatter.PartType.TEXT) {
content = ofNullable(data).map(d -> {
try {
return new String(d, ofNullable(it.getCharset()).orElse(StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}).orElse("");
try {
content = new String(data, ofNullable(it.getCharset()).orElse(StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
} else {
content = data;
}
} else {
content = body;
}
HttpPartFormatter.Builder partBuilder = new HttpPartFormatter.Builder(partType, partMimeType, content);
ofNullable(it.getHeaders()).ifPresent(headers -> headers.forEach((key, value) -> partBuilder.addHeader(
new Header(key, value))));
ofNullable(it.getHeaders()).ifPresent(headers -> headers.forEach((key, value) -> partBuilder.addHeader(new Header(
key,
value
))));
partBuilder.controlName(it.getControlName());
partBuilder.charset(it.getCharset());
partBuilder.fileName(it.getFileName());
partBuilder.headerConverter(partHeaderConverter);
return partBuilder.build();
} catch (IOException e) {
ReportPortal.emitLog("Unable to read file: " + e.getMessage(),
"ERROR",
Calendar.getInstance().getTime()
);
ReportPortal.emitLog("Unable to read file: " + e.getMessage(), "ERROR", Calendar.getInstance().getTime());
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList())).orElse(Collections.emptyList());
}

@Nonnull
public static HttpRequestFormatter createHttpRequestFormatter(
@Nonnull FilterableRequestSpecification requestSpecification,
public static HttpRequestFormatter createHttpRequestFormatter(@Nonnull FilterableRequestSpecification requestSpecification,
@Nullable Function<String, String> uriConverter, @Nullable Function<Header, String> headerConverter,
@Nullable Function<Cookie, String> cookieConverter,
@Nullable Map<String, Function<String, String>> prettiers,
@Nullable Function<Cookie, String> cookieConverter, @Nullable Map<String, Function<String, String>> prettiers,
@Nullable Function<Header, String> partHeaderConverter, @Nonnull Map<String, BodyType> bodyTypeMap) {
HttpRequestFormatter.Builder builder = new HttpRequestFormatter.Builder(requestSpecification.getMethod(),
HttpRequestFormatter.Builder builder = new HttpRequestFormatter.Builder(
requestSpecification.getMethod(),
requestSpecification.getURI()
);
ofNullable(requestSpecification.getHeaders()).ifPresent(headers -> headers.forEach(h -> builder.addHeader(h.getName(),
ofNullable(requestSpecification.getHeaders()).ifPresent(headers -> headers.forEach(h -> builder.addHeader(
h.getName(),
h.getValue()
)));
ofNullable(requestSpecification.getCookies()).ifPresent(cookies -> cookies.forEach(c -> builder.addCookie(c.getName(),
ofNullable(requestSpecification.getCookies()).ifPresent(cookies -> cookies.forEach(c -> builder.addCookie(
c.getName(),
c.getValue(),
c.getComment(),
c.getPath(),
Expand All @@ -124,10 +122,7 @@ public static HttpRequestFormatter createHttpRequestFormatter(
c.getVersion(),
c.getSameSite()
)));
builder.uriConverter(uriConverter)
.headerConverter(headerConverter)
.cookieConverter(cookieConverter)
.prettiers(prettiers);
builder.uriConverter(uriConverter).headerConverter(headerConverter).cookieConverter(cookieConverter).prettiers(prettiers);
String mimeType = getMimeType(requestSpecification.getContentType());
BodyType bodyType = getBodyType(requestSpecification.getContentType(), bodyTypeMap);
switch (bodyType) {
Expand All @@ -150,13 +145,10 @@ public static HttpRequestFormatter createHttpRequestFormatter(
public static HttpResponseFormatter createHttpResponseFormatter(@Nonnull Response response,
@Nullable Function<Header, String> headerConverter, @Nullable Function<Cookie, String> cookieConverter,
@Nullable Map<String, Function<String, String>> prettiers, @Nonnull Map<String, BodyType> bodyTypeMap) {
HttpResponseFormatter.Builder builder = new HttpResponseFormatter.Builder(response.statusCode(),
response.getStatusLine()
);
ofNullable(response.getHeaders()).ifPresent(headers -> headers.forEach(h -> builder.addHeader(h.getName(),
h.getValue()
)));
ofNullable(response.getDetailedCookies()).ifPresent(cookies -> cookies.forEach(c -> builder.addCookie(c.getName(),
HttpResponseFormatter.Builder builder = new HttpResponseFormatter.Builder(response.statusCode(), response.getStatusLine());
ofNullable(response.getHeaders()).ifPresent(headers -> headers.forEach(h -> builder.addHeader(h.getName(), h.getValue())));
ofNullable(response.getDetailedCookies()).ifPresent(cookies -> cookies.forEach(c -> builder.addCookie(
c.getName(),
c.getValue(),
c.getComment(),
c.getPath(),
Expand Down
Loading

0 comments on commit b67988d

Please sign in to comment.