How to send a multi-page request using RestAssured?

I have @Controllerwith a method with a signature like this:

@PostMapping
@ResponseBody
public ResponseEntity<Result> uploadFileAndReturnJson(@RequestParam("file") MultipartFile file) {}

I want to build a multi-page request without physically creating any file. I tried to do it like this:

private MultiPartSpecification getMultiPart() {
    return new MultiPartSpecBuilder("111,222")
            .mimeType(MimeTypeUtils.MULTIPART_FORM_DATA.toString())
            .controlName("file")
            .fileName("file")
            .build();
}

Response response = RestAssured.given(this.spec)
            .auth().basic("admin", "admin")
            .multiPart(getMultiPart())
            .when().post(URL);

Sorry, I got the answer:

The required request fragment 'file' is missing

I tried looking at RestAssured unit tests, and it seems like I'm doing it right. If I try to pass byte [] or InputStream instead of String, an exception is thrown:

Unable to retry the request with a non-repeating request object.

Thanks for the help.

+5
source share
2 answers

, []. MultiPartSpecBuilder(byte[] content), .

private MultiPartSpecification getMultiPart() {
         return new MultiPartSpecBuilder("Test-Content-In-File".getBytes()).
                fileName("book.txt").
                controlName("file").
                mimeType("text/plain").
                build();
   }

, [], https://github.com/rest-assured/rest-assured/issues/507. auth, .

.auth().preemptive.basic("admin", "admin")
+4
try {

    RestAssured.given()
            .header(new Header("content-type", "multipart/form-data"))
            .multiPart("file",new File( "./src/main/resources/test.txt"))
            .formParam("description", "This is my doc")
            .auth().preemptive().basic(loginModel.getUsername(), loginModel.getPassword())
            .when()
            .post(URL)
            .then()
            .assertThat()
            .body(matchesJsonSchemaInClasspath("schemas/members/member-document.json"));
}
catch(Exception e) {
    Assert.assertEquals(false, true);
    logger.error(e.getMessage(), e);
}
0

Source: https://habr.com/ru/post/1663642/


All Articles