reportportal / logger-java-rest-assured

Report Portal logger for REST Assured
Apache License 2.0
2 stars 0 forks source link

Custom prettiers don't get registered #23

Closed Dyadko closed 1 year ago

Dyadko commented 1 year ago

Describe the bug Registered custom prettier provokes

java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap')
    at com.epam.reportportal.restassured.support.HttpEntityFactory.createHttpRequestFormatter(HttpEntityFactory.java:144)
    at com.epam.reportportal.restassured.ReportPortalRestAssuredLoggingFilter.filter(ReportPortalRestAssuredLoggingFilter.java:167)

Steps to Reproduce Steps to reproduce the behavior:

  1. Register new prettier custom_type as per README.md
  2. for me it was like

    public class BaseTest {
    private static final Map<String, Function<String, String>> REPORTPORTAL_PRETTIERS = Map.of(
            org.apache.http.entity.ContentType.APPLICATION_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.APPLICATION_SOAP_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.APPLICATION_ATOM_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.APPLICATION_SVG_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.APPLICATION_XHTML_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.TEXT_XML.getMimeType(), XmlPrettier.INSTANCE,
            org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType(), JsonPrettier.INSTANCE,
            "text/json", JsonPrettier.INSTANCE,
            "custom_type", JsonPrettier.INSTANCE,
            org.apache.http.entity.ContentType.TEXT_HTML.getMimeType(), HtmlPrettier.INSTANCE
    );
    
    static {
        RestAssured.useRelaxedHTTPSValidation();
        RestAssured.filters(
                new ReportPortalRestAssuredLoggingFilter(
                        42,
                        LogLevel.INFO)
                        .setContentPrettiers(REPORTPORTAL_PRETTIERS)
        );
    }
    
    public static RequestSpecification getClient(String baseUri) {
        return RestAssured
            .given()
            .baseUri(baseUri);
    }
    }
  3. Send a request with a header Content-Type: custom_type and valid json in body.

Expected behavior According to REPORTPORTAL_PRETTIERS the Content-Type: custom_type should be treated as usual JSON

Actual behavior Unable to send a request as ReportPortal fails being unable to cast String to byte[]

java.lang.ClassCastException: class java.lang.String cannot be cast to class [B (java.lang.String and [B are in module java.base of loader 'bootstrap')
    at com.epam.reportportal.restassured.support.HttpEntityFactory.createHttpRequestFormatter(HttpEntityFactory.java:144)
    at com.epam.reportportal.restassured.ReportPortalRestAssuredLoggingFilter.filter(ReportPortalRestAssuredLoggingFilter.java:167)

Dependency versions

    java 11
    gradle 7.4
    implementation "com.epam.reportportal:logger-java-rest-assured:5.2.4"
    implementation "com.epam.reportportal:agent-java-cucumber6:5.1.2"

Additional context Seems like any mimeTypes, if they are not in DEFAULT_PRETTIERS map predefined in code, lead to BodyType detected as BINARY. For BINARY body is parsed as byte[] even being String. I'd vote to detect BodyType as TEXT if a prettier is registered - as parsing logic is defined in Prettier implementation.

Dyadko commented 1 year ago

Probably not a bug

resolved issue with setting custom BodyTypeMap:

    public static final Map<String, BodyType> BODY_TYPE_MAP = Map.of(
            "text/json", BodyType.TEXT,
            "custom_type", BodyType.TEXT,
            org.apache.http.entity.ContentType.TEXT_XML.getMimeType(), BodyType.TEXT,
            org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType(), BodyType.TEXT,
            org.apache.http.entity.ContentType.TEXT_HTML.getMimeType(), BodyType.TEXT
    );

    static {
        RestAssured.useRelaxedHTTPSValidation();
        RestAssured.filters(
                new ReportPortalRestAssuredLoggingFilter(
                        42,
                        LogLevel.INFO)
                        .setBodyTypeMap(BODY_TYPE_MAP),
                new RestAssuredConsoleFilter(),
                new AllureRestAssured()
        );
    }