rest-assured body(InputStream) does not work

Viewed 371

I am using rest-assured 4.3.3 (the version is forced by the newest Spring Boot 2.5.0).

Rest-assured interprets the InputStream as Object and cannot handle it. When I press Ctrl+Q in IntelliJ IDEA (Javadoc) over .body(getInputStream()), it shows the signature RequestSpecification body(InputStream body) so the compiler should understand properly which of the the heavily overloaded .body() methods is supposed to be used.

However, when I replace getInputStream() with getFile(), it works. That's how I know that the XML in the resource file is correct.

The documentation says for both the methods body(File body) as well as body(InputStream body) exactly the same:

Specify file content that'll be sent with the request.

So my understanding is that these are just two alternative ways how to pass the body if I have it in a file.

My code:

class MyTest  {
    @Value("classpath:/valid-request.xml")
    private Resource validRequest;
    ....
    @Test
    void myTest() {
        given()
                .header("Content-Type", "text/xml; charset=UTF-8")
                .header("SOAPAction", "myService")
                .body(validRequest.getInputStream())  // this does not work
//              .body(validRequest.getFile())  // this works
        .when()
                .post("/myApi")
        .then()
                .log().ifValidationFails()
                .statusCode(200)
                .contentType("text/xml")
        ;
        
    }
}

Error message:

java.io.BufferedInputStream@6ceb953
2021-06-11 17:35:47.083 ERROR [,628aa0b986fe0c98,628aa0b986fe0c98] 11428 --- [o-auto-1-exec-1] com.sun.xml.messaging.saaj.soap          : SAAJ0511: Unable to create envelope from given source

I think that it is caused by a bug in rest-assured, but just for a check, maybe you might spot a flaw in my code better than I.

Anyway, I created an issue https://github.com/rest-assured/rest-assured/issues/1480. The same error has been reported as "fixed" in the closed issue https://github.com/rest-assured/rest-assured/issues/1040.

1 Answers

Actually no, it's not a bug. It says InputStream as a method parameter, because it expects that you will pass an object, which will be interpreted and serialized by InputStream. The best way is to use POJO-classes for request body payloads, however in your case I'd suggest to go ahead and read that file as a String and pass as a param into body(), this should be working.

Related