diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a3f87..2724b94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Changed +- More `Prettiers` to `Prettifiers` renaming and deprecation of `Prettiers`, by @HardNorth ## [5.2.6] ### Added diff --git a/src/main/java/com/epam/reportportal/formatting/AbstractHttpFormatter.java b/src/main/java/com/epam/reportportal/formatting/AbstractHttpFormatter.java index dc47a93..c7286c4 100644 --- a/src/main/java/com/epam/reportportal/formatting/AbstractHttpFormatter.java +++ b/src/main/java/com/epam/reportportal/formatting/AbstractHttpFormatter.java @@ -37,7 +37,7 @@ import java.util.function.Function; import static com.epam.reportportal.formatting.http.Constants.BODY_TYPE_MAP; -import static com.epam.reportportal.formatting.http.Constants.DEFAULT_PRETTIERS; +import static com.epam.reportportal.formatting.http.Constants.DEFAULT_PRETTIFIERS; import static java.util.Optional.ofNullable; /** @@ -54,7 +54,7 @@ public abstract class AbstractHttpFormatter cookieConverter; protected final Function uriConverter; - private Map> contentPrettifiers = DEFAULT_PRETTIERS; + private Map> contentPrettifiers = DEFAULT_PRETTIFIERS; /** * @deprecated Use {@link #getContentPrettifiers()} instead diff --git a/src/main/java/com/epam/reportportal/formatting/http/Constants.java b/src/main/java/com/epam/reportportal/formatting/http/Constants.java index fe4fba8..0daa696 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/Constants.java +++ b/src/main/java/com/epam/reportportal/formatting/http/Constants.java @@ -17,9 +17,9 @@ package com.epam.reportportal.formatting.http; import com.epam.reportportal.formatting.http.entities.BodyType; -import com.epam.reportportal.formatting.http.prettiers.HtmlPrettier; -import com.epam.reportportal.formatting.http.prettiers.JsonPrettier; -import com.epam.reportportal.formatting.http.prettiers.XmlPrettier; +import com.epam.reportportal.formatting.http.prettifiers.HtmlPrettifier; +import com.epam.reportportal.formatting.http.prettifiers.JsonPrettifier; +import com.epam.reportportal.formatting.http.prettifiers.XmlPrettifier; import com.epam.reportportal.utils.http.ContentType; import java.util.*; @@ -47,43 +47,48 @@ public class Constants { ContentType.MULTIPART_PARALLEL ))); - public static final Set TEXT_TYPES = - Collections.unmodifiableSet(new HashSet<>(Arrays.asList(ContentType.APPLICATION_JSON, - ContentType.TEXT_PLAIN, - ContentType.TEXT_HTML, - ContentType.TEXT_XML, - ContentType.APPLICATION_XML, - ContentType.APPLICATION_SOAP_XML, - ContentType.APPLICATION_ATOM_XML, - // Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly - "text/json", - "application/x.reportportal.launch.v2+json", - "application/x.reportportal.test.v2+json" - ))); + public static final Set TEXT_TYPES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + ContentType.APPLICATION_JSON, + ContentType.TEXT_PLAIN, + ContentType.TEXT_HTML, + ContentType.TEXT_XML, + ContentType.APPLICATION_XML, + ContentType.APPLICATION_SOAP_XML, + ContentType.APPLICATION_ATOM_XML, + // Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly + "text/json", + "application/x.reportportal.launch.v2+json", + "application/x.reportportal.test.v2+json" + ))); public static final Set FORM_TYPES = Collections.singleton(ContentType.APPLICATION_FORM_URLENCODED); - public static final Map BODY_TYPE_MAP = Collections.unmodifiableMap(Stream.of(TEXT_TYPES.stream() - .collect(Collectors.toMap(k -> k, v -> BodyType.TEXT)), + public static final Map BODY_TYPE_MAP = Collections.unmodifiableMap(Stream.of( + TEXT_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.TEXT)), FORM_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.FORM)), MULTIPART_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.MULTIPART)) ).flatMap(m -> m.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); - public static final Map> DEFAULT_PRETTIERS = - Collections.unmodifiableMap(new HashMap>() {{ - put(ContentType.APPLICATION_XML, XmlPrettier.INSTANCE); - put(ContentType.APPLICATION_SOAP_XML, XmlPrettier.INSTANCE); - put(ContentType.APPLICATION_ATOM_XML, XmlPrettier.INSTANCE); - put(ContentType.APPLICATION_SVG_XML, XmlPrettier.INSTANCE); - put(ContentType.APPLICATION_XHTML_XML, XmlPrettier.INSTANCE); - put(ContentType.TEXT_XML, XmlPrettier.INSTANCE); - put(ContentType.APPLICATION_JSON, JsonPrettier.INSTANCE); - // Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly - put("text/json", JsonPrettier.INSTANCE); - put("application/x.reportportal.launch.v2+json", JsonPrettier.INSTANCE); - put("application/x.reportportal.test.v2+json", JsonPrettier.INSTANCE); - put(ContentType.TEXT_HTML, HtmlPrettier.INSTANCE); - }}); + public static final Map> DEFAULT_PRETTIFIERS = Collections.unmodifiableMap(new HashMap>() {{ + put(ContentType.APPLICATION_XML, XmlPrettifier.INSTANCE); + put(ContentType.APPLICATION_SOAP_XML, XmlPrettifier.INSTANCE); + put(ContentType.APPLICATION_ATOM_XML, XmlPrettifier.INSTANCE); + put(ContentType.APPLICATION_SVG_XML, XmlPrettifier.INSTANCE); + put(ContentType.APPLICATION_XHTML_XML, XmlPrettifier.INSTANCE); + put(ContentType.TEXT_XML, XmlPrettifier.INSTANCE); + put(ContentType.APPLICATION_JSON, JsonPrettifier.INSTANCE); + // Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly + put("text/json", JsonPrettifier.INSTANCE); + put("application/x.reportportal.launch.v2+json", JsonPrettifier.INSTANCE); + put("application/x.reportportal.test.v2+json", JsonPrettifier.INSTANCE); + put(ContentType.TEXT_HTML, HtmlPrettifier.INSTANCE); + }}); + + /** + * @deprecated Use {@link #DEFAULT_PRETTIFIERS} instead + */ + @Deprecated + public static final Map> DEFAULT_PRETTIERS = DEFAULT_PRETTIFIERS; private Constants() { throw new RuntimeException("No instances should exist for the class!"); diff --git a/src/main/java/com/epam/reportportal/formatting/http/HttpFormatUtils.java b/src/main/java/com/epam/reportportal/formatting/http/HttpFormatUtils.java index 062ee55..fafd396 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/HttpFormatUtils.java +++ b/src/main/java/com/epam/reportportal/formatting/http/HttpFormatUtils.java @@ -61,14 +61,11 @@ public static String joinParts(@Nonnull String delimiter, @Nullable String... pa if (parts == null) { return ""; } - return Arrays.stream(parts) - .filter(p -> Objects.nonNull(p) && !p.isEmpty()) - .collect(Collectors.joining(delimiter)); + return Arrays.stream(parts).filter(p -> Objects.nonNull(p) && !p.isEmpty()).collect(Collectors.joining(delimiter)); } @Nonnull - public static String format(@Nullable List entities, @Nonnull Function converter, - @Nullable String tag) { + public static String format(@Nullable List entities, @Nonnull Function converter, @Nullable String tag) { String prefix = tag == null ? "" : tag + LINE_DELIMITER; if (entities == null || entities.isEmpty()) { return ""; @@ -80,34 +77,23 @@ public static String format(@Nullable List entities, @Nonnull Function headers, - @Nullable Function headerConverter) { - return format(headers, - headerConverter == null ? DefaultHttpHeaderConverter.INSTANCE : headerConverter, - HEADERS_TAG - ); + public static String formatHeaders(@Nullable List
headers, @Nullable Function headerConverter) { + return format(headers, headerConverter == null ? DefaultHttpHeaderConverter.INSTANCE : headerConverter, HEADERS_TAG); } @Nonnull - public static String formatCookies(@Nullable List cookies, - @Nullable Function cookieConverter) { - return format(cookies, - cookieConverter == null ? DefaultCookieConverter.INSTANCE : cookieConverter, - COOKIES_TAG - ); + public static String formatCookies(@Nullable List cookies, @Nullable Function cookieConverter) { + return format(cookies, cookieConverter == null ? DefaultCookieConverter.INSTANCE : cookieConverter, COOKIES_TAG); } @Nonnull public static String formatText(@Nullable String header, @Nullable List params, @Nullable String tag, - @Nullable Function paramConverter) { + @Nullable Function paramConverter) { if (params == null || params.isEmpty()) { return header == null ? "" : header; } String prefix = tag == null ? "" : tag + LINE_DELIMITER; - String body = format(params, - paramConverter == null ? DefaultFormParamConverter.INSTANCE : paramConverter, - null - ); + String body = format(params, paramConverter == null ? DefaultFormParamConverter.INSTANCE : paramConverter, null); return (header == null || header.isEmpty() ? "" : header + LINE_DELIMITER + LINE_DELIMITER) + (body.isEmpty() ? body : prefix + BODY_HIGHLIGHT + LINE_DELIMITER + body + LINE_DELIMITER + BODY_HIGHLIGHT); @@ -115,18 +101,18 @@ public static String formatText(@Nullable String header, @Nullable List p @Nonnull public static String formatText(@Nullable String header, @Nullable String body, @Nullable String tag, - @Nullable Map> contentPrettiers, String contentType) { - Map> prettiers = contentPrettiers; - if (contentPrettiers == null) { - prettiers = Collections.emptyMap(); + @Nullable Map> contentPrettifiers, String contentType) { + Map> prettifiers = contentPrettifiers; + if (contentPrettifiers == null) { + prettifiers = Collections.emptyMap(); } if (body == null || body.isEmpty()) { return header == null ? "" : header; } return (header == null || header.isEmpty() ? "" : header + LINE_DELIMITER + LINE_DELIMITER) + (tag == null || tag.isEmpty() ? "" : - tag + LINE_DELIMITER) + BODY_HIGHLIGHT + LINE_DELIMITER + (prettiers.containsKey(contentType) ? - prettiers.get(contentType).apply(body) : + tag + LINE_DELIMITER) + BODY_HIGHLIGHT + LINE_DELIMITER + (prettifiers.containsKey(contentType) ? + prettifiers.get(contentType).apply(body) : body) + LINE_DELIMITER + BODY_HIGHLIGHT; } @@ -162,10 +148,9 @@ public static Stream> toKeyValue(@Nonnull String headerValu } @Nonnull - public static Cookie toCookie(@Nonnull String name, @Nullable String value, @Nullable String comment, - @Nullable String path, @Nullable String domain, @Nullable Long maxAge, @Nullable Boolean secured, - @Nullable Boolean httpOnly, @Nullable Date expiryDate, @Nullable Integer version, - @Nullable String sameSite) { + public static Cookie toCookie(@Nonnull String name, @Nullable String value, @Nullable String comment, @Nullable String path, + @Nullable String domain, @Nullable Long maxAge, @Nullable Boolean secured, @Nullable Boolean httpOnly, + @Nullable Date expiryDate, @Nullable Integer version, @Nullable String sameSite) { Cookie cookie = new Cookie(name); cookie.setValue(value); cookie.setComment(comment); @@ -197,9 +182,7 @@ public static Cookie toCookie(@Nonnull String headerValue) { // Wed, 06-Sep-2023 11:22:09 GMT Date expiryDate = ofNullable(cookieMetadata.get("expires")).map(d -> { try { - return new SimpleDateFormat(DefaultCookieConverter.DEFAULT_COOKIE_DATE_FORMAT).parse(d.replace('-', - ' ' - )); + return new SimpleDateFormat(DefaultCookieConverter.DEFAULT_COOKIE_DATE_FORMAT).parse(d.replace('-', ' ')); } catch (ParseException e) { return null; } @@ -207,7 +190,8 @@ public static Cookie toCookie(@Nonnull String headerValue) { Integer version = cookieMetadata.get("version") == null ? null : Integer.valueOf(cookieMetadata.get("version")); String sameSite = cookieMetadata.get("samesite"); - return toCookie(nameValue.getKey(), + return toCookie( + nameValue.getKey(), nameValue.getValue(), comment, path, @@ -231,8 +215,10 @@ public static boolean isSetCookie(@Nullable String headerName) { @Nonnull private static Charset getCharset(@Nullable String contentType) { - return ofNullable(contentType).flatMap(h -> toKeyValue(h).filter(p -> "charset".equalsIgnoreCase(p.getKey())) - .findAny()).map(Pair::getValue).map(Charset::forName).orElse(StandardCharsets.UTF_8); + return ofNullable(contentType).flatMap(h -> toKeyValue(h).filter(p -> "charset".equalsIgnoreCase(p.getKey())).findAny()) + .map(Pair::getValue) + .map(Charset::forName) + .orElse(StandardCharsets.UTF_8); } @Nonnull diff --git a/src/main/java/com/epam/reportportal/formatting/http/HttpPartFormatter.java b/src/main/java/com/epam/reportportal/formatting/http/HttpPartFormatter.java index f4b45a8..61bf039 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/HttpPartFormatter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/HttpPartFormatter.java @@ -43,7 +43,7 @@ public class HttpPartFormatter { private String fileName; private Function headerConverter; - private Map> prettiers; + private Map> prettifiers; public HttpPartFormatter(@Nonnull PartType type, @Nonnull String mimeType, @Nonnull Object payload) { this.type = type; @@ -71,7 +71,7 @@ public byte[] getBinaryPayload() { } public String formatAsText() { - return HttpFormatUtils.formatText(formatHeaders(), getTextPayload(), BODY_PART_TAG, prettiers, mimeType); + return HttpFormatUtils.formatText(formatHeaders(), getTextPayload(), BODY_PART_TAG, prettifiers, mimeType); } public String formatForBinaryDataPrefix() { @@ -128,8 +128,17 @@ public void setHeaderConverter(Function headerConverter) { this.headerConverter = headerConverter; } - public void setPrettiers(Map> prettiers) { - this.prettiers = prettiers; + public void setPrettifiers(Map> prettifiers) { + this.prettifiers = prettifiers; + } + + /** + * @param prettifiers a map with the content type as a key and the prettifier function as a value + * @deprecated Use {@link #setPrettifiers(Map)} instead + */ + @Deprecated + public void setPrettiers(Map> prettifiers) { + setPrettifiers(prettifiers); } public enum PartType { @@ -148,7 +157,7 @@ public static class Builder { private String fileName; private Function headerConverter; - private Map> prettiers; + private Map> prettifiers; /*** * @@ -198,11 +207,21 @@ public Builder headerConverter(Function converter) { return this; } - public Builder prettiers(Map> formatPrettiers) { - this.prettiers = formatPrettiers; + public Builder prettifiers(Map> formatPrettifiers) { + this.prettifiers = formatPrettifiers; return this; } + /** + * @param formatPrettifiers a map with the content type as a key and the prettifier function as a value + * @return the builder instance + * @deprecated Use {@link #prettifiers(Map)} instead + */ + @Deprecated + public Builder prettiers(Map> formatPrettifiers) { + return prettifiers(formatPrettifiers); + } + public HttpPartFormatter build() { HttpPartFormatter formatter = new HttpPartFormatter(type, mimeType, payload); formatter.setControlName(controlName); @@ -210,7 +229,7 @@ public HttpPartFormatter build() { formatter.setCharset(charset); formatter.setFileName(fileName); formatter.setHeaderConverter(ofNullable(headerConverter).orElse(DefaultHttpHeaderConverter.INSTANCE)); - formatter.setPrettiers(ofNullable(prettiers).orElse(Constants.DEFAULT_PRETTIERS)); + formatter.setPrettifiers(ofNullable(prettifiers).orElse(Constants.DEFAULT_PRETTIFIERS)); return formatter; } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/HttpRequestFormatter.java b/src/main/java/com/epam/reportportal/formatting/http/HttpRequestFormatter.java index bfa138f..71d1ef6 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/HttpRequestFormatter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/HttpRequestFormatter.java @@ -43,7 +43,7 @@ public class HttpRequestFormatter implements HttpFormatter { private Function headerConverter; private Function cookieConverter; private Function paramConverter; - private Map> prettiers; + private Map> prettifiers; private List
headers; private List cookies; @@ -86,7 +86,7 @@ public String formatAsText() { if (BodyType.FORM == type) { return HttpFormatUtils.formatText(formatHead(), getFormBody(), BODY_FORM_TAG, paramConverter); } - return HttpFormatUtils.formatText(formatHead(), getTextBody(), BODY_TAG, prettiers, mimeType); + return HttpFormatUtils.formatText(formatHead(), getTextBody(), BODY_TAG, prettifiers, mimeType); } public void setUriConverter(@Nonnull Function uriConverter) { @@ -175,8 +175,17 @@ public List getMultipartBody() { throw new ClassCastException("Cannot return multipart body for body type: " + type.name()); } - public void setPrettiers(Map> prettiers) { - this.prettiers = prettiers; + public void setPrettifiers(Map> prettifiers) { + this.prettifiers = prettifiers; + } + + /** + * @param prettifiers a map with the content type as a key and the prettifier function as a value + * @deprecated Use {@link #setPrettifiers(Map)} instead + */ + @Deprecated + public void setPrettiers(Map> prettifiers) { + setPrettifiers(prettifiers); } public static class Builder { @@ -195,7 +204,7 @@ public static class Builder { private Object body; - private Map> prettiers; + private Map> prettifiers; public Builder(@Nonnull String requestMethod, @Nonnull String requestUri) { method = requestMethod; @@ -232,9 +241,10 @@ public Builder addCookie(Cookie cookie) { return this; } - public Builder addCookie(String name, String value, String comment, String path, String domain, Long maxAge, - Boolean secured, Boolean httpOnly, Date expiryDate, Integer version, String sameSite) { - return addCookie(HttpFormatUtils.toCookie(name, + public Builder addCookie(String name, String value, String comment, String path, String domain, Long maxAge, Boolean secured, + Boolean httpOnly, Date expiryDate, Integer version, String sameSite) { + return addCookie(HttpFormatUtils.toCookie( + name, value, comment, path, @@ -302,18 +312,28 @@ public Builder addBodyPart(HttpPartFormatter part) { return this; } - public Builder prettiers(Map> formatPrettiers) { - this.prettiers = formatPrettiers; + public Builder prettifiers(Map> formatPrettifiers) { + this.prettifiers = formatPrettifiers; return this; } + /** + * @param formatPrettifiers a map with the content type as a key and the prettifier function as a value + * @return the builder instance + * @deprecated Use {@link #prettifiers(Map)} instead + */ + @Deprecated + public Builder prettiers(Map> formatPrettifiers) { + return prettifiers(formatPrettifiers); + } + public HttpRequestFormatter build() { HttpRequestFormatter result = new HttpRequestFormatter(method, uri); result.setUriConverter(ofNullable(uriConverter).orElse(DefaultUriConverter.INSTANCE)); result.setHeaderConverter(ofNullable(headerConverter).orElse(DefaultHttpHeaderConverter.INSTANCE)); result.setCookieConverter(ofNullable(cookieConverter).orElse(DefaultCookieConverter.INSTANCE)); result.setParamConverter(ofNullable(paramConverter).orElse(DefaultFormParamConverter.INSTANCE)); - result.setPrettiers(ofNullable(prettiers).orElse(Constants.DEFAULT_PRETTIERS)); + result.setPrettifiers(ofNullable(prettifiers).orElse(DEFAULT_PRETTIFIERS)); result.setHeaders(headers); result.setCookies(cookies); if (body != null) { diff --git a/src/main/java/com/epam/reportportal/formatting/http/HttpResponseFormatter.java b/src/main/java/com/epam/reportportal/formatting/http/HttpResponseFormatter.java index a6a7f06..053981d 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/HttpResponseFormatter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/HttpResponseFormatter.java @@ -41,7 +41,7 @@ public class HttpResponseFormatter implements HttpFormatter { private Function headerConverter; private Function cookieConverter; - private Map> prettiers; + private Map> prettifiers; private List
headers; private List cookies; @@ -106,7 +106,7 @@ public void setBody(Object body) { @Override @Nonnull public String formatAsText() { - return HttpFormatUtils.formatText(formatHead(), getTextBody(), BODY_TAG, prettiers, mimeType); + return HttpFormatUtils.formatText(formatHead(), getTextBody(), BODY_TAG, prettifiers, mimeType); } public void setHeaderConverter(Function headerConverter) { @@ -142,8 +142,17 @@ public byte[] getBinaryBody() { throw new ClassCastException("Cannot return binary body for body type: " + type.name()); } - public void setPrettiers(Map> prettiers) { - this.prettiers = prettiers; + public void setPrettifiers(Map> prettifiers) { + this.prettifiers = prettifiers; + } + + /** + * @param prettifiers a map with the content type as a key and the prettifier function as a value + * @deprecated Use {@link #setPrettifiers(Map)} instead + */ + @Deprecated + public void setPrettiers(Map> prettifiers) { + setPrettifiers(prettifiers); } public static class Builder { @@ -161,7 +170,7 @@ public static class Builder { private Object body; - private Map> prettiers; + private Map> prettifiers; public Builder(int statusCode, String reasonPhrase) { this.code = statusCode; @@ -188,9 +197,10 @@ public Builder addCookie(Cookie cookie) { return this; } - public Builder addCookie(String name, String value, String comment, String path, String domain, Long maxAge, - Boolean secured, Boolean httpOnly, Date expiryDate, Integer version, String sameSite) { - return addCookie(HttpFormatUtils.toCookie(name, + public Builder addCookie(String name, String value, String comment, String path, String domain, Long maxAge, Boolean secured, + Boolean httpOnly, Date expiryDate, Integer version, String sameSite) { + return addCookie(HttpFormatUtils.toCookie( + name, value, comment, path, @@ -226,16 +236,26 @@ public Builder bodyBytes(String mimeType, byte[] payload) { return this; } - public Builder prettiers(Map> formatPrettiers) { - this.prettiers = formatPrettiers; + public Builder prettifiers(Map> formatPrettifiers) { + this.prettifiers = formatPrettifiers; return this; } + /** + * @param formatPrettifiers a map with the content type as a key and the prettifier function as a value + * @return the builder instance + * @deprecated Use {@link #prettifiers(Map)} instead + */ + @Deprecated + public Builder prettiers(Map> formatPrettifiers) { + return prettifiers(formatPrettifiers); + } + public HttpResponseFormatter build() { HttpResponseFormatter result = new HttpResponseFormatter(code, phrase); result.setHeaderConverter(ofNullable(headerConverter).orElse(DefaultHttpHeaderConverter.INSTANCE)); result.setCookieConverter(ofNullable(cookieConverter).orElse(DefaultCookieConverter.INSTANCE)); - result.setPrettiers(ofNullable(prettiers).orElse(Constants.DEFAULT_PRETTIERS)); + result.setPrettifiers(ofNullable(prettifiers).orElse(DEFAULT_PRETTIFIERS)); result.setHeaders(headers); result.setCookies(cookies); if (body != null) { diff --git a/src/main/java/com/epam/reportportal/formatting/http/converters/DefaultCookieConverter.java b/src/main/java/com/epam/reportportal/formatting/http/converters/DefaultCookieConverter.java index 19318ba..b68cb0d 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/converters/DefaultCookieConverter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/converters/DefaultCookieConverter.java @@ -72,21 +72,15 @@ public DefaultCookieConverter() { ofNullable(c.getComment()).ifPresent(comment -> cookieValues.add(COMMENT + ATTRIBUTE_VALUE + comment)); ofNullable(c.getPath()).ifPresent(path -> cookieValues.add(PATH + ATTRIBUTE_VALUE + path)); ofNullable(c.getDomain()).ifPresent(domain -> cookieValues.add(DOMAIN + ATTRIBUTE_VALUE + domain)); - ofNullable(c.getMaxAge()).filter(m -> m != UNDEFINED) - .ifPresent(maxAge -> cookieValues.add(MAX_AGE + ATTRIBUTE_VALUE + maxAge)); - ofNullable(c.getSecured()).filter(s -> s) - .ifPresent(secured -> cookieValues.add(SECURE + ATTRIBUTE_VALUE + secured)); - ofNullable(c.getHttpOnly()).filter(h -> h) - .ifPresent(httpOnly -> cookieValues.add(HTTP_ONLY + ATTRIBUTE_VALUE + httpOnly)); + ofNullable(c.getMaxAge()).filter(m -> m != UNDEFINED).ifPresent(maxAge -> cookieValues.add(MAX_AGE + ATTRIBUTE_VALUE + maxAge)); + ofNullable(c.getSecured()).filter(s -> s).ifPresent(secured -> cookieValues.add(SECURE + ATTRIBUTE_VALUE + secured)); + ofNullable(c.getHttpOnly()).filter(h -> h).ifPresent(httpOnly -> cookieValues.add(HTTP_ONLY + ATTRIBUTE_VALUE + httpOnly)); SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); sdf.setTimeZone(timeZone); - ofNullable(c.getExpiryDate()).ifPresent(expireDate -> cookieValues.add( - EXPIRES + ATTRIBUTE_VALUE + sdf.format(expireDate))); + ofNullable(c.getExpiryDate()).ifPresent(expireDate -> cookieValues.add(EXPIRES + ATTRIBUTE_VALUE + sdf.format(expireDate))); ofNullable(c.getVersion()).ifPresent(version -> cookieValues.add(VERSION + ATTRIBUTE_VALUE + version)); ofNullable(c.getSameSite()).ifPresent(sameSite -> cookieValues.add(SAME_SITE + ATTRIBUTE_VALUE + sameSite)); - return cookieValues.isEmpty() ? - c.getName() : - c.getName() + NAME_DELIMITER + String.join(ATTRIBUTE_SEPARATOR, cookieValues); + return cookieValues.isEmpty() ? c.getName() : c.getName() + NAME_DELIMITER + String.join(ATTRIBUTE_SEPARATOR, cookieValues); }).orElse(null); } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingCookieConverter.java b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingCookieConverter.java index 9e3ff51..3cf14f3 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingCookieConverter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingCookieConverter.java @@ -29,7 +29,8 @@ public class SanitizingCookieConverter implements Function { - public static final Set SESSION_COOKIES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("sid", + public static final Set SESSION_COOKIES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + "sid", "session", "session_id", "sessionId", diff --git a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingHttpHeaderConverter.java b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingHttpHeaderConverter.java index 080ffad..adc35dc 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingHttpHeaderConverter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingHttpHeaderConverter.java @@ -28,8 +28,7 @@ import static java.util.Optional.ofNullable; public class SanitizingHttpHeaderConverter implements Function { - public static final Set SENSITIVE_HEADERS = Collections.unmodifiableSet(new HashSet<>(Collections.singletonList( - "Authorization"))); + public static final Set SENSITIVE_HEADERS = Collections.unmodifiableSet(new HashSet<>(Collections.singletonList("Authorization"))); public static final Function INSTANCE = new SanitizingHttpHeaderConverter(); @@ -45,12 +44,10 @@ private SanitizingHttpHeaderConverter() { @Override public @Nullable String apply(@Nullable Header header) { - return DefaultHttpHeaderConverter.INSTANCE.apply(ofNullable(header).filter(h -> sanitizeSet.contains(h.getName())) - .map(h -> { - Header newHeader = h.clone(); - newHeader.setValue(REMOVED_TAG); - return newHeader; - }) - .orElse(header)); + return DefaultHttpHeaderConverter.INSTANCE.apply(ofNullable(header).filter(h -> sanitizeSet.contains(h.getName())).map(h -> { + Header newHeader = h.clone(); + newHeader.setValue(REMOVED_TAG); + return newHeader; + }).orElse(header)); } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingUriConverter.java b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingUriConverter.java index 5cc3926..1f32d84 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingUriConverter.java +++ b/src/main/java/com/epam/reportportal/formatting/http/converters/SanitizingUriConverter.java @@ -29,16 +29,12 @@ public class SanitizingUriConverter implements Function { public String apply(String uriStr) { try { URI uri = URI.create(uriStr); - String userInfo = ofNullable(uri.getUserInfo()).filter(info -> !info.isEmpty()) - .map(info -> info.split(":", 2)) - .map(info -> { - if (info.length > 1) { - return new String[]{info[0], REMOVED_TAG}; - } - return info; - }) - .map(info -> String.join(":", info)) - .orElse(null); + String userInfo = ofNullable(uri.getUserInfo()).filter(info -> !info.isEmpty()).map(info -> info.split(":", 2)).map(info -> { + if (info.length > 1) { + return new String[] { info[0], REMOVED_TAG }; + } + return info; + }).map(info -> String.join(":", info)).orElse(null); return new URI( uri.getScheme(), userInfo, diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettiers/HtmlPrettier.java b/src/main/java/com/epam/reportportal/formatting/http/prettiers/HtmlPrettier.java index b2772df..7a09b60 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/prettiers/HtmlPrettier.java +++ b/src/main/java/com/epam/reportportal/formatting/http/prettiers/HtmlPrettier.java @@ -16,30 +16,21 @@ package com.epam.reportportal.formatting.http.prettiers; -import org.jsoup.Jsoup; +import com.epam.reportportal.formatting.http.prettifiers.HtmlPrettifier; import org.jsoup.nodes.Document; -public class HtmlPrettier implements Prettier { +/** + * @deprecated Use {@link HtmlPrettifier} instead. + */ +@Deprecated +public class HtmlPrettier extends HtmlPrettifier { private static final Document.OutputSettings OUTPUT_SETTINGS = new Document.OutputSettings().indentAmount(2); - public static final HtmlPrettier INSTANCE = new HtmlPrettier(); - - private final Document.OutputSettings settings; - public HtmlPrettier(Document.OutputSettings outputSettings) { - settings = outputSettings; + super(outputSettings); } private HtmlPrettier() { this(OUTPUT_SETTINGS); } - - @Override - public String apply(String html) { - try { - return Jsoup.parse(html).outputSettings(settings).html().trim(); - } catch (Exception ignore) { - return html; - } - } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettiers/JsonPrettier.java b/src/main/java/com/epam/reportportal/formatting/http/prettiers/JsonPrettier.java index a7770fb..5aad695 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/prettiers/JsonPrettier.java +++ b/src/main/java/com/epam/reportportal/formatting/http/prettiers/JsonPrettier.java @@ -16,31 +16,21 @@ package com.epam.reportportal.formatting.http.prettiers; -import com.fasterxml.jackson.databind.JsonNode; +import com.epam.reportportal.formatting.http.prettifiers.JsonPrettifier; import com.fasterxml.jackson.databind.ObjectMapper; -public class JsonPrettier implements Prettier { +/** + * @deprecated Use {@link JsonPrettifier} instead. + */ +@Deprecated +public class JsonPrettier extends JsonPrettifier { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - public static final JsonPrettier INSTANCE = new JsonPrettier(); - - private final ObjectMapper mapper; - public JsonPrettier(ObjectMapper objectMapper) { - mapper = objectMapper; + super(objectMapper); } private JsonPrettier() { this(OBJECT_MAPPER); } - - @Override - public String apply(String json) { - try { - JsonNode node = mapper.readTree(json); - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node).trim(); - } catch (Exception ignore) { - return json; - } - } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettiers/Prettier.java b/src/main/java/com/epam/reportportal/formatting/http/prettiers/Prettier.java index ff9df6a..3ef0b50 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/prettiers/Prettier.java +++ b/src/main/java/com/epam/reportportal/formatting/http/prettiers/Prettier.java @@ -16,7 +16,10 @@ package com.epam.reportportal.formatting.http.prettiers; -import java.util.function.Function; +import com.epam.reportportal.formatting.http.prettifiers.Prettifier; -public interface Prettier extends Function { +/** + * @deprecated Use {@link Prettifier} instead. + */ +public interface Prettier extends Prettifier { } diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettiers/XmlPrettier.java b/src/main/java/com/epam/reportportal/formatting/http/prettiers/XmlPrettier.java index afee5aa..d90fad0 100644 --- a/src/main/java/com/epam/reportportal/formatting/http/prettiers/XmlPrettier.java +++ b/src/main/java/com/epam/reportportal/formatting/http/prettiers/XmlPrettier.java @@ -16,22 +16,16 @@ package com.epam.reportportal.formatting.http.prettiers; -import org.xml.sax.InputSource; +import com.epam.reportportal.formatting.http.prettifiers.XmlPrettifier; -import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; import java.util.HashMap; import java.util.Map; -public class XmlPrettier implements Prettier { +/** + * @deprecated Use {@link XmlPrettifier} instead. + */ +public class XmlPrettier extends XmlPrettifier { private static final int DEFAULT_INDENT = 2; private static final Map DEFAULT_OUTPUT_PROPERTIES = new HashMap() {{ put(OutputKeys.ENCODING, "UTF-8"); @@ -39,39 +33,11 @@ public class XmlPrettier implements Prettier { put(OutputKeys.INDENT, "yes"); }}; - public static final XmlPrettier INSTANCE = new XmlPrettier(); - - private final ThreadLocal threadLocal; - public XmlPrettier(int indent, Map outputSettings) { - threadLocal = ThreadLocal.withInitial(() -> { - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - transformerFactory.setAttribute("indent-number", indent); - Transformer transformer; - try { - transformer = transformerFactory.newTransformer(); - } catch (TransformerConfigurationException e) { - throw new IllegalArgumentException(e.getMessage(), e); - } - outputSettings.forEach(transformer::setOutputProperty); - return transformer; - }); + super(indent, outputSettings); } private XmlPrettier() { this(DEFAULT_INDENT, DEFAULT_OUTPUT_PROPERTIES); } - - @Override - public String apply(String xml) { - try { - InputSource src = new InputSource(new StringReader(xml)); - org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src); - Writer out = new StringWriter(); - threadLocal.get().transform(new DOMSource(document), new StreamResult(out)); - return out.toString().trim(); - } catch (Exception ignore) { - return xml; - } - } } diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettifiers/HtmlPrettifier.java b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/HtmlPrettifier.java new file mode 100644 index 0000000..ccdb36e --- /dev/null +++ b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/HtmlPrettifier.java @@ -0,0 +1,45 @@ +/* + * Copyright 2022 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.formatting.http.prettifiers; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +public class HtmlPrettifier implements Prettifier { + private static final Document.OutputSettings OUTPUT_SETTINGS = new Document.OutputSettings().indentAmount(2); + + public static final HtmlPrettifier INSTANCE = new HtmlPrettifier(); + + private final Document.OutputSettings settings; + + public HtmlPrettifier(Document.OutputSettings outputSettings) { + settings = outputSettings; + } + + private HtmlPrettifier() { + this(OUTPUT_SETTINGS); + } + + @Override + public String apply(String html) { + try { + return Jsoup.parse(html).outputSettings(settings).html().trim(); + } catch (Exception ignore) { + return html; + } + } +} diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettifiers/JsonPrettifier.java b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/JsonPrettifier.java new file mode 100644 index 0000000..09f1410 --- /dev/null +++ b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/JsonPrettifier.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.formatting.http.prettifiers; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JsonPrettifier implements Prettifier { + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + public static final JsonPrettifier INSTANCE = new JsonPrettifier(); + + private final ObjectMapper mapper; + + public JsonPrettifier(ObjectMapper objectMapper) { + mapper = objectMapper; + } + + private JsonPrettifier() { + this(OBJECT_MAPPER); + } + + @Override + public String apply(String json) { + try { + JsonNode node = mapper.readTree(json); + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node).trim(); + } catch (Exception ignore) { + return json; + } + } +} diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettifiers/Prettifier.java b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/Prettifier.java new file mode 100644 index 0000000..01370e9 --- /dev/null +++ b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/Prettifier.java @@ -0,0 +1,22 @@ +/* + * Copyright 2025 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.formatting.http.prettifiers; + +import java.util.function.Function; + +public interface Prettifier extends Function { +} diff --git a/src/main/java/com/epam/reportportal/formatting/http/prettifiers/XmlPrettifier.java b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/XmlPrettifier.java new file mode 100644 index 0000000..807bc30 --- /dev/null +++ b/src/main/java/com/epam/reportportal/formatting/http/prettifiers/XmlPrettifier.java @@ -0,0 +1,77 @@ +/* + * Copyright 2022 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.reportportal.formatting.http.prettifiers; + +import org.xml.sax.InputSource; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.HashMap; +import java.util.Map; + +public class XmlPrettifier implements Prettifier { + private static final int DEFAULT_INDENT = 2; + private static final Map DEFAULT_OUTPUT_PROPERTIES = new HashMap() {{ + put(OutputKeys.ENCODING, "UTF-8"); + put(OutputKeys.OMIT_XML_DECLARATION, "yes"); + put(OutputKeys.INDENT, "yes"); + }}; + + public static final XmlPrettifier INSTANCE = new XmlPrettifier(); + + private final ThreadLocal threadLocal; + + public XmlPrettifier(int indent, Map outputSettings) { + threadLocal = ThreadLocal.withInitial(() -> { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + transformerFactory.setAttribute("indent-number", indent); + Transformer transformer; + try { + transformer = transformerFactory.newTransformer(); + } catch (TransformerConfigurationException e) { + throw new IllegalArgumentException(e.getMessage(), e); + } + outputSettings.forEach(transformer::setOutputProperty); + return transformer; + }); + } + + private XmlPrettifier() { + this(DEFAULT_INDENT, DEFAULT_OUTPUT_PROPERTIES); + } + + @Override + public String apply(String xml) { + try { + InputSource src = new InputSource(new StringReader(xml)); + org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src); + Writer out = new StringWriter(); + threadLocal.get().transform(new DOMSource(document), new StreamResult(out)); + return out.toString().trim(); + } catch (Exception ignore) { + return xml; + } + } +} diff --git a/src/test/java/com/epam/reportportal/formatting/http/HttpFormatUtilsTest.java b/src/test/java/com/epam/reportportal/formatting/http/HttpFormatUtilsTest.java index 6a74a98..9ebfd2d 100644 --- a/src/test/java/com/epam/reportportal/formatting/http/HttpFormatUtilsTest.java +++ b/src/test/java/com/epam/reportportal/formatting/http/HttpFormatUtilsTest.java @@ -34,8 +34,7 @@ public static Iterable contentTypes() { new Object[] { "text/html; charset=utf-16", "text/html" }, new Object[] { "application/json", "application/json" }, new Object[] { null, "application/octet-stream" }, - new Object[] { "application/x-www-form-urlencoded; charset=ISO-8859-1", - "application/x-www-form-urlencoded" } + new Object[] { "application/x-www-form-urlencoded; charset=ISO-8859-1", "application/x-www-form-urlencoded" } ); } @@ -70,8 +69,7 @@ public static Iterable bodyTypes() { new Object[] { null, BodyType.NONE }, new Object[] { "application/x-www-form-urlencoded; charset=ISO-8859-1", BodyType.FORM }, new Object[] { "image/jpeg", BodyType.BINARY }, - new Object[] { "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", - BodyType.MULTIPART }, + new Object[] { "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", BodyType.MULTIPART }, new Object[] { "", BodyType.NONE } ); } @@ -84,8 +82,8 @@ public void testGetBodyType(String contentType, BodyType expected) { public static Iterable headerValues() { return Arrays.asList( - new Object[] { "Content-Type: multipart/form-data; boundary=LjalBsLkAYGgsOfzTPStiqo8-Ur9wnV9", - "Content-Type", "multipart/form-data; boundary=LjalBsLkAYGgsOfzTPStiqo8-Ur9wnV9" }, + new Object[] { "Content-Type: multipart/form-data; boundary=LjalBsLkAYGgsOfzTPStiqo8-Ur9wnV9", "Content-Type", + "multipart/form-data; boundary=LjalBsLkAYGgsOfzTPStiqo8-Ur9wnV9" }, new Object[] { "Host: docker.local:8080", "Host", "docker.local:8080" }, new Object[] { "X-Custom-Header: a: b", "X-Custom-Header", "a: b" }, new Object[] { "X-Custom-Header: ", "X-Custom-Header", "" }, @@ -112,15 +110,14 @@ public void testToHeader(String headerLine, String expectedKey, String expectedV public static Iterable cookieValues() { return Arrays.asList( new Object[] { "test=value", "test", "value", null, null, false, false }, - new Object[] { "test=value; expires=" + DATE_STR + "; path=/; secure; httponly", "test", "value", - DATE_CAL.getTime(), "/", true, true } + new Object[] { "test=value; expires=" + DATE_STR + "; path=/; secure; httponly", "test", "value", DATE_CAL.getTime(), "/", + true, true } ); } @ParameterizedTest @MethodSource("cookieValues") - public void testToCookie(String headerLine, String name, String value, Date date, String path, boolean secure, - boolean http) { + public void testToCookie(String headerLine, String name, String value, Date date, String path, boolean secure, boolean http) { Cookie cookie = HttpFormatUtils.toCookie(headerLine); assertThat(cookie.getName(), equalTo(name)); assertThat(cookie.getValue(), equalTo(value)); @@ -132,11 +129,7 @@ public void testToCookie(String headerLine, String name, String value, Date date } public static Iterable cookieHeaders() { - return Arrays.asList( - new Object[] { "cookie", true }, - new Object[] { "Cookie", true }, - new Object[] { "Cook", false } - ); + return Arrays.asList(new Object[] { "cookie", true }, new Object[] { "Cookie", true }, new Object[] { "Cook", false }); } @ParameterizedTest @@ -146,11 +139,7 @@ public void testIsCookie(String header, boolean expectedResult) { } public static Iterable setCookieHeaders() { - return Arrays.asList( - new Object[] { "set-cookie", true }, - new Object[] { "Set-Cookie", true }, - new Object[] { "setcookie", false } - ); + return Arrays.asList(new Object[] { "set-cookie", true }, new Object[] { "Set-Cookie", true }, new Object[] { "setcookie", false }); } @ParameterizedTest diff --git a/src/test/java/com/epam/reportportal/formatting/http/HttpRequestFormatterTest.java b/src/test/java/com/epam/reportportal/formatting/http/HttpRequestFormatterTest.java index e74ecf0..7fdb929 100644 --- a/src/test/java/com/epam/reportportal/formatting/http/HttpRequestFormatterTest.java +++ b/src/test/java/com/epam/reportportal/formatting/http/HttpRequestFormatterTest.java @@ -35,7 +35,8 @@ public class HttpRequestFormatterTest { public static final String TEST_TEXT = "this is a test"; public static Iterable textBodies() { - return Arrays.asList(new Object[] { ContentType.APPLICATION_JSON, "{\"object\": {\"key\": \"value\"}}", + return Arrays.asList( + new Object[] { ContentType.APPLICATION_JSON, "{\"object\": {\"key\": \"value\"}}", "{\n" + " \"object\" : {\n" + " \"key\" : \"value\"\n" + " }\n" + "}" }, new Object[] { ContentType.APPLICATION_XML, "value", "\n" + " \n" + " value\n" + " \n" + "" }, @@ -50,15 +51,15 @@ private static String getRequestString() { } private static String getTextBody(ContentType contentType, String bodyTag, String payload) { - return getRequestString() + HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": " + contentType - + LINE_DELIMITER + LINE_DELIMITER + bodyTag + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + payload - + LINE_DELIMITER + BODY_HIGHLIGHT; + return getRequestString() + HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": " + contentType + LINE_DELIMITER + + LINE_DELIMITER + bodyTag + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + payload + LINE_DELIMITER + BODY_HIGHLIGHT; } @ParameterizedTest @MethodSource("textBodies") public void verify_request_text_body_format(ContentType contentType, String body, String expected) { - HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).addHeader(HttpHeaders.CONTENT_TYPE, + HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).addHeader( + HttpHeaders.CONTENT_TYPE, contentType.toString() ).bodyText(contentType.getMimeType(), body).build(); assertThat(formatter.formatAsText(), equalTo(getTextBody(contentType, BODY_TAG, expected))); @@ -69,7 +70,8 @@ public void verify_request_form_body_format() { String body = "test1=test1&test2=wefwfqwef%20df%20qwef%20%23%24%25&test3=&F%23%24%25DFFG=dclk%20345%25%2056%20"; String expectedBody = "test1: test1\ntest2: wefwfqwef df qwef #$%\ntest3: \nF#$%DFFG: dclk 345% 56 "; ContentType contentType = ContentType.APPLICATION_FORM_URLENCODED; - HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).addHeader(HttpHeaders.CONTENT_TYPE, + HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).addHeader( + HttpHeaders.CONTENT_TYPE, contentType.toString() ).bodyParams(body).build(); assertThat(formatter.formatAsText(), equalTo(getTextBody(contentType, BODY_FORM_TAG, expectedBody))); @@ -77,14 +79,14 @@ public void verify_request_form_body_format() { @Test public void verify_request_no_header_format() { - HttpRequestFormatter formatter = new HttpRequestFormatter.Builder(REQUEST_METHOD, REQUEST_URL).bodyText( - ContentType.TEXT_PLAIN.getMimeType(), - TEST_TEXT - ).build(); + HttpRequestFormatter formatter = new HttpRequestFormatter.Builder( + REQUEST_METHOD, + REQUEST_URL + ).bodyText(ContentType.TEXT_PLAIN.getMimeType(), TEST_TEXT).build(); assertThat( formatter.formatAsText(), - equalTo(getRequestString() + BODY_TAG + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + TEST_TEXT - + LINE_DELIMITER + BODY_HIGHLIGHT) + equalTo(getRequestString() + BODY_TAG + LINE_DELIMITER + BODY_HIGHLIGHT + LINE_DELIMITER + TEST_TEXT + LINE_DELIMITER + + BODY_HIGHLIGHT) ); } } diff --git a/src/test/java/com/epam/reportportal/formatting/http/converters/ConvertersTest.java b/src/test/java/com/epam/reportportal/formatting/http/converters/ConvertersTest.java index 9ad1f31..6a197c1 100644 --- a/src/test/java/com/epam/reportportal/formatting/http/converters/ConvertersTest.java +++ b/src/test/java/com/epam/reportportal/formatting/http/converters/ConvertersTest.java @@ -19,10 +19,10 @@ import com.epam.reportportal.formatting.http.Constants; import com.epam.reportportal.formatting.http.entities.Cookie; import com.epam.reportportal.formatting.http.entities.Header; -import com.epam.reportportal.formatting.http.prettiers.HtmlPrettier; -import com.epam.reportportal.formatting.http.prettiers.JsonPrettier; -import com.epam.reportportal.formatting.http.prettiers.Prettier; -import com.epam.reportportal.formatting.http.prettiers.XmlPrettier; +import com.epam.reportportal.formatting.http.prettifiers.HtmlPrettifier; +import com.epam.reportportal.formatting.http.prettifiers.JsonPrettifier; +import com.epam.reportportal.formatting.http.prettifiers.Prettifier; +import com.epam.reportportal.formatting.http.prettifiers.XmlPrettifier; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -46,8 +46,8 @@ public static Iterable cookieCases() { cookie2.setPath("/"); return Arrays.asList( - new Object[] { cookie1, "session_id: " + Constants.REMOVED_TAG - + "; Comment=test comment; Path=/; Domain=example.com; HttpOnly=true" }, + new Object[] { cookie1, + "session_id: " + Constants.REMOVED_TAG + "; Comment=test comment; Path=/; Domain=example.com; HttpOnly=true" }, new Object[] { null, null }, new Object[] { cookie2, "test_cookie: my_test_session_id; Path=/" } ); @@ -61,8 +61,7 @@ public void testSessionIdHeaderRemove(Cookie input, String expected) { public static Iterable headerCases() { return Arrays.asList( - new Object[] { new Header("Authorization", "Bearer test_token"), - "Authorization: " + Constants.REMOVED_TAG }, + new Object[] { new Header("Authorization", "Bearer test_token"), "Authorization: " + Constants.REMOVED_TAG }, new Object[] { null, null }, new Object[] { new Header("Accept", "*/*"), "Accept: \\*/\\*" } ); @@ -92,28 +91,27 @@ public void testUriSanitizingConverter(String input, String expected) { public static Iterable prettierData() { return Arrays.asList( - new Object[] { JsonPrettier.INSTANCE, "{\"object\": {\"key\": \"value\"}}", + new Object[] { JsonPrettifier.INSTANCE, "{\"object\": {\"key\": \"value\"}}", "{\n \"object\" : {\n \"key\" : \"value\"\n }\n}" }, - new Object[] { XmlPrettier.INSTANCE, "value", + new Object[] { XmlPrettifier.INSTANCE, "value", "\n \n value\n \n" }, - new Object[] { HtmlPrettier.INSTANCE, "

hello world

", + new Object[] { HtmlPrettifier.INSTANCE, "

hello world

", "\n \n \n

hello world

\n \n" }, - new Object[] { JsonPrettier.INSTANCE, "^$V\\B#$^", "^$V\\B#$^" }, - new Object[] { XmlPrettier.INSTANCE, "^$V\\B#$^", "^$V\\B#$^" }, - new Object[] { HtmlPrettier.INSTANCE, "^$V\\B#$\"^", + new Object[] { JsonPrettifier.INSTANCE, "^$V\\B#$^", "^$V\\B#$^" }, + new Object[] { XmlPrettifier.INSTANCE, "^$V\\B#$^", "^$V\\B#$^" }, + new Object[] { HtmlPrettifier.INSTANCE, "^$V\\B#$\"^", "\n \n \n ^$V\\B#$\"^\n \n" }, - new Object[] { HtmlPrettier.INSTANCE, "\n \n \n </\n \n" }, - new Object[] { HtmlPrettier.INSTANCE, "", "\n \n \n" }, - new Object[] { JsonPrettier.INSTANCE, null, null }, - new Object[] { XmlPrettier.INSTANCE, null, null }, - new Object[] { HtmlPrettier.INSTANCE, null, null } + new Object[] { HtmlPrettifier.INSTANCE, "\n \n \n </\n \n" }, + new Object[] { HtmlPrettifier.INSTANCE, "", "\n \n \n" }, + new Object[] { JsonPrettifier.INSTANCE, null, null }, + new Object[] { HtmlPrettifier.INSTANCE, null, null }, + new Object[] { HtmlPrettifier.INSTANCE, null, null } ); } @ParameterizedTest @MethodSource("prettierData") - public void test_prettiers(Prettier prettier, String input, String expected) { + public void test_prettifier(Prettifier prettier, String input, String expected) { assertThat(prettier.apply(input), equalTo(expected)); } }