rest-assured icon indicating copy to clipboard operation
rest-assured copied to clipboard

Removing headers that were automatically added

Open mmcdermo2 opened this issue 10 months ago • 0 comments

I’m trying to write a test at the moment to perform a multipart POST operation. However, the request is generating some headers that I didn’t add, and that I don’t want (resulting in the request getting filtered by our WAF). The headers in question are:

Transfer-Encoding Accept-Encoding Content-Transfer-Encoding

I’ve tried casting the Request to a FilterableRequest and removing the headers, but they don’t get removed. I suspect they’re getting added by the Apache HttpClient ? Is there any way to remove such headers using REST Assured ? Below is what I have so far

        byte[] fileContent;
        try {
            fileContent = Files.readAllBytes(Path.of(file.getPath()));
        }
        catch (IOException ioException) {
            throw new TestException(ioException.getMessage());
        }

        FilterableRequestSpecification given = (FilterableRequestSpecification)
        given()
                .config(RestAssuredConfig.config()
                        .sslConfig(SSLConfig.sslConfig()
                                .relaxedHTTPSValidation()
                                .allowAllHostnames()
                                )
                )
                .contentType(MULTIPART_FORM_DATA)
                .accept("application/json")
                .auth().oauth2(token.getAccessToken())
                .multiPart(new MultiPartSpecBuilder(fileContent)
                        .controlName("file")
                        .fileName(file.getName())
                        .mimeType("text/xml")
                        .build());

        final String response =
        given
                .removeHeader("Transfer-Encoding")
                .removeHeader("Accept-Encoding")
                .removeHeader("Content-Transfer-Encoding")
        .when()
                .log().all()
                .post(REPORTS_IMPORT_URL_ENDPOINT)
        .then()
                .log().all()
                .statusCode(HttpStatus.SC_CREATED)
                .extract()
                .body()
                .asString();

And this is the request that gets generated

POST /api/v1/reports HTTP/1.1
User-Agent: Apache-HttpClient/4.4.1 (Java/11.0.22)
Authorization: Bearer  <token removed>
Accept: application/json
Connection: close
Transfer-Encoding: chunked
Content-Type: multipart/form-data; boundary=iho-Fb5-DiGJsfc5qR03lUI0sQ_XVoxMGP7gjkt
Host: x.x.x.x

Accept-Encoding: gzip,deflate

--iho-Fb5-DiGJsfc5qR03lUI0sQ_XVoxMGP7gjkt
Content-Disposition: form-data; name="file"; filename="Report.xml"
Content-Type: text/xml
Content-Transfer-Encoding: binary

<?xml version="1.0" encoding="utf-8" ?>
<reportexport>
    <name>Report</name>
     <field1>sample value</field>
</reportexport>
--iho-Fb5-DiGJsfc5qR03lUI0sQ_XVoxMGP7gjkt--

I know there’s a PR up to remove the deprecated Content-Transfer-Encoding header, but even still I would need to remove the other two. Does anybody have any idea what I’m doing wrong ? Any help would be really greatly appreciated.

Thanks in advance

mmcdermo2 avatar Apr 25 '24 08:04 mmcdermo2